Load DropDownList with SQL Server data
I'm trying to populate a ASP.NET DropDownList with SQL Server data using C#.
I can do this just by using VS2008 Drag 'n Drop features but i would like to accomplish this just by using C# code.
The DropDownList component is in the Default.aspx file, and this is the code i have to add data to my DropDownList:
This code it triggered when i select a value from another DDL in the same page. I debugged the project and i noticed that then the "DropDownList2.DataBind();" happens it jumps back to the top of the function and does this until the page time's out.
protected void DropDownList2_Load(object sender, EventArgs e)
string data = DropDownList1.SelectedValue;
string connStr = ConfigurationManager.ConnectionStrings["MenuDbConnString"].ToString();
DropDownList2.Visible = true;
using (SqlConnection conn = new SqlConnection(connStr))
string sql = "SELECT MenuID, Text, ParentID FROM Menu WHERE ParentID = @MenuID";
SqlCommand cmd = new SqlCommand(sql, conn);
DropDownList2.DataSource = cmd.ExecuteReader();
DropDownList2.DataTextField = "Text";
DropDownList2.DataValueField = "MenuID";
DropDownList2.DataBind(); //start's looping and doesn't stop :(
What is the problem in my code?