Database Design
Query
- create table tbl_student(Student_ID int identity(1,1),
- StudentName varchar(80),RollNo int,Class varchar(50),
- Section varchar(50))
Design
Now I am creating a Webform to insert some data to the database.
Dropdownindex.aspx: Dropdownindex.aspx.cs: - protected void Button1_Click(object sender, EventArgs e)
- {
- try
- {
- string str = "Data Source=.;Initial Catalog=Wordpress;Integrated Security=True";
- SqlConnection con = new SqlConnection(str);
- SqlCommand cmd = new SqlCommand("insert into tbl_student ([StudentName],[RollNo],[Class],[Section]) values (@StudentName,@RollNo,@Class,@Section)", con);
- con.Open();
- cmd.Parameters.AddWithValue("@StudentName", TextBox1.Text);
- cmd.Parameters.AddWithValue("@RollNo", TextBox2.Text);
- cmd.Parameters.AddWithValue("@Class", DropDownList1.Text);
- cmd.Parameters.AddWithValue("@Section", DropDownList2.Text);
- cmd.ExecuteNonQuery();
- Label5.Text = "Submitted Successfully";
- con.Close();
- }
- catch(Exception ex)
- {
- throw ex;
- }
-
- }
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:
- <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
- <asp:ListItem>--Select--</asp:ListItem>
- <asp:ListItem Value="UKG">UKG</asp:ListItem>
- <asp:ListItem Value="I ">I </asp:ListItem>
- <asp:ListItem Value="II ">II</asp:ListItem>
- <asp:ListItem Value="III ">III</asp:ListItem>
- <asp:ListItem Value="IV ">IV </asp:ListItem>
- <asp:ListItem Value="V ">V</asp:ListItem>
- <asp:ListItem Value="VI ">VI</asp:ListItem>
- <asp:ListItem Value="VII ">VII</asp:ListItem>
- <asp:ListItem Value="VIII ">VIII</asp:ListItem>
- <asp:ListItem Value="IX ">IX</asp:ListItem>
- <asp:ListItem Value="X ">X</asp:ListItem>
- <asp:ListItem Value="XI ">XI</asp:ListItem>
- <asp:ListItem Value="XII ">XII</asp:ListItem>
- </asp:DropDownList>
DropdownGrid.aspx.cs:
Here is the code for displaying the GridView on Index change event of dropdownlist:
- protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
- {
- string str = "Data Source=.;Initial Catalog=Wordpress;Integrated Security=True";
- SqlConnection con = new SqlConnection(str);
- con.Open();
- SqlCommand cmd1 = new SqlCommand("Select StudentName,RollNo,Section from tbl_student where Class ='" + DropDownList1.SelectedItem.Text + "'", con);
- SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
- DataSet ds = new DataSet();
- da1.Fill(ds, "tbl_student");
- GridView1.DataMember = "tbl_student";
- GridView1.DataSource = ds;
- GridView1.DataBind();
- con.Close();
- }
Output: