Monday, September 29, 2008

What is the difference between 3 Tier & 3 Layer Applications?

The terms tier and layer are frequently used interchangeably, but actually there is a difference between them:

Tiers indicate a physical separation of components, which may mean different assemblies such as DLL, EXE etc on the same server or multiple servers; but layers refers to a logical separation of components, such as having distinct namespaces and classes for the Database Access Layer (DAL), Business Logic Layer (BLL) and User Interface Layer (UIL).

Therefore, tier is about physical separation and units of deployment, and layers are about logical separation and units of design.

Thursday, September 25, 2008

How to Make a Captcha Image in C# .NET



What Captcha stand for?
Completely Automated Public Turing test to tell Computers and Humans Apart.
The Captcha technology help you to make sure your site is reasonably secure against automated attacks.

Write the following code in a class named Captcha :

public class Captcha
{
    //make the captcha image for text
    public Bitmap MakeCaptchaImage(string txt, int width, int hight, string fontFamilyName)
    {
        //make the bitmap and the associated Graphics object
        Bitmap bm = new Bitmap(width, hight);
        Graphics gr = Graphics.FromImage(bm);
        gr.SmoothingMode = SmoothingMode.HighQuality;
        RectangleF recF = new RectangleF(0, 0, width, hight);
        Brush br;
        br = new HatchBrush(HatchStyle.SmallConfetti, Color.LightGray, Color.White);
        gr.FillRectangle(br, recF);
        SizeF text_size;
        Font the_font;
        float font_size = hight + 1;
        do
        {
            font_size -= 1;
            the_font = new Font(fontFamilyName, font_size, FontStyle.Bold, GraphicsUnit.Pixel);
            text_size = gr.MeasureString(txt, the_font);
        }
        while ((text_size.Width > width) || (text_size.Height > hight));
        // Center the text.
        StringFormat string_format = new StringFormat();
        string_format.Alignment = StringAlignment.Center;
        string_format.LineAlignment = StringAlignment.Center;

        // Convert the text into a path.
        GraphicsPath graphics_path = new GraphicsPath();
        graphics_path.AddString(txt, the_font.FontFamily, 1, the_font.Size, recF, string_format);

        //Make random warping parameters.
        Random rnd = new Random();
        PointF[] pts = { new PointF((float)rnd.Next(width) / 4, (float)rnd.Next(hight) / 4), new PointF(width - (float)rnd.Next(width) / 4, (float)rnd.Next(hight) / 4), new PointF((float)rnd.Next(width) / 4, hight - (float)rnd.Next(hight) / 4), new PointF(width - (float)rnd.Next(width) / 4, hight - (float)rnd.Next(hight) / 4) };
        Matrix mat = new Matrix();
        graphics_path.Warp(pts, recF, mat, WarpMode.Perspective, 0);

        // Draw the text.
        br = new HatchBrush(HatchStyle.LargeConfetti, Color.LightGray, Color.DarkGray);
        gr.FillPath(br, graphics_path);

        // Mess things up a bit.
        int max_dimension = System.Math.Max(width, hight);
        for (int i = 0; i <= (int)width * hight / 30; i++)
        {
            int X = rnd.Next(width);
            int Y = rnd.Next(hight);
            int W = (int)rnd.Next(max_dimension) / 50;
            int H = (int)rnd.Next(max_dimension) / 50;
            gr.FillEllipse(br, X, Y, W, H);
        }
        for (int i = 1; i <= 5; i++)
        {
            int x1 = rnd.Next(width);
            int y1 = rnd.Next(hight);
            int x2 = rnd.Next(width);
            int y2 = rnd.Next(hight);
            gr.DrawLine(Pens.DarkGray, x1, y1, x2, y2);
        }
        for (int i = 1; i <= 5; i++)
        {
            int x1 = rnd.Next(width);
            int y1 = rnd.Next(hight);
            int x2 = rnd.Next(width);
            int y2 = rnd.Next(hight);
            gr.DrawLine(Pens.LightGray, x1, y1, x2, y2);
        }
        graphics_path.Dispose();
        br.Dispose();
        the_font.Dispose();
        gr.Dispose();
        return bm;
    }
}

Write the following code in the page load event:

Captcha oCaptcha = new Captcha();
Random rnd = new Random();
string[] s = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
int i;
StringBuilder sb = new StringBuilder(4);
for (i = 0; i <= 4; i++)
{
    sb.Append(s[rnd.Next(1, s.Length)]);
}
Bitmap bm = oCaptcha.MakeCaptchaImage(sb.ToString(), 200, 100, "Arial");
string url = Server.MapPath("~/Images/Captcha.bmp");
bm.Save(url);
Image1.ImageUrl = "~/Images/Captcha.bmp";

Tuesday, September 9, 2008

The difference between Server.Transfer and Response.Redirect in ASP .NET

Server.Transfer processes the page from one page directly to the next page without making a round-trip back to the client's browser. This way is faster, with a little less overhead on the server.
However, it does not update the clients url history list or current url.

Response.Redirect, as expected, is used to redirect the user's browser to another page or site. It does perform a trip back to the client where the client's browser is actually redirected to the new page.
The browser history list is updated to reflect the new address.

Sunday, September 7, 2008

Javascript is not working with AJAX

If you are using AJAX, and control (inside an UpdatePanel) causes a postback that would normally run Javascript, and you find the Javascript is not running, add a Postback Trigger to the UpdatePanel using the ID of the control that causes the postback.

And this would solve the issue.

Friday, September 5, 2008

How To Make Previous Calendar Dates Not Selectable in ASP.NET

In order to make all dates before the current date, not able to be selected, in the onDayRender event for your Calendar:


if (e.Day.Date < DateTime.Today)
{
    e.Day.IsSelectable = false;
}

To make it more obvious to the end user, also add:

e.Cell.BackColor = Drawing.Color.GhostWhite;
e.Cell.ForeColor = Drawing.Color.Gainsboro;

Wednesday, September 3, 2008

How can you speed up your ASP .NET application in the Production Environment?

After you finish your ASP .NET Application "Developing and Testing", and before you upload the application to a production server, don't forget to change the value of debug="true" to false in the compilation section in the Web.Config file.

This would increase the performance of your application.

Monday, September 1, 2008

How to Get the referring page on Page_Load event in ASP .NET

When a page loads, in order to get the name of the page that sent you there, all you need to use is:


Request.UrlReferrer.ToString();

You can create a global variable to hold it:

string sReferrer = "";

Then, in the Page_Load event, assign it:

if (!Page.IsPostback)
{
    sReferrer = Request.UrlReferrer.ToString();
}

Or, you can put it in ViewState at that time:

if (!Page.IsPostback)
{
    ViewState("Referrer") = Request.UrlReferrer.ToString();
}

From there, on out, within that page, you can use either the variable, or ViewState, within that page as a link, or whatever you need it for.

Hire Me

Follow me on Facebook

Follow me

Do you find this Blog helpful?

Follow by Email

About Me

My Photo
Expert Senior Software Developer

Microsoft Business Card