Jean Paul's Blog

There are 2 types of People in the World, One who Likes SharePoint and..

  • Microsoft MVP

  • MindCracker MVP

  • CodeProject MVP

  • eBook on SharePoint 2010

  • eBook on Design Patterns

  • eBook on Windows Azure

  • NLayers Framework @ CodePlex

  • MSDN Forums

  • .Net vs. Java

    Due to Public Demand

Posts Tagged ‘asp.net’

What is Host Header and how to configure it?

Posted by JP on January 11, 2011

 

Host Header is a third piece of information to identify a web site.

Thus the web site can be identified by using the following:

    • IP Address
    • Port
    • Host Header

Host Headers was introduced in HTTP 1.1

Eg: http://www.google.com

www.google.com is the host header

Configuration in IIS

We can configure host header in IIS using inetmgr application.

After configuration we need to publish the host header in the WINS (Windows Name Service) or DNS (Domain Name Service) depending on the site is an intranet or internet site.

Advertisements

Posted in ASP.NET, IIS | Tagged: , , | Leave a Comment »

For Beginners: In ASP.NET how to show a Javascript Alert message box?

Posted by JP on December 2, 2010

I have seen frequently that beginners in ASP.NET try to use Windows Forms MessageBox and fails.

I would like to say that MessageBox class is meant for Windows Forms Application and not ASP.NET Web applications.

In ASP.NET applications, you have to:

Use either a page to show the message

or

Use javascript alert function to show the message (the example is given below)

Example

Create a new web application using File > New Project > Web > ASP.NET Web Application

image

In the solution Explorer, right click on the Default.aspx and use View Code command.

Replace the Page_Load method code as below:

protected void Page_Load(object sender, EventArgs e)

{

    string message = "Test Message Box in ASP.NET";

    string script = "<script language=’javascript’>alert(‘" + message + "’);</script>";

    ClientScript.RegisterStartupScript(GetType(), "key", script);

}

Now you can execute the application.

Output

On loading the page you will see the following message box (if not enable javascript in your browser)

image

Posted in C# | Tagged: , , | Leave a Comment »

Deploying ASP.NET application to Production Server using FTP

Posted by JP on November 30, 2010

Visual Studio really makes it simple for deploying web applications.

In this article we are going to use FTP for deployment.

Scenario

Hope you have a web application with multiple folders and dozens of file.  Selecting each item and uploading to the site is really tedious job.  We can simplify it by the following.

Pre-requisites

You should have the following:

  • FTP Server Name
  • Username and Password
  • Folder on server

Deployment

Open the solution and right click on the web project and choose the option Publish.

image

You should see the following dialog window.

image

Click on the “…” button highlighted against the Target location item.

You will be prompted with the dialog box below.

image

Enter your Server name, Directory, Username and Password.

Uncheck the Anonymous Login checkbox.

After entering, cilck the Open button.  This will close the dialog box.

Now you can click Publish to do the deployment.

Posted in ASP.NET, IIS | Tagged: , , , | Leave a Comment »

ASP.NET–How to format currency based on user’s browser setting

Posted by JP on November 11, 2010

Hello All.  In this example I am trying to show how to format currency based on user’s browser setting.

Our aim is to show them the formatted currency like:

Format Language
$1,000.00 United States
1,000.00د.إ.‏ Arabic
R$ 1.000,00 Portugese
1.000,00 TL Turkish

Step 1: Create the ASP.NET Application

Create a new web application and on the default page place a button and label.

On the button click you can add the following code.

protected void Button1_Click(object sender, EventArgs e)

{

    CultureInfo cultureInfo = CultureInfo.CreateSpecificCulture(this.Request.UserLanguages[0]);

 

    double value = 1000;

    Label1.Text = value.ToString("C", cultureInfo.NumberFormat);

}

The code collects the user language from Request object and uses the ToString() method to format the value.

That finishes the coding part.. Now you have to see the things working.  For this you have to run the application and test it by changing the browser language.  I will guide you through it.

Step 2: Execute the application

You can execute the application and see the page opened in your browser.

image

Try clicking the button and you can see the number formatted in current language.

Step3: Change Browser Language

In this step we have to change the browser language.

If you are using Internet Explorer choose Internet Options screen.

image

Click the Languages button on the bottom and you can see the following screen.

image

Click the Add button to add a new language.  Select Arabic, move it up using the “Move up” button and click Ok.

image

Now you are ready to test the code in Arabic.

Click the “Change Format” button again and you can see the number format in Arabic.

image

Note: After changing the languages to back in browser you might need to restart it.

Posted in C# | Tagged: , , , | Leave a Comment »

HttpHandler – A Certificate example

Posted by JP on October 13, 2010

Scenario

You are a developer at Grade Nineteen University. One of the recent requirments the management had is to create an online certificate.
So that the students that pass out should be able to view their Certificates online. This will definitely add some technological edge to the university.

The student will be given his own certification url as following: http://www.cert19.edu/certificates/100.cert

On accessing the url, the following Certifcate image will be displayed for the student id: 100

(100.cert should be interpreted as “Show Certificate for Student Id as 100)

clip_image002

Easy Way of Implementation – But not the Best

The first thought of implementation would be to create jpg files for all the students and copy them into the cert folder of the website. This approach is feasible but following overheads are there:

· For 1000 students passed out each year it requires 50 MB disk space.

· While backing up database of site, we have to do backup of jpg files too which is tedious.

If we have to take a backup of the site, we have to copy the files separately and do the database backup separately.

Http Handlers for the Rescue – A better Approach

We can achieve the same behavior with little overheads – by avoiding files creation for certificates.

We can use the database records to dynamically create certificate based on the student url.

By default the ISAPI handlers, handle the .aspx url extensions. We have to create a new handler for .jpg file handling that would be redirected to our certificate creation code.

Steps

1. Create a new web application project

Create a new web application project and name it as Grade19University

2. Create a new class and implement the IHttpHandler interface

Create a new class named “CertHandler” and implement the IHttpHandler interface

Add the following code.

public class CertHandler : IHttpHandler

{

public bool IsReusable

{

get { return false; }

}

/// <summary>

/// Core Method

/// </summary>

/// <param name="context"></param>

public void ProcessRequest(HttpContext context)

{

StudentLibrary studentLibrary = new StudentLibrary();

Bitmap image = studentLibrary.GetCertificate

(context.Request.Url.ToString());

if (image != null)

{

context.Response.ContentType = "image/bmp";

image.Save(context.Response.OutputStream, ImageFormat.Bmp);

}

else

context.Response.Write("Invalid Student Id!");

}

}

// The StudentLibrary class contains all student id lookup and image generating code

// A Certificate template is stored in application resource file

3. Modify the config file

Open the web.config file and in the httpHandlers section add the new verb

<httpHandlers>

<add verb="*" path="*.cert" validate="false" type="Grade19University.CertHandler, Grade19University"/>

</httpHandlers>

4. Run the application and test the url

You can execute the application and see the certificates. The library contains 3 students information.

http://localhost:32971/certificates/101.cert

http://localhost:32971/certificates/102.cert

http://localhost:32971/certificates/103.cert

Note

We can add more optimizations to the code like verifying the raw url, customizing the error messages etc.

The source code can be found in c-sharpcorner.com

http://www.c-sharpcorner.com/uploadfile/40e97e/3806/default.aspx

Posted in ASP.NET | Tagged: , , | Leave a Comment »

How to generate a bitmap dynamically in ASP.NET?

Posted by JP on October 12, 2010

There are two steps in this process

Step1: Create the bitmap dynamically

private Bitmap GetDynamicBitmap()

{

Bitmap bitmap = new Bitmap(100, 100);

      Graphics graphics = Graphics.FromImage(bitmap);

      graphics.DrawString("Dynamic Bitmap", new Font("Calibri", 14, FontStyle.Bold), Brushes.Black,

      new PointF(10, 10));

 

      return bitmap;

}            

Step2: Set the http response to bitmap

Bitmap bitmap = GetDynamicBitmap();

context.Response.ContentType = "image/bmp";

bitmap.Save(context.Response.OutputStream, ImageFormat.Bmp);

Posted in C# | Tagged: , , | Leave a Comment »