Mock tests, Interview questions, Tutorials and Tech news
 
 
Home > Programming / tutorials > Beginner’s tutorial for ASP.NET programming – Part 2

Beginner’s tutorial for ASP.NET programming – Part 2

November 12th, 2009 Arunava Leave a comment Go to comments

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

Figure-22: Structure of the ‘LoginTable’

Figure-22: Structure of the ‘LoginTable’

Now we will add the following data in this table

Figure-23: Data in the ‘LoginTable’

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

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()’

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

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

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-28: Click on ‘Submit’ without entering values in the text boxes

Figure-29: Desired output for scenario 1

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-30: User enters a correct entry

Figure-31: Desired output for 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.

Figure-32: User enters  a wrong ‘Name’

Figure-32: User enters a wrong ‘Name’

Figure-33: Desired output for scenario-3

Figure-33: Desired output for scenario-3

So our application is working fine for the entire test scenarios.

Please leave your comments and feedback.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Mixx
  • Google Bookmarks
  • IndianPad
  • Reddit
  1. vijay kumar
    May 9th, 2012 at 06:09 | #1

    sir

    i got the problem with it…it says

    invalidOperationExcaption was unhandled by user code
    The ConnectionString property has not been initialized.

    please give me the solution

  2. s
    April 21st, 2012 at 01:44 | #2

    good tutorail. but I am getting the problem of System.InvalidOperationException’ at sqladapter.fill(dataset); as I am the beginner please help me

  3. s
    April 21st, 2012 at 01:42 | #3

    I am getting System.InvalidOperationException’ at adapt.fill(dataset);

  4. Yscha
    April 20th, 2012 at 10:09 | #4

    Great tutorial! Explain what I need to start ASP. Your work is truely appreciated.

  5. April 16th, 2012 at 08:19 | #5

    nice tutorial….

  6. vvvlow
    March 9th, 2012 at 04:11 | #6

    Just want to add a simple suggest to make this sample more realistic:

    For the Textbox2 (the password input box), change the TextMode from SingleLine to Password.

  7. vvvlow
    March 9th, 2012 at 03:49 | #7

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

  8. shro
    March 8th, 2012 at 05:59 | #8

    Hie how to start sql server 2005 database?
    Without that I cannot make database.

  9. shro
    March 8th, 2012 at 04:25 | #9

    How to start Sql server 2005 database engine?

  10. HANU
    March 6th, 2012 at 01:10 | #10

    this material was so good and much useful to me

    thanx a lot

  11. Hema
    February 22nd, 2012 at 04:18 | #11

    Hema :Excellent, This tutorial is very much useful for me….

  12. Hema
    February 22nd, 2012 at 04:16 | #12

    Excellent, This session is very much useful for me….

  13. Mo Sosti
    February 20th, 2012 at 04:53 | #13

    This is the BEST Tutorial ever….

    Many Thanks…..Keep them coming.

  14. JJ
    February 15th, 2012 at 03:28 | #14

    FIGURE 25 cut off on the right hand side so cant finish my code

  15. Afsha Khan
    January 16th, 2012 at 10:57 | #15

    Superb Explanation…

  16. Dinesh Jain
    December 20th, 2011 at 08:08 | #16

    Excellent … tutorial !
    It clears every doubt of beginners
    Thanks a lot

  17. Nlm
    December 9th, 2011 at 06:07 | #17

    nice tutorial..

    Thanks

  18. Anil Kumar
    December 2nd, 2011 at 04:25 | #18

    No doubt, it very useful for the start. But based on this, can you give me the guidelines to proceed to next step.

    thanks,

    Anil Kumar

  19. Megasoft
    November 29th, 2011 at 01:41 | #19

    These tutorial must be available also in VB.Net 2010

  20. Abs
    November 10th, 2011 at 09:42 | #20

    Everything is going good until i get to the snippet code for searchTable()… can’t see the full code and doesnt even have instruction where to put the code or how to use it.

    Thanks for the help, -aK>

  21. wasim
    October 27th, 2011 at 05:28 | #21

    excellent.. the way you explained is very nice. it’s very easy to understand.thnx a lot

  22. blueking
    July 9th, 2011 at 06:17 | #22

    Thanks man!! it helps me a loot….waiting for a post on “working with master page and controls”

    hurry!! please!!

  23. kc
    June 23rd, 2011 at 12:33 | #23

    nice

  24. May 4th, 2011 at 13:00 | #24

    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)

  25. Ashish
    April 29th, 2010 at 06:58 | #25

    Superb.. loved the way you explained.

  26. apurv
    November 23rd, 2009 at 16:21 | #26

    awesome tutorial! very good hands on starting point for beginners. can you also tell how to disable copy command for password box ?

  1. November 12th, 2009 at 15:23 | #1
Get Adobe Flash playerPlugin by wpburn.com wordpress themes