problem with databinding dataviewmanager with dropdownlist at design time
-
At run time need to determine the controls (dropdownlist) and set the values. at design i need to set datasource, datatext,datakey. the problem is these comboboxes receive data from different table but their column names are same and dont want to use different dataset and dataview. Iam using dataviewmanager. the code is as follows string sqlStr ="SELECT * FROM Categories;"; dAdapter = new OleDbDataAdapter(sqlStr,oleDbConnection1); dset = new DataSet(); dAdapter.TableMappings.Add("Table", "Categories"); dAdapter.Fill(dset); DataSet.DefaultViewManager property this.dviewmanager=dset.DefaultViewManager; this.DropDownList1.DataSource=this.dviewmanager; this.DropDownList1.DataTextField = "Categories.CategoryID"; this.Page.DataBind(); it is giving me following error DataBinder.Eval: 'System.Data.DataViewManagerListItemTypeDescriptor' does not contain a property with the name Categories.CategoryID. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Web.HttpException: DataBinder.Eval: 'System.Data.DataViewManagerListItemTypeDescriptor' does not contain a property with the name Categories.CategoryID.
-
At run time need to determine the controls (dropdownlist) and set the values. at design i need to set datasource, datatext,datakey. the problem is these comboboxes receive data from different table but their column names are same and dont want to use different dataset and dataview. Iam using dataviewmanager. the code is as follows string sqlStr ="SELECT * FROM Categories;"; dAdapter = new OleDbDataAdapter(sqlStr,oleDbConnection1); dset = new DataSet(); dAdapter.TableMappings.Add("Table", "Categories"); dAdapter.Fill(dset); DataSet.DefaultViewManager property this.dviewmanager=dset.DefaultViewManager; this.DropDownList1.DataSource=this.dviewmanager; this.DropDownList1.DataTextField = "Categories.CategoryID"; this.Page.DataBind(); it is giving me following error DataBinder.Eval: 'System.Data.DataViewManagerListItemTypeDescriptor' does not contain a property with the name Categories.CategoryID. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Web.HttpException: DataBinder.Eval: 'System.Data.DataViewManagerListItemTypeDescriptor' does not contain a property with the name Categories.CategoryID.
Hi there, You may reconsider using the DataViewManager object as a datasource to bind to a dropdownlist control. Because, in the OnDataBinding event, a DataViewManagerListItemTypeDescriptor object is actually used to populate the item collection of the control, and basically this object does not have either the Categories.ProductID property or the CategoryID. In this case, the simple way is to use one the objects: dataset, datatable, and dataview to bind to the control:
...
DropDownList1.DataSource = dset.Tables["Categories"];
DropDownList1.DataTextField = "CategoryID";
... -
Hi there, You may reconsider using the DataViewManager object as a datasource to bind to a dropdownlist control. Because, in the OnDataBinding event, a DataViewManagerListItemTypeDescriptor object is actually used to populate the item collection of the control, and basically this object does not have either the Categories.ProductID property or the CategoryID. In this case, the simple way is to use one the objects: dataset, datatable, and dataview to bind to the control:
...
DropDownList1.DataSource = dset.Tables["Categories"];
DropDownList1.DataTextField = "CategoryID";
...This solution is not working iam mailing code snippet if you can help I have clearly told datasource,datatextfield to be set at design time The tables are of different name but they have same colums names at design level at run time Iam doing so string sqlStr ="SELECT * FROM Employees"; dAdapter = new OleDbDataAdapter(sqlStr,oleDbConnection1); dAdapter.Fill(dset,"Employees"); sqlStr ="SELECT * FROM Emp"; dAdapter.SelectCommand.CommandText = sqlStr; dAdapter.Fill(dset,"Emp"); this.Page.Databind(); I tried your code it didnt work
-
This solution is not working iam mailing code snippet if you can help I have clearly told datasource,datatextfield to be set at design time The tables are of different name but they have same colums names at design level at run time Iam doing so string sqlStr ="SELECT * FROM Employees"; dAdapter = new OleDbDataAdapter(sqlStr,oleDbConnection1); dAdapter.Fill(dset,"Employees"); sqlStr ="SELECT * FROM Emp"; dAdapter.SelectCommand.CommandText = sqlStr; dAdapter.Fill(dset,"Emp"); this.Page.Databind(); I tried your code it didnt work
Hi there, + You should replace the '<' and '>' characters with '<' and '>' in your post, otherwise people cann't read it. + You don't need to set the DataTextField property at design time, you will do it at runtime. You are still able to use one dataset to fill up data for multilple dropdownlist controls at run time, the sample code looks like this:
string sqlStr ="SELECT * FROM Employees";
dAdapter = new OleDbDataAdapter(sqlStr,oleDbConnection1);
dAdapter.Fill(dset,"Employees");
sqlStr ="SELECT * FROM Emp";
dAdapter.SelectCommand.CommandText = sqlStr;
dAdapter.Fill(dset,"Emp");this.DropDownList1.DataMember = "Employees";
this.DropDownList1.DataTextField = "EmployeeName";//I assume 'EmployeeName' is one of the columns of Employees.this.DropDownList3.DataMember = "Emp";
this.DropDownList3.DataTextField = "EmpName";//I assume 'EmpName' is one of the columns of Emp.this.Page.Databind();
Here, in the sample code the ListControl.DataMember property is used to specify which table will be bound to the control.
-
Hi there, You may reconsider using the DataViewManager object as a datasource to bind to a dropdownlist control. Because, in the OnDataBinding event, a DataViewManagerListItemTypeDescriptor object is actually used to populate the item collection of the control, and basically this object does not have either the Categories.ProductID property or the CategoryID. In this case, the simple way is to use one the objects: dataset, datatable, and dataview to bind to the control:
...
DropDownList1.DataSource = dset.Tables["Categories"];
DropDownList1.DataTextField = "CategoryID";
...Hi Thank you very much for the reply As I have told already I have different tables with same columns name since these are being extracted from XML files. Second point The requirement as come in such a way where in I have to set these at design time not at run time. If these had to be set at run time my code would have worked very well
-
Hi Thank you very much for the reply As I have told already I have different tables with same columns name since these are being extracted from XML files. Second point The requirement as come in such a way where in I have to set these at design time not at run time. If these had to be set at run time my code would have worked very well
-
Perhaps, I misunderstood your requirement. Can we also try to set the DataMember property at design time like DataTextField and DataSource properties?
...DataMember="<%# dset.Tables[0].TableName%>"
DataMember="<%# dset.Tables[0].TableName%>" This work fine with single control if there are more than one dropdownlists it gives error DataMember="<%# dset.Tables[0].TableName%>"//This works fine DataMember="<%# dset.Tables[1].TableName%>"//this gives error DataMember="<%# dset.Tables[2].TableName%>"//this gives error
-
DataMember="<%# dset.Tables[0].TableName%>" This work fine with single control if there are more than one dropdownlists it gives error DataMember="<%# dset.Tables[0].TableName%>"//This works fine DataMember="<%# dset.Tables[1].TableName%>"//this gives error DataMember="<%# dset.Tables[2].TableName%>"//this gives error
+ What does the error say? + I'm not sure if the markup on the page is the same as yours:
<asp:DropDownList id="DropDownList1" runat="server" DataSource="<%# dset %>"
DataMember="<%# dset.Tables[0].TableName%>" DataTextField="<%# dset.Tables[0].Columns[0].ColumnName%>">
</asp:DropDownList>
<asp:DropDownList id="Dropdownlist3" runat="server" DataSource="<%# dset %>"
DataMember="<%# dset.Tables[1].TableName%>" DataTextField="<%# dset.Tables[0].Columns[0].ColumnName%>" >
</asp:DropDownList>In code-behind, I use the sample code in your post, and it's working. + You declare the dset dataset as typesafe or untypesafe?
-
+ What does the error say? + I'm not sure if the markup on the page is the same as yours:
<asp:DropDownList id="DropDownList1" runat="server" DataSource="<%# dset %>"
DataMember="<%# dset.Tables[0].TableName%>" DataTextField="<%# dset.Tables[0].Columns[0].ColumnName%>">
</asp:DropDownList>
<asp:DropDownList id="Dropdownlist3" runat="server" DataSource="<%# dset %>"
DataMember="<%# dset.Tables[1].TableName%>" DataTextField="<%# dset.Tables[0].Columns[0].ColumnName%>" >
</asp:DropDownList>In code-behind, I use the sample code in your post, and it's working. + You declare the dset dataset as typesafe or untypesafe?