Introduction
Today, I have provided an article showing you how to bind a DropDownList with a database and display the bound data in a GridView in ASP.NET. In this article, we create a table in an SQL Server database and bind that table with a DropDownList control. After that, we select an item in the DropDownList and match it with the database table for the records that match with the database. It will be displayed in the GridView control. To do that, we create a connection string object to connect the database with the application and read data from the database using the select command to display data in the DropDownList. All you have to do is implement and hook it up to your requirements or needs. First, start Visual Studio .NET and make a new ASP.NET website using Visual Studio 2010.
Creating Table in SQL Server Database
Now create a table named UserDetail with the columns id, name, country, and city. Set the identity property=true for id. The table looks as in the following.

Now insert some values in this table. The table looks like this.

Now you have to create a website.
- Go to Visual Studio 2010
- New-> Select a website application
- Click OK

Now add a new page to the website.
- Go to the Solution Explorer
- Right-click on the Project name
- Select add a new item
- Add a new web page and give it a name
- Click OK

Design the page and place the required control in it. Now drag and drop one DropDownList control, Button, and GridView control on the form. Let's take a look at a practical example.
Example
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click1" Style="height: 26px" />
<br />
<br />
<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#999999"
BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical">
<AlternatingRowStyle BackColor="#DCDCDC" />
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#0000A9" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>
</div>
</form>
</body>
</html>



Vishnu KumarPosted Apr 27, 2021, 5:59 AM
Please show that how it is work in .net core....?
Goku OpPosted Mar 29, 2021, 4:46 AM
SOLVED!!!!!!!! There is NO OnSelectedIndexChanged event handler for Dropdown. That is why it fetches only id=1 data. What is this Handler?? Ans>>>When an item is changed in ASP.Net DropDownList, the following OnSelectedIndexChanged event handler is executed.Inside the event handler, the selected Text and Value of the DropDownList Selected Item is fetched and displayed using JavaScript Alert message box.
Vivek K. ChauhanPosted Aug 23, 2020, 10:58 AM
On button click the query is fetching id 1 even after selecting 2nd or 3rd option in dropdown. Not getting the reason?
indradeo kumarPosted Aug 7, 2019, 1:18 AM
Only one row display not multiple row
projects funPosted Jun 14, 2019, 2:00 AM
GridView1.DataSource = dt; what does this command do??
harshita chaudharyPosted Jun 14, 2017, 1:35 AM
How to do for multiple dropdown list????
Brajesh SinghPosted Jun 6, 2017, 4:58 PM
DropDownList1.DataTextField = "Name"; DropDownList1.DataValueField = "ID";
Thiruppathi RPosted May 23, 2017, 12:32 AM
Nice Article........
Fzii RosalPosted Jan 28, 2017, 8:07 AM
Hy guys i need help anyone can helpme?
Shobhit KaushikPosted Oct 26, 2015, 11:55 AM
thank u
Rama Gopala Krishna BadithaboinaPosted Oct 6, 2015, 8:32 AM
I am requesting you to make it as i am not much aware about these codings
Rama Gopala Krishna BadithaboinaPosted Oct 6, 2015, 8:31 AM
Sir i request you to make a two drop down boxes and make another box which would our result box when i select the data from the two dropdown lists i should get details in result box
Vandana BochkerPosted Sep 22, 2015, 6:03 AM
why we are using both command and adapter here and iam getting error near da.fill(dt) error is "An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code"
nofact nofacePosted Jun 14, 2015, 9:50 AM
tank you over and over and over again
Vinod BanalaPosted Jun 16, 2014, 6:17 AM
Only first person data is displaying even i select other person name field....what to do
pushpa cpPosted Apr 23, 2014, 7:20 AM
only the 1st person detail is displayed in gridview even if 2nd or 3rd or any other person is selected, kindly reply soon the solution so that any person selected oly his data is displayed........
ravichandran perumalPosted Nov 27, 2013, 10:19 PM
reply as soon as possible
ravichandran perumalPosted Nov 27, 2013, 10:18 PM
string strConnection = "Data Source=.; uid=sa; pwd=wintellect;database=rohatash;"; Now bind the DropDownList with the database table. SqlConnection con = new SqlConnection(str); i want to know is (str)=strConnection;
Vikram RathorePosted Oct 5, 2012, 7:47 AM
Hello you are binding dropdownlist1 twice,was this done with a purpose?
Anubhav SatlePosted Aug 23, 2012, 5:24 AM
hello sir, i want to know the deference between DropDownList1.DataTextField = "Name"; & DropDownList1.DataValueField = "ID"; sir why we use textfild and valuefild..
nitin karandePosted Jun 14, 2012, 5:56 AM
i am doing this example,but it shows complete table...not selected row in my practice.