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 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 would get open. We can now choose “Internet E-mail address” from the editor.
What we plan to do in this application is to store the information of 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 initialized 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 would get 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’ would get created and an event handler called Page_Load( ) would get 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 following figure.