Bind Dropdown from data table in asp.net

   Step 1 -Creating Table in SQL Server Database


create a table named MstUser with the columns id, name, country and city. Set the identity property=true for id.

Step 2- insert data into ‘MstUser’ Table

Like this:-



Step 3 -Now You have to Create a WebApplication
  1.               Go to Visual Studio 2015
  2.          New Project ->Web->Asp.net Web Application->-Web Forms>Click Ok






Step 4-Now Add Web Page
  1. ·         Go to the Solution Explorer
  2. ·         Right –Click on the Project Name
  3. ·         Select  ’add New Item’ option
  4. ·         Add new  Web Form from Items and give Name
  5. ·         Click OK



Step 5-Design the page and place the required control on it.
Now drag and drop one DropDownList control, One Button and  one GridView control on the form from toolbox . Like given bellow .




Step 6-Now add the following namespaces:
  1.  using System.Data.SqlClient;
  2.   using System.Data;

Step 7-Now write the connection string to connect to the database:
string str = "Data Source=RND-PCTPX04\\SQLEXPRESS_2014;Initial Catalog=DemoDb;Integrated Security=True";


Step 8-Now bind the DropDownList with the database table.

            Go to cs file of web form and write this code on page load event
protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(str);
        string com = "Select * from MstUser";
        SqlDataAdapter adpt = new SqlDataAdapter(com, con);
        DataTable dt = new DataTable();
        adpt.Fill(dt);
        DropDownList1.DataSource = dt;
        DropDownList1.DataBind();
        DropDownList1.DataTextField = "UserName";
        DropDownList1.DataValueField = "ID";
        DropDownList1.DataBind();
 
    }

Step 9-Now double-click on the Button control and add the following code:
          SqlConnection con = new SqlConnection(str);
          SqlCommand cmd = new SqlCommand("select * from MstUser where id = '" + Drop    DownList1.SelectedValue + "'", con);
          SqlDataAdapter Adpt = new SqlDataAdapter(cmd);
          DataTable dt = new DataTable();
          Adpt.Fill(dt);
          GridView1.DataSource = dt;
          GridView1.DataBind();
          Label1.Text = "record found";
      


Step 10-Code-behind file
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;

namespace Demo
{
    public partial class ShowDropdown : System.Web.UI.Page
    {
        string str = "Data Source=RND-PCTPX04\\SQLEXPRESS_2014;Initial Catalog=DemoDb;Integrated Security=True";
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(str);
            string com = "Select * from MstUser";
            SqlDataAdapter adpt = new SqlDataAdapter(com, con);
            DataTable dt = new DataTable();
            adpt.Fill(dt);
            DropDownList1.DataSource = dt;
            DropDownList1.DataBind();
            DropDownList1.DataTextField = "UserName";
            DropDownList1.DataValueField = "ID";
            DropDownList1.DataBind();

        }

        protected void Button1_Click1(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(str);
            SqlCommand cmd = new SqlCommand("select * from MstUser 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";
        }
    }
}


Step 11-Now run the application  
You will get desired result means you dropdown list load with data from database

Step 12-Now select the User name from DropDownList control and click on the Button to se all details of user .