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

Be Sociable, Share!
  1. nirmal
    December 9th, 2013 at 07:47 | #1

    i want to complete notes of asp.net please help me

  2. taruni
    November 14th, 2013 at 03:46 | #2

    gud website for beginners

  3. Raasitha
    August 30th, 2013 at 02:46 | #3

    Superbbb tutorial for beginners.Keep rockzzzz

  4. uma
    August 26th, 2013 at 07:30 | #4

    Its very useful fr beginners..plz continue next parts…

  5. Vermew
    August 16th, 2013 at 23:25 | #5

    This is a nice one :D

  6. July 25th, 2013 at 02:06 | #6

    It’s very trouble-free to find out any topic on net as compared to textbooks, as I found this article at this web page.

  7. xyz
    July 25th, 2013 at 01:16 | #7

    good one!

  8. abc
    July 25th, 2013 at 01:14 | #8

    Very helpful for beginners!!

  9. richa
    July 20th, 2013 at 21:17 | #9

    plz….write the code properly for get n set …bcz its not visible

  10. Monika Singania
    July 18th, 2013 at 04:50 | #10

    @vinay
    Did u get the soln…….

  11. Monika Singania
    July 18th, 2013 at 03:47 | #11

    i am getting exception error… adapter.Fill(ds); here… Invalid Operation exception. can any1 help me with this …

  12. lhetz
    July 8th, 2013 at 05:16 | #12

    big help for starting asp.net

  13. nividh
    May 4th, 2013 at 18:59 | #13

    Good one

  14. arhamni
    May 3rd, 2013 at 22:43 | #14

    like this…thanks

  15. April 30th, 2013 at 04:13 | #15

    This is definitely a page beginners have to go thru..

  16. April 30th, 2013 at 00:34 | #16

    Everyone loves it when individuals get together and share opinions.

    Great website, keep it up!

  17. indru
    April 24th, 2013 at 13:46 | #17

    Thank u. Its really helpful.

  18. April 12th, 2013 at 09:40 | #18

    great page …i am very impressed

  19. Sarath
    April 2nd, 2013 at 14:32 | #19

    Great…..

  20. Rk singh
    March 26th, 2013 at 07:02 | #20

    it’s so fine

  21. ky
    March 25th, 2013 at 04:15 | #21

    one of the best site for the beginners….

  22. swamyreddy
    March 24th, 2013 at 23:19 | #22

    ie is very useful for biginners
    s.thanks &regards

  23. Shah
    March 21st, 2013 at 08:56 | #23

    Good tutorial. easy to understand.
    Require tutorial using vb instead of c#.
    Thanks & Regards,

  24. Ashutosh Kr
    March 13th, 2013 at 06:20 | #24

    @rox

    (System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]);

  25. rox
    March 8th, 2013 at 12:03 | #25

    In here fig.25, I can’t see end of the third line (System.Configuration.ConfigurationManager.AppSettings….)

    someone please post a reply with what is that missing part.

  26. kannan
    March 7th, 2013 at 00:32 | #26

    Very helpful tutorial for beginners who want to start coding in ASP.Net

  27. George
    March 5th, 2013 at 06:17 | #27

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

  28. ANGHA
    February 28th, 2013 at 01:09 | #28

    this is A helpfull

  29. ashraf
    February 9th, 2013 at 03:14 | #29

    This is very helpful for new developers not only to develop a system using ASP .Net but the overall procedure.

    Thanks a looooooooooooooooooot.

  30. kahindi
    February 1st, 2013 at 08:39 | #30

    Am so grateful,I have learned a lot from you since i’m a beginner to asp.net

  31. January 29th, 2013 at 04:00 | #31

    @shraddha
    Hi, you just need to change ” to “. Sometimes, when you copy strings, this char is not as same as ‘ ” ‘, so you must replace it and everything should be fine

  32. jyoti
    January 19th, 2013 at 00:57 | #32

    this is really a very nice tutorial for a beginner

  33. J.Thenraj
    January 19th, 2013 at 00:17 | #33

    Its really help to me, Thanking you!

  34. Akshay
    January 15th, 2013 at 00:35 | #34

    really helpful and easy to understand.

  35. veena
    January 12th, 2013 at 00:29 | #35

    the snapshot of the searchtable is not that clear could you pls post it..i am in needfull
    need to know about the connection string source code .Thanks

  36. shraddha
    January 8th, 2013 at 05:09 | #36

    error in Web.confog file
    Error 2 Character ‘”’ (”) is illegal for XML attribute quotes. Please use single quote (&#39) or double quote (&#34). D:\asp\TutorialExample\web.config 26 39 D:\asp\TutorialExample\

  37. Vash
    January 8th, 2013 at 01:37 | #37

    Nice Post. I learned a lot from you since i’m a beginner to asp.net

  38. harry
    January 2nd, 2013 at 16:18 | #38

    excellent tutorial

  39. Amandeep
    January 1st, 2013 at 23:31 | #39

    This is very good tutorial for a fresher. We need some more tutorial for further study.
    Thank you so much.

  40. Aneesh
    December 21st, 2012 at 05:22 | #40

    Very easy to understand..

  41. ravi
    December 16th, 2012 at 02:43 | #41

    @dev
    in Sqlserver – open Object Explorer
    right click on Database, create new datatbase ,
    click created database, right click on table, create table
    name nvarchar(50)
    password nvarchar(50)
    and save it

  42. SHERSHAH
    December 15th, 2012 at 20:44 | #42

    HOW TO Add the following line under the section of web config:

  43. Ratan
    December 8th, 2012 at 02:13 | #43

    Excellent tutorial. I created this application in one go, without getting any exception. Very nice explanation.

  44. dev
    December 7th, 2012 at 07:03 | #44

    hoe to open sql page .. i dono how to create table

  45. Mohammad
    November 4th, 2012 at 16:48 | #45

    Hi vinary..
    don’t forget to close the text with a single quote

    string query = “SELECT * FROM LoginTable WHERE Name=’” + TextBox1.Text + “‘ AND Password=’” + TextBox2.Text+”‘”;

  46. vinay
    October 29th, 2012 at 02:49 | #46

    i am getting exception error… adapter.Fill(ds); here… Invalid Operation exception. can any1 help me with this …

Comment pages
1 2 1009
  1. November 12th, 2009 at 15:23 | #1

Get Adobe Flash playerPlugin by wpburn.com wordpress themes