Friday, December 9, 2011

How to compare, select and bind data from Database Table into DropDownList & TextBox using SqlDataAdapter and DataSet


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 SqlDataAdapter and DataSet to perform this task. where SqlDataAdapter Represents a set of data commands and a database connection that are used to fill the DataSet and update a SQL Server database.

DataSet is a collection of data.Most commonly a dataset corresponds to the contents of a single database table, or a single statistical data matrix, where each column of the table represents a particular variable, and each row corresponds to a given member of the dataset in question.

step by step process of this tutorial is given below.

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.

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

Click on Image to Enlarge

Click on Image to Enlarge

Step 5:
View in Browser:
You have to enter Country Id and click on submit button.This is a view when user enters -1 as country id.

This is a view when user enters 1 as country id.

No comments:

Popular Posts