Database Design

Query

  1. create table tbl_student(Student_ID int identity(1,1),
  2. StudentName varchar(80),RollNo int,Class varchar(50),
  3. Section varchar(50))

Design

table design view

Now I am creating a Webform to insert some data to the database.

Dropdownindex.aspx:

Dropdownindex

Dropdownindex.aspx.cs:
  1. protected void Button1_Click(object sender, EventArgs e)
  2. {
  3. try
  4. {
  5. string str = "Data Source=.;Initial Catalog=Wordpress;Integrated Security=True";
  6. SqlConnection con = new SqlConnection(str);
  7. SqlCommand cmd = new SqlCommand("insert into tbl_student ([StudentName],[RollNo],[Class],[Section]) values (@StudentName,@RollNo,@Class,@Section)", con);
  8. con.Open();
  9. cmd.Parameters.AddWithValue("@StudentName", TextBox1.Text);
  10. cmd.Parameters.AddWithValue("@RollNo", TextBox2.Text);
  11. cmd.Parameters.AddWithValue("@Class", DropDownList1.Text);
  12. cmd.Parameters.AddWithValue("@Section", DropDownList2.Text);
  13. cmd.ExecuteNonQuery();
  14. Label5.Text = "Submitted Successfully";
  15. con.Close();
  16. }
  17. catch(Exception ex)
  18. {
  19. throw ex;
  20. }
  21. }
Output:

Output

Now create a Webform for Displaying the GridView on the Index change event of dropdownlist.

DropdownGrid.aspx:

Code:

Here I am hardcoding the dropdown list items:
  1. <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
  2. <asp:ListItem>--Select--</asp:ListItem>
  3. <asp:ListItem Value="UKG">UKG</asp:ListItem>
  4. <asp:ListItem Value="I ">I </asp:ListItem>
  5. <asp:ListItem Value="II ">II</asp:ListItem>
  6. <asp:ListItem Value="III ">III</asp:ListItem>
  7. <asp:ListItem Value="IV ">IV </asp:ListItem>
  8. <asp:ListItem Value="V ">V</asp:ListItem>
  9. <asp:ListItem Value="VI ">VI</asp:ListItem>
  10. <asp:ListItem Value="VII ">VII</asp:ListItem>
  11. <asp:ListItem Value="VIII ">VIII</asp:ListItem>
  12. <asp:ListItem Value="IX ">IX</asp:ListItem>
  13. <asp:ListItem Value="X ">X</asp:ListItem>
  14. <asp:ListItem Value="XI ">XI</asp:ListItem>
  15. <asp:ListItem Value="XII ">XII</asp:ListItem>
  16. </asp:DropDownList>
dropdown list

DropdownGrid.aspx.cs:

Here is the code for displaying the GridView on Index change event of dropdownlist:
  1. protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
  2. {
  3. string str = "Data Source=.;Initial Catalog=Wordpress;Integrated Security=True";
  4. SqlConnection con = new SqlConnection(str);
  5. con.Open();
  6. SqlCommand cmd1 = new SqlCommand("Select StudentName,RollNo,Section from tbl_student where Class ='" + DropDownList1.SelectedItem.Text + "'", con);
  7. SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
  8. DataSet ds = new DataSet();
  9. da1.Fill(ds, "tbl_student");
  10. GridView1.DataMember = "tbl_student";
  11. GridView1.DataSource = ds;
  12. GridView1.DataBind();
  13. con.Close();
  14. }
Output:

show data