Reader Level:
ARTICLE

Binding DropDownList With Database in ASP.NET

Posted by Rohatash Kumar Articles | ASP.NET Programming May 08, 2012
Today, I have provided an article showing you how to bind a DropDownList with a database and display the bound data in a GridView.
  • 2
  • 0
  • 168850

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 a SQL Server database and bind that table with a DropDownList control. After that we select an item in the DropDownList and match with the database table for the records that match with the database. It will display 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 requirement or need. First, start Visual Studio .NET and make a new ASP.NET web site 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:

img3.jpg

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

img4.jpg

Now you have to create a web site.

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

img5.jpg

Now add a new page to the website.

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

img6.jpg

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.

.aspx Code

<%@ 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:
 

Code-behind

 

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 and select the name from DropDownList control and click on the Button.

img1.jpg

Now click on the Button.

img2.jpg

Article Extensions
Contents added by nitin karande on Jun 13, 2012
actually i did this example ,but some problem creates in my programme.

complete table show's in my output.

please guide to me.


kind regards,
nk.
 
COMMENT USING

Trending up