Beginner’s tutorial for ASP.NET programming – Part 2
This is in part 2 of the tutorial Beginner’s Tutorial for programming with ASP.NET – Part 1. Please go through this before you continue further.
Creating the Database Table
Here we will create a table called ‘LoginTable’ in our SQL Server. Please have a look at Figure-22 for the design structure of the table. This table contains two columns ‘Name’ and ‘Password’.
Now we will add the following data in this table
So the table at the back end is also prepared for our Tutorial.
Connecting the Database table from our web application
In the next step we will demonstrate how to connect the database table in windows authentication mode from our web application. There are several methods for this but here we will show the method of declaring a key ‘ConnectionString’ and its value in Web.config file. Add the following line under the <appsettings> section of web config:
<add key=” ConnectionString” value=”Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Practice” />
Here as our SQL Server belongs to the same machine we do not mention any server name in the ‘value’.
Integrated Security=SSPI refers windows authentication to connect to the database.
Initial Catalog specifies the Database name in which our table belongs. In our example we create the ‘LoginTable’ under ‘Practice’ database. You may create the table under different database . In that case you specify the database name in place of ‘Practice’.
Creating the code behind page for Login.aspx to make the application ready
The default method for implementing server side code in Visual Studio is to use code-behind pages. When we use code-behind pages, the programming logic is in a separate file than the visual elements of the page.
To go to the page behind page press F7 in the Design view of ‘Login.aspx’ page or from the solution explorer click ‘Login.aspx.cs’ . At the top of this page you will find the default namespaces declared. Most of the general purpose .Net base classes are in a namespace called ‘system’. Now to connect with the SQL Server please add another namespace ‘System.Data.SqlClient’ as shown in the Figure-24:
You can now find a function Page_Load() declared in the code behind page. This is a page event which runs every time the page is requested. Now it is not only the one page event. When an ASP.NET page is requested, there are a series of page events that occur. These events always occur in the same order , which is referred to as the page event life cycle. Let’s discuss on it in brief.
The page event life cycle consists of the following page events, which occur in the following order:
Page request: The page request occurs before the page life cycle begins. When the page is requested by a user, ASP.NET determines whether the page needs to be parsed and compiled or whether a cached version of the page can be sent in response without running the page.
Start:In the start step, page properties such as Request and Response are set.
Page initialization:This page event initializes the page by creating and initializing the web server controls on the page.
Load:This event runs every time the page is requested.
Validation:During validation, the Validate method of all validator controls is called, which sets the ‘IsValid’ property of individual validator controls and of the page.
Postback event handling:If the request is a postback, any event handlers are called.
Rendering: During the rendering phase, the page calls the Render method for each control, providing a text writer that writes its output to the OutputStream of the page’s Response property.
Unload:Unload is called after the page has been fully rendered, sent to the client, and is ready to be discarded.
Now let’s back in our example. Here the user will enter the ’Name’ and ‘Password’ in our Login.aspx page and we will search our ‘LoginTable’ with those credentials to find any existing entry. If such entry exists then user will be redirected to the ‘Welcome.aspx’ page.
So at first we will create a boolean function ‘SearchTable()’ which creates and opens a SQL connection. After that we will search the ‘LoginTable’ with the proper query (with the entered ‘Name’ and ‘Password’) and if any match found then the function will return true, otherwise it returns false. Please find the attached snapshot (Figure-25) to get the code.
The code shown here is self explanatory but let’s again clarify two terms:
DataSet: These are complex objects that allow you to store multiple DataTables of data from a data source. DataSet objects are similar to a virtual database that is inside a web application.
DataAdapter: This object serves as a link between a DataSet object and a data source that can be used for retrieving and saving data. The SqlDataAdapter class is specific to a SQL Server version 7.0 or later database.
Now we will call this ‘SearchTable()’ function when the user click’s the ‘Submit’ button from Login.aspx page. Just go to the Design view of this page and click on the ‘Submit’ button. This will add the button click event at the code behind page like this:
Now add these lines within this function to complete the application.
Now our web application is ready for testing.
Testing the Web Application
Now set the start page ‘Login.aspx’ by right clicking on the project in solution explorer and press F5 to run the application from localhost. If you are working in windows XP please install IIS from the ‘Additional Windows Components’ of Windows XP CD before running the application.
Here we can have three test sceanarios:
User does not enter anything in the text boxes and press the ‘Submit’ button. In this case the ‘Required field Validators ‘ will show ‘Required’ messages. Please have a look at the two figures below to understand the scenario:
User will enter a valid ‘Name’ and ‘Password’ and click’s on ‘Submit’. User will be redirected to ‘Welcome.aspx’ page. Figure-30 and 31 will make yourself clear about this scenario:
User enters a wrong ‘Name’ or ‘Password’ or both and then he/she will be prompted by an error message at the bottom of ‘Login.aspx’ page. Here are the screenshot of the test case and desired output.
So our application is working fine for the entire test scenarios.
Please leave your comments and feedback.