Thursday, December 15, 2011

How to bind data to Grid View using SqlDataReader



In this tutorial we are setting up values in Grid with SqlDataReader. Where this class provides a way of reading a forward-only stream of rows from a SQL Server database.
To create a SqlDataReader, you must call the ExecuteReader method of the SqlCommand object, instead of directly using a constructor.


While the SqlDataReader is being used, the associated SqlConnection is busy serving the SqlDataReader, and no other operations can be performed on the SqlConnection other than closing it. This is the case until the Close method of the SqlDataReader is called. For example, you cannot retrieve output parameters until after you call Close.


Step1:  File->New->Website
Step 2: Create Database named Database.mdf
Create Table Named city under Tables.

city Table:

Step 3:  
aspx page code split view :

Step 4:
aspx.cs page code
Remember to write SqlConnection in one line otherwise it can show error.







Step 5: View in Browser:

Also Read How to bind data to DorpDownList & TextBox using SqlDataReader
Also Read How to select data from Database Table into DropDownList with SqlDataAdapter and DataSet

1 comment:

Anonymous said...

Nice work....its cool

Popular Posts