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’.

Figure-22: Structure of the ‘LoginTable’

Now we will add the following data in this table

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:

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:

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.

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:

Figure-26: Click event function added on the code behind page

Now add these lines within this function to complete the application.

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 sceanarios:

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:

Figure-28: Click on ‘Submit’ without entering values in the text boxes

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:

Figure-30: User enters a correct entry

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.

Very simple and excellent tutorial. It works fine with Visual studio development server. But gives error ‘Failed to access IIS metabase’ when running in ‘Use IIS web server’ from visual studio.Pls help…I am stuck at this point of time.
.

I appreciate your effort to teach ASP.net. The first tutorial is very helpful.
I succeeded in learning the first tutorial .However in the Asp.net part 2 under Private bool(SearchTable)
the code is not clear as the entire part is not visible.If you can please mail the entire code with explation and where to put it would be of great help.

I appreciate your effort to teach ASP.net. The first tutorial is very helpful.
I succeeded in learning the first tutorial .However in the Asp.net part 2 under Private bool(SearchTable)
the code is not clear as the entire part is not visible.If you can please mail the entire code with explation and where to put it would be of great help.

This is a great tutorial!! I have been searching for such a helpful tutorial since a year.. thank you so very much for sharing this.. Now as I am a beginner kindly help us in designing the webpage with CSS (may be) and connecting asp files with css etc..

@shro
I just discover how to do so.
Download Microsoft SQL Server Management Studio Express(SSMSE) and you can manage your SQL Server Express installed with Visual Studio.
If you are running Win7, you have to run the SSMSE under command prompt as administrator.

I faced problem when implement your code.
My Error is:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 – Could not open a connection to SQL Server)