Menu



This is in part 2 of the tutorial ASP.NET: How To Create Project/Website In Visual Studio – 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’.
Please click on the photo for large view
Figure-22: Structure of the ‘LoginTable’

Now we will add the following data in this table 

Please click on the figure for large view
Figure-23: Data in the ‘LoginTable’
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:
<appSettings>
<add key=” ConnectionString” value=”Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Practice” />
</appSettings>
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 adds another namespace ‘System.Data.SqlClient’ as shown in the Figure-24:
Please click on the figure for large view


Figure-24: Add the System.Data.SqlClient namespace in the code behind page

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.

Please click on the figure for large view

Figure-25: Details of the function ‘SearchTable()’
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:

Please click on the figure for large view
Figure-26: Click event function added on the code behind page
Now add these lines within this function to complete the application.

Please click on the figure for large view
Figure-27: Call the ‘SearchTable’ function and set the proper output
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 scenarios:
Scenario 1:
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:
Please click on the figure for large view


Figure-28: Click on ‘Submit’ without entering values in the text boxes
Please click on the figure for large view

Figure-29: Desired output for scenario 1

Scenario 2:
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:
Please click on the figure for large view
Figure-30: User enters a correct entry
Please click on the figure for large view
Figure-31: Desired output for correct entry

Scenario-3
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.
Please click on the figure for large view

Figure-32: User enters a wrong ‘Name’
Please click on the figure for large view



Figure-33: Desired output for scenario-3
So our application is working fine for the entire test scenarios.
Please leave your comments and feedback.









0 comments:

Post a Comment

Share your views or discuss.

 
Top