You are re-binding both lists on every page load. You need to wrap that code in an
if (!IsPostBack)
test.
Also, wrap your
SqlConnection
in a
using
block.
And the
SelectedValue
already returns a
string
; there's no need to call
.ToString()
on it.
public partial class CreatAgroup : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["seniorConnectionString"].ConnectionString))
{
con.Open();
using (SqlCommand cmd = new SqlCommand("select st_name, STID from dbo.[Student]", con))
using (SqlDataReader read = cmd.ExecuteReader())
{
DropDownList3.DataSource = read;
DropDownList3.DataTextField = "st_name";
DropDownList3.DataValueField = "STID";
DropDownList3.DataBind();
}
using (SqlCommand cmd = new SqlCommand("select S_name, SID from dbo.[Suporvisor(commtiee)]", con))
using (SqlDataReader read = cmd.ExecuteReader())
{
DropDownList2.DataSource = read;
DropDownList2.DataTextField = "S_name";
DropDownList2.DataValueField = "SID";
DropDownList2.DataBind();
}
}
}
}
protected void DropDownList2_SelectedIndexChanged(Object Sender, EventArgs e)
{
Label1.Text = DropDownList2.SelectedValue;
}
protected void DropDownList3_SelectedIndexChanged(Object Sender, EventArgs e)
{
Label2.Text = DropDownList3.SelectedValue;
}
}
NB: You should avoid using "special" characters in your SQL table and column names. Stick to using A-Z, 0-9, and the underscore character
_. As you've discovered, using "special" characters makes your queries harder to write, because you have to enclose the name in square brackets.