Issue dated - 7th July 2003

-


Previous Issues

CURRENT ISSUE
INDIA NEWS
STOCK FILE
INDIA TRENDS
NEWS ANALYSIS
OPINION
INDIA COMPUTES!
E-BUSINESS
COMPANY WATCH
TECHNOLOGY
TECHSPACE
PRODUCTS
COLUMNS
TECH FORUM

THE C# COLUMN

BETWEEN THE BYTES
TECHNOLOGY
SPECIALS <NEW>
Symantec Report
Security Headquarters
JobsDB
MINDPRINTS
HMA BANKBIZ
EC SERVICES
ARCHIVES/SEARCH
IT APPOINTMENTS
WRITE TO US
SUBSCRIBE/RENEW
CUSTOMER SERVICE
ADVERTISE
ABOUT US

 Network Sites
  IT People
  Network Magazine
  Business Traveller
  Exp. Hotelier & Caterer
  Exp. Travel & Tourism
  Exp. Pharma Pulse
  Exp. Healthcare Mgmt.
  Express Textile
 Group Sites
  ExpressIndia
  Indian Express
  Financial Express

 
Front Page > TechSpace > Story Print this Page|  Email this page

Creating Web applications - II

The C# Column - Yashawant Kanetkar

Last time we began creating a registration form for students and defined the required controls.Let us now look at changing the properties of the validation controls. The name requid is given to a RequiredFieldValidator control. This control is used to check whether the user has entered some ID in the uid textbox. If the user fails to enter an ID and clicks on the ‘Submit Form’ button, this validator would check the textbox, display an error message and prevent a postback. To be able to accomplish this we must set the ErrorMessage property of this validator to some string, and most importantly, associate this validator with the uid textbox. We can do so by setting the ControlToValidate property of this control to the uid textbox.

The name compass is given to the CompareValidator control. This control is used to check whether the value in the Password textbox matches the Confirm Password textbox. To be able to do so we need to set the ControlToValidate property of the validator to cpass and ControlToCompare to pass. If the password does not match, an error message should be displayed.

The name checkmail is given to the RegularExpressionValidator. Here too we need to set the ErrorMessage property to a string and the ControlToValidate property to the mail textbox. This is because we want that the e-mail should be in a proper format. To be able to do so we can set the RegularExpression property of this control to the Internet e-mail format. This can be done by clicking a small button that appears beside the property name in the Properties window. On doing so a Regular Expression Editor is opened. We can now choose "Internet E-mail address" from the editor.

What we plan to do in this application is to store information regarding the student in the database. So whenever the student enters his or her information and clicks the ‘Submit Form’ button a handler should get called that will write the details to the database. Add the Click event handler for the ‘Submit Form’ button and write the code in it as shown below:

private void submit_Click ( object sender,
System.EventArgs e )
{
String constr =
@"Provider=Microsoft.Jet.OLEDB.4.0 ;
Data Source=c:\ online.mdb" ;
String cmdstr = "SELECT * from registration" ;
OleDbDataAdapter da ;
da = new OleDbDataAdapter ( cmdstr, constr ) ;
DataSet ds = new DataSet( ) ;
DataTable dt = ds.Tables [ "registration" ] ;
da.Fill ( ds, "registration" ) ;
DataRow row = dt.NewRow( ) ;
row [ 0 ] = uid.Text ;
row [ 1 ] = pass.Text ;
row [ 2 ] = sname.Text ;
row [ 3 ] = address.Text ;
row [ 4 ] = state.Text ;
row [ 5 ] = zip.Text ;
row [ 6 ] = country.Text ;
row [ 7 ] = mail.Text ;
row [ 8 ] = course.SelectedItem.Text ;
dt.Rows.Add ( row ) ;
OleDbCommandBuilder mybuilder ;
mybuilder = new OleDbCommandBuilder ( da ) ;
da.Update ( ds, "registration" ) ;
Response.Redirect ( "Confirm.aspx" ) ;
}

We have used the OLEDB .NET Data provider in this case with a disconnected approach and the data source is the ‘online.mdb’ file stored in the local drive. In this handler we have created the connection string, the command string, the OleDbDataAdapter object and the DataSet object. Next we have filled the DataSet with the table from the database and extracted the Table in a DataTable object referred to by dt. Then we have created a new DataRow object. We have initialised the row fields with the text entered in the corresponding textboxes containing the student’s information. Next we have added the row to the DataTable object referred to by dt. Then we have created an OleDbCommandBuilder object and updated the database using the Update( ) method. After doing this we have used the static method called Redirect( ) of the Response class to browse to a different page called ‘Confirm.aspx’. We can create the ‘Confirm.aspx’ file by simply adding another Webform (.aspx file) to the project (through Solution Explorer), naming it ‘Confirm.aspx’ and adding the text to it.

Compilation

We are now all set to compile the program. We need to do so using ‘Build | Compile’ menu option. On compiling the HTML tags present in the ‘WebForm1.aspx’ file and the code in the ‘WebForm1.aspx.cs’ file called the code-behind file, a new intermediate file gets created. This file contains a class written in C# and is derived from the WebForm1 class that contains code to emit HTML from ASP.NET tags. This class gets compiled to a ‘Registration.dll’ file.

Here we have chosen to deploy our files on the local machine. But if we plan to deploy it on the Web server, we need to copy the ‘.aspx’ and ‘.aspx.cs’ files into the virtual directory. We can also copy the ‘Registration.dll’ file but this is optional because if the file is not deployed, it gets created and cached in the Web server as soon as the first request is made. The process of compilation is shown in the following figure.

Let us now see the ‘WebForm1.aspx’ file in HTML view. This file contains the ASP.NET tags for our application. The first line of this file is known as the @Page directive. This directive is given below:

<%@ Page Language = "c#" Codebehind = "WebForm1.aspx.cs" Inherits = "Registration.WebForm1"%>

This directive defines general attributes and compilation settings for ASPX files (for example, C# or VB). It also specifies the code behind file and the base class from which the intermediate class was derived.

Sending a request

Now, if a client wants to send a request for executing the program that we developed, he can do so by starting the browser and mentioning the following URL.

http://localhost/Registration/WebForm1.aspx

This sends a request to the server. On the server, an object of the class present in ‘Registration.dll’ gets created and an event handler called Page_Load( ) gets called. This handler outputs HTML, which is then rendered on the client browser.

Now whenever a student enters information into the registration form and clicks on the ‘Submit Form’ button, he is presented with the page shown in the figure.

Yashavant Kanetkar, one of the first Express Computer columnists, is an established software expert, speaker and author with several best-sellers to his credit, including titles like “Let Us C” and the “Fundas” series. Contact him at kanet@nagpur.dot.net.in
<Back to top>


© Copyright 2003: Indian Express Group (Mumbai, India). All rights reserved throughout the world. This entire site is compiled in
Mumbai by The Business Publications Division of the Indian Express Group of Newspapers.
Please contact our Webmaster for any queries on this site.