Working With DropDownList SelectedIndexChanged Event

Working with DropDownList SelectedIndex Changed Event

Let's make a scenario to use a SelectedIndexChanged event of a dropdownlist control by binding it with a database.

Create a Table

Here we are taking four attributes. 

 

Add Content

Connection String

Setting Connection String in the web.config file. 

Front-End

Here is how our aspx page looks like. 

Code

Find the aspx.cs code below.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Data;  
  4. using System.Data.SqlClient;  
  5. using System.Configuration;  
  6. using System.Web;  
  7. using System.Web.UI;  
  8. using System.Web.UI.WebControls;  
  9.   
  10. namespace StateManagement  
  11. {  
  12.     public partial class WebForm2 : System.Web.UI.Page  
  13.     {  
  14.           
  15.         SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);  
  16.         protected void Page_Load(object sender, EventArgs e)  
  17.         {  
  18.             
  19.             if (!IsPostBack)  
  20.             {  
  21.                 String strQuery = "SELECT distinct (City) FROM UserDetails";  
  22.   
  23.                 SqlCommand cmd = new SqlCommand();  
  24.                 cmd.CommandType = CommandType.Text;  
  25.                 cmd.CommandText = strQuery;  
  26.                 cmd.Connection = con;  
  27.                 try  
  28.                 {  
  29.                     con.Open();  
  30.                     DropDownList1.DataSource = cmd.ExecuteReader();  
  31.                     DropDownList1.DataTextField = "City";  
  32.                     DropDownList1.DataValueField = "City";  
  33.                     DropDownList1.DataBind();  
  34.                     //   DropDownList2.SelectedIndex = 1;  
  35.                 }  
  36.                 catch (Exception ex)  
  37.                 {  
  38.                     throw ex;  
  39.                 }  
  40.                 finally  
  41.                 {  
  42.                     con.Close();  
  43.                     //  con.Dispose();  
  44.                 }  
  45.             }  
  46.              
  47.         }  
  48.   
  49.         protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)  
  50.         {  
  51.               
  52.             String strQuery = "SELECT distinct (Designation) FROM UserDetails";  
  53.   
  54.             SqlCommand cmd = new SqlCommand();  
  55.             cmd.CommandType = CommandType.Text;  
  56.             cmd.CommandText = strQuery;  
  57.             cmd.Connection = con;  
  58.             try  
  59.             {  
  60.                 con.Open();  
  61.                 DropDownList2.DataSource = cmd.ExecuteReader();  
  62.                 DropDownList2.DataTextField = "Designation";  
  63.                 DropDownList2.DataValueField = "Designation";  
  64.                 DropDownList2.DataBind();  
  65.                 // DropDownList2.SelectedIndex = 1;  
  66.             }  
  67.             catch (Exception ex)  
  68.             {  
  69.                 throw ex;  
  70.             }  
  71.             finally  
  72.             {  
  73.                 con.Close();  
  74.                 con.Dispose();  
  75.             }            
  76.   
  77.         }        
  78.   
  79.         protected void Button2_Click(object sender, EventArgs e) //Gridview binding  
  80.         {  
  81.             SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);  
  82.             SqlCommand cmd = new SqlCommand("select * from UserDetails where City = '" + DropDownList1.SelectedValue + "' and Designation='"+DropDownList2.SelectedValue+"'" , con);  
  83.             SqlDataAdapter Adpt = new SqlDataAdapter(cmd);  
  84.             DataTable dt = new DataTable();  
  85.             Adpt.Fill(dt);  
  86.             GridView1.DataSource = dt;  
  87.             GridView1.DataBind();  
  88.         }  
  89.   
  90.         protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)  
  91.         {  
  92.             //code for  DropDownList2_SelectedIndexChanged  
  93.         }       
  94.   
  95.          }  
  96. }

Output

Select the City DropDownList.
 

Select the Designation DropDownList.

 
 
Click the Search button to display details in the GridView. 
 
Summary

Great! We have implemented binding with a DropDownlist, worked with the SelectedIndexChanged event of DropDownList and GridView binding.

Thanks.

Next Recommended Readings