Reputation: 4698
I have a page with a dropdownlist where you select a semester. Each has it's own unique id. When a semester is selected, I want to populate a dropdownlist of courses from a stored procedure(sp). This sp uses a parameter called semesterID, so it gets all courses with the selected semesterID. My problem is that the semester list always returns 0. Why does this happen?
Here is my C# code:
private void PopulateSemesterList()
{
string connstring;
connstring = ConfigurationManager.ConnectionStrings["dbConn"].ConnectionString;
SqlConnection conn = new SqlConnection(connstring);
SqlCommand cmd = new SqlCommand("dbo.GetSemesters", conn);
conn.Open();
DDSemesters.DataSource = cmd.ExecuteReader();
DDSemesters.DataTextField = "semesterName";
DDSemesters.DataValueField = "semesterId";
this.DataBind();
conn.Close();
conn.Dispose();
DDSemesters.Items.Insert(0, new ListItem("Select Semester", "0"));
DDSemesters.SelectedIndex = 0;
}
protected void DDSemesters_SelectedIndexChanged(object sender, EventArgs e)
{
string connstring;
connstring = ConfigurationManager.ConnectionStrings["dbConn"].ConnectionString;
SqlConnection conn = new SqlConnection(connstring);
SqlCommand cmd = new SqlCommand("dbo.GetCourses", conn);
cmd.Parameters.Add(new SqlParameter("@semesterId", DDSemesters.SelectedItem.Value));
selectedCourseLbl.Text = DDSemesters.SelectedItem.Value;
conn.Open();
DDCourses.DataSource = cmd.ExecuteReader();
DDCourses.DataTextField = "courseName";
DDCourses.DataValueField = "courseId";
this.DataBind();
conn.Close();
conn.Dispose();
DDCourses.Items.Insert(0, new ListItem("Select Course", "0"));
DDCourses.SelectedIndex = 0;
DDCourses.Visible = true;
CoursesLbl.Visible = true;
}
Here is my ASP:
<asp:Label ID="Label1" runat="server" Text="Select Semester"></asp:Label><br />
<asp:DropDownList ID="DDSemesters" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DDSemesters_SelectedIndexChanged" DataTextField="semesterName" DataValueField="semesterId"></asp:DropDownList><br />
<asp:Label ID="selectedCourseLbl" runat="server" Text="Label"></asp:Label>
<asp:Label ID="CoursesLbl" runat="server" Text="Select Course" Visible="false"></asp:Label><br />
<asp:DropDownList visible="false" ID="DDCourses" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DDCourses_SelectedIndexChanged" DataTextField="courseName" DataValueField="courseId"></asp:DropDownList><br />
Upvotes: 0
Views: 100
Reputation: 50
Use Datatable to fetch the values in drop down list.
string connstring;
connstring = ConfigurationManager.ConnectionStrings["dbConn"].ConnectionString;
SqlConnection conn = new SqlConnection(connstring);
SqlCommand cmd = new SqlCommand("dbo.GetSemesters", conn);
conn.Open();
DataTable dt = new DataTable();
dt = cmd.ExecuteReader();
if (dt.Rows.Count > 0)
{
DDSemesters.DataSource = dt;
DDSemesters.DataTextField = "semesterName";
DDSemesters.DataValueField = "semesterId";
DDSemesters.DataBind();
}
conn.Close();
conn.Dispose();
DDSemesters.Items.Insert(0, "Select Semester");
Upvotes: 0
Reputation: 460168
I'd bet any money that your Page_Load
looks like this:
protected void Page_Load(Object sender, EventArgs e)
{
PopulateSemesterList();
// maybe other code
}
change it to
protected void Page_Load(Object sender, EventArgs e)
{
if(!IsPostBack)
PopulateSemesterList();
// maybe other code
}
The problem is that PopulateSemesterList
clears the selection, so you should do this only on the initial load and not on every postback. Page_Load
happens before all events like the SelectedIndexChanged
-event of the DropDownList
.
Upvotes: 4