Binding DropDownList With Database in ASP.NET

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.

sqlQuery

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

SQL Table

Now you have to create a website.

  • Go to Visual Studio 2010
  • New-> Select a website application
  • Click OK

Visual Studio WIndow

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

Solution Explorer

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>

Now add the following namespaces.

using System.Data.SqlClient;
using System.Data;

Now write the connection string to connect to the database

string strConnection = "Data Source=.; uid=sa; pwd=wintellect;database=rohatash;";

Now bind the DropDownList with the database table.

SqlConnection con = new SqlConnection(str);
string com = "Select * from UserDetail";
SqlDataAdapter adpt = new SqlDataAdapter(com, con);
DataTable dt = new DataTable();
adpt.Fill(dt);
DropDownList1.DataSource = dt;
DropDownList1.DataBind();
DropDownList1.DataTextField = "Name";
DropDownList1.DataValueField = "ID";
DropDownList1.DataBind();

Now double-click on the Button control and add the following code.

SqlConnection con = new SqlConnection(str);  
SqlCommand cmd = new SqlCommand("select * from UserDetail where id = '" + DropDownList1.SelectedValue + "'", con);  
SqlDataAdapter Adpt = new SqlDataAdapter(cmd);  
DataTable dt = new DataTable();  
Adpt.Fill(dt);  
GridView1.DataSource = dt;  
GridView1.DataBind();  
Label1.Text = "record found";  

In the code-behind, write the following code.

To display data in The GridViw, use a DataAdapter object to retrieve the data from the database and place that data into a table.

using System;    
using System.Collections.Generic;    
using System.Linq;    
using System.Web;    
using System.Web.UI;    
using System.Web.UI.WebControls;    
using System.Data.SqlClient;    
using System.Data;    
     
public partial class Default2 : System.Web.UI.Page    
{    
    string str = "Data Source=.;uid=sa;pwd=wintellect;database=rohatash";    
    protected void Page_Load(object sender, EventArgs e)    
    {    
        SqlConnection con = new SqlConnection(str);    
        string com = "Select * from UserDetail";    
        SqlDataAdapter adpt = new SqlDataAdapter(com, con);    
        DataTable dt = new DataTable();    
        adpt.Fill(dt);    
        DropDownList1.DataSource = dt;    
        DropDownList1.DataBind();    
        DropDownList1.DataTextField = "Name";    
        DropDownList1.DataValueField = "ID";    
        DropDownList1.DataBind();    
    }    
    protected void Button1_Click1(object sender, EventArgs e)    
    {    
        SqlConnection con = new SqlConnection(str);    
        SqlCommand cmd = new SqlCommand("select * from UserDetail where id = '" + DropDownList1.SelectedValue + "'", con);    
        SqlDataAdapter Adpt = new SqlDataAdapter(cmd);    
        DataTable dt = new DataTable();    
        Adpt.Fill(dt);    
        GridView1.DataSource = dt;    
        GridView1.DataBind();    
        Label1.Text = "record found";    
    }    
}

Now run the application select the name from the DropDownList control, and click on the Button.

Dropdown List

Now click on the Button.

Dropdown Output


Similar Articles