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

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.