DataBind causing infinite loop?
-
I am re-writing a control I was working on to use dynamically created template columns instead of using the designer for it. For one of my columns, I need a DropDown list, this worked in my old version fine. However, when writing the Databind event for the new control I am running into a problem where when I call the DataBind() on the listcontrol, it's causing an infinite loop. It simply cuts out on the bind, and the method re-starts, I cant figure out why this is happening. However, if I simply loop though all the items in the Dataset, and add them to the dropdown list that way, this works fine - however, much rather use databinding to do this.
private void TemplateControl_DataBindingDL(object sender,System.EventArgs e)
{
Debug.WriteLine("Starting DataBind:" + System.DateTime.Now);
DropDownList lc;
lc = (DropDownList) sender;
string strSqlCommand;
DataGridItem container = (DataGridItem) lc.NamingContainer;
SqlConnection sqlConnection1 = new SqlConnection();
DataSet dsTemp = new DataSet();
strSqlCommand = "SELECT " + fieldName + " FROM V_T_TASKS GROUP BY " + fieldName;SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter(strSqlCommand,sqlConnection1); sqlConnection1.ConnectionString = "...//connection string"; sqlConnection1.Open(); sqlDataAdapter1.Fill(dsTemp); Debug.WriteLine("assigning datasource..."); lc.DataSource = dsTemp; lc.DataTextField = fieldName; lc.DataValueField = fieldName; Debug.WriteLine("assignedg datasource... binding"); Debug.WriteLine("Rows count=" + dsTemp.Tables\[0\].Rows.Count); lc.DataBind(); Debug.WriteLine("..bound"); dsTemp.Clear(); Debug.WriteLine("Cleared..."); lc.Items.Add( DataBinder.Eval(container.DataItem,fieldName,"{0}")); Debug.WriteLine("Ending DataBind:" + System.DateTime.Now); lc.SelectedIndex = 0; sqlConnection1.Close(); }
When I run this, it'll just get to the last two debug statements, then jump back upto the start and keep looping - not throwing any errors or exceptions either. If I take out the databind and replace it with a normal loop to add each item in the dataset, it works fine. any ideas?