Databinding to a Dropdownlist Problem
-
I have a dropdown list control (lstState) that is contained within a user control (ucAddressInfo) which itself is contained on another user control (ucUserInfo) which finally is contained on an aspx page. So the heirachy is such Parent page is ASPX ------> User control ucUserInfo ------> contains ucAddressInfo --------> contains dropdownlist (lstState) as well as a datagrid (dglist) I then have the following code in the page_load event of the ucAddressInfo control (this is to create som test data. //Create a Datatable with some data DataTable oData = new DataTable(); oData.Columns.Add ("StateAbbrev", typeof(String)); oData.Columns.Add ("StateName", typeof(String)); DataRow oRow = oData.NewRow (); oRow["StateAbbrev"] = "AZ"; oRow["StateName"] = "Arizona"; oData.Rows.Add(oRow); lstState.DataValueField = "StateAbbrev"; lstState.DataTextField = "StateName"; //Set the DataSource of the DropDownList lstState.DataSource = oData; //Clear the datagrid control on the ucAddressInfo //User Control dgStates.Columns.Clear(); dgStates.AutoGenerateColumns = false; BoundColumn bCol = new BoundColumn(); bCol.HeaderText = "StateProvinceName"; bCol.DataField = "StateAbbrev"; dgStates.Columns.Add( bCol ); bCol = new BoundColumn(); bCol.HeaderText = "StateProvinceName"; bCol.DataField = "StateName"; dgStates.Columns.Add( bCol ); dgStates.DataSource= oData; // Do the databind of the Datagrid //This works fine dgStates.DataBind(); //Now do the databind of the DropDown list box lstState.DataBind(); //This fails with the following error "Exception Details: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: value" Has Anyone else had this issue and how have you solved it. I have tried moving the lstState.Databind(); to the aspx page load, the ucAddressInfo OnInit event and numerous other places with no luck!!!! Thanks in Advance Carpe Diem
-
I have a dropdown list control (lstState) that is contained within a user control (ucAddressInfo) which itself is contained on another user control (ucUserInfo) which finally is contained on an aspx page. So the heirachy is such Parent page is ASPX ------> User control ucUserInfo ------> contains ucAddressInfo --------> contains dropdownlist (lstState) as well as a datagrid (dglist) I then have the following code in the page_load event of the ucAddressInfo control (this is to create som test data. //Create a Datatable with some data DataTable oData = new DataTable(); oData.Columns.Add ("StateAbbrev", typeof(String)); oData.Columns.Add ("StateName", typeof(String)); DataRow oRow = oData.NewRow (); oRow["StateAbbrev"] = "AZ"; oRow["StateName"] = "Arizona"; oData.Rows.Add(oRow); lstState.DataValueField = "StateAbbrev"; lstState.DataTextField = "StateName"; //Set the DataSource of the DropDownList lstState.DataSource = oData; //Clear the datagrid control on the ucAddressInfo //User Control dgStates.Columns.Clear(); dgStates.AutoGenerateColumns = false; BoundColumn bCol = new BoundColumn(); bCol.HeaderText = "StateProvinceName"; bCol.DataField = "StateAbbrev"; dgStates.Columns.Add( bCol ); bCol = new BoundColumn(); bCol.HeaderText = "StateProvinceName"; bCol.DataField = "StateName"; dgStates.Columns.Add( bCol ); dgStates.DataSource= oData; // Do the databind of the Datagrid //This works fine dgStates.DataBind(); //Now do the databind of the DropDown list box lstState.DataBind(); //This fails with the following error "Exception Details: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: value" Has Anyone else had this issue and how have you solved it. I have tried moving the lstState.Databind(); to the aspx page load, the ucAddressInfo OnInit event and numerous other places with no luck!!!! Thanks in Advance Carpe Diem
Have you tried naming your data table when you create it and setting that name to the DataMember Property? Try this and see if it works: for your declaration:
DataTable oData = new DataTable("TblStates");
during your binding setup, in this order:lstState.DataSource = oData; lstState.DataMember = "TblStates"; lstState.DataValueField = "StateAbbrev"; lstState.DataTextField = "StateName"; lstState.DataBind();
'Cause I'm living on things that excite me, be it pastries or lobsters or love...
I'm just trying to get by, being quiet and shy, in a world full of push and shove... Jimmy Buffett - The Wino and I know -
Have you tried naming your data table when you create it and setting that name to the DataMember Property? Try this and see if it works: for your declaration:
DataTable oData = new DataTable("TblStates");
during your binding setup, in this order:lstState.DataSource = oData; lstState.DataMember = "TblStates"; lstState.DataValueField = "StateAbbrev"; lstState.DataTextField = "StateName"; lstState.DataBind();
'Cause I'm living on things that excite me, be it pastries or lobsters or love...
I'm just trying to get by, being quiet and shy, in a world full of push and shove... Jimmy Buffett - The Wino and I know -
I have tried that and it is not the data source that is causing the problem as I have tried it with an item list, a collection base, a hashtable and and array, all with the same result. Carpe Diem
Hrmm... this kind of leads me to think that the problem is actually elsewhere. Could you post the entire stack trace? It may help to shed some light. Also, just for simplification, have you tried commenting out the datagrid lines before testing? --Jesse
-
I have a dropdown list control (lstState) that is contained within a user control (ucAddressInfo) which itself is contained on another user control (ucUserInfo) which finally is contained on an aspx page. So the heirachy is such Parent page is ASPX ------> User control ucUserInfo ------> contains ucAddressInfo --------> contains dropdownlist (lstState) as well as a datagrid (dglist) I then have the following code in the page_load event of the ucAddressInfo control (this is to create som test data. //Create a Datatable with some data DataTable oData = new DataTable(); oData.Columns.Add ("StateAbbrev", typeof(String)); oData.Columns.Add ("StateName", typeof(String)); DataRow oRow = oData.NewRow (); oRow["StateAbbrev"] = "AZ"; oRow["StateName"] = "Arizona"; oData.Rows.Add(oRow); lstState.DataValueField = "StateAbbrev"; lstState.DataTextField = "StateName"; //Set the DataSource of the DropDownList lstState.DataSource = oData; //Clear the datagrid control on the ucAddressInfo //User Control dgStates.Columns.Clear(); dgStates.AutoGenerateColumns = false; BoundColumn bCol = new BoundColumn(); bCol.HeaderText = "StateProvinceName"; bCol.DataField = "StateAbbrev"; dgStates.Columns.Add( bCol ); bCol = new BoundColumn(); bCol.HeaderText = "StateProvinceName"; bCol.DataField = "StateName"; dgStates.Columns.Add( bCol ); dgStates.DataSource= oData; // Do the databind of the Datagrid //This works fine dgStates.DataBind(); //Now do the databind of the DropDown list box lstState.DataBind(); //This fails with the following error "Exception Details: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: value" Has Anyone else had this issue and how have you solved it. I have tried moving the lstState.Databind(); to the aspx page load, the ucAddressInfo OnInit event and numerous other places with no luck!!!! Thanks in Advance Carpe Diem