This tutorial will demonstrate how to bind data from a database to a drop down list using ASP.NET 4.0 and C#.
Adding a Database
To demonstrate databinding to a drop down list from a database, we are going to make a simple web site with a database in it that holds some states. Then, we will bind those states as items in a drop down list. At this point I have created a new ASP.NET Empty Web Site. To begin:
Next, we need to add a table to the database with some sample data in it. To do this:
Now that we have some data setup in our database, we need to add the web page with a drop down list on it. To do this:
After our web form is all setup, all we have left to do is the databinding.
Databinding the DropDownList
Next, we are going to bind the data from our States table to our drop down list. To do this:
If you're looking for a really good web host, try Server Intellect - we found the setup procedure and their control panel, very easy to adapt to and their IT team is awesome!
Testing
To test this out, go ahead and load up the web site. Ensure that the drop down list is populated with the items that correspond to the states we added in the database.
The Default.aspx source looks like this:
To demonstrate databinding to a drop down list from a database, we are going to make a simple web site with a database in it that holds some states. Then, we will bind those states as items in a drop down list. At this point I have created a new ASP.NET Empty Web Site. To begin:
- Right click the project in your solution explorer.
- Select add ASP.NET folder.
- Select App_Data.
- Right click the App_Data folder.
- Select add new item...
- Select a SQL Database.
- Name it 'Database.mdf'.
- Click add.
Next, we need to add a table to the database with some sample data in it. To do this:
- Expand the Database.mdf folder in your server/database explorer.
- Right click the Tables folder.
- Select add new table.
- Add a column to the table named 'State' with a type of 'nvarchar(50)'.
- Save the table as 'States'.
- Add a few states to the Table. I added Alabama, Alaska, and Arizona respectively.
Now that we have some data setup in our database, we need to add the web page with a drop down list on it. To do this:
- Right click the project in your solution explorer.
- Select add new item...
- Select a web form.
- Name it 'Default.aspx'.
- Click add.
- Open Default.aspx to design mode.
- Drag and drop a dropdownlist onto the web form.
After our web form is all setup, all we have left to do is the databinding.
Databinding the DropDownList
Next, we are going to bind the data from our States table to our drop down list. To do this:
- Expand the DropDownList Tasks menu.
- Select Choose Data Source...
- Select <New data source...> from the drop down list.
- For the data source type, choose a Sql Database.
- Click ok.
- For the data connection, choose Database.mdf from the drop down list.
- Click next.
- Choose to save the connection string and click next.
- Leave the default select statement.
- Click next.
- Here you can test the query to ensure that it is working and click finish.
- Ensure that our selected data source is the data source we just added and that our selected data fields for the display and value of the drop down list are both State.
- Click ok.
If you're looking for a really good web host, try Server Intellect - we found the setup procedure and their control panel, very easy to adapt to and their IT team is awesome!
Testing
To test this out, go ahead and load up the web site. Ensure that the drop down list is populated with the items that correspond to the states we added in the database.
The Default.aspx source looks like this:
<body> |
No comments:
Post a Comment