Tuesday, December 13, 2011

How to bind data to DorpDownList & TextBox using SqlDataReader

In this tutorial we are setting up values in textbox and dropdownlist corresponding to countryid value. when user enters a countryid (available in database table) in textbox and click on submit button then user gets corresponding values as cityid and cityname in another textbox and dropdownlist respectively.

We are using SqlDataReader to perform this task. 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:  
Defalt.aspx code
This is user's input page.Here we are using two textbox and one dropdownlist.
One textbox is for user's input as countryid and other textbox and dropdownlist is for display corresponding output.

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

Step 5:
View in Browser:

No comments:

Popular Posts