JRockFL
asked on
Strongly Tyed Dataset, Unable to cast object of type 'System.Data.DataTable'
I am using a .NET 2.0 Strongly typed Dataset.
1. Take the strongly typed ProductsDataTable, convert it to a view and filter it, convert it back to ProductsDataTable
public ProductsDataTable GetProducts(int productNo)
{
ProductsTableAdapter ta = new ProductsTableAdapter();
ProductsDataTable dt = new ProductsDataTable();
dt = ta.GetData(productNo);
DataView dv = new DataView(dt);
dv.RowFilter = "InStock <> 0";
dv.Sort = "ProductNo";
return (ProductDataTable)dv.ToTab le();
}
Unable to cast object of type 'System.Data.DataTable' to type 'ProductDataTable'.
Any ideas?
1. Take the strongly typed ProductsDataTable, convert it to a view and filter it, convert it back to ProductsDataTable
public ProductsDataTable GetProducts(int productNo)
{
ProductsTableAdapter ta = new ProductsTableAdapter();
ProductsDataTable dt = new ProductsDataTable();
dt = ta.GetData(productNo);
DataView dv = new DataView(dt);
dv.RowFilter = "InStock <> 0";
dv.Sort = "ProductNo";
return (ProductDataTable)dv.ToTab
}
Unable to cast object of type 'System.Data.DataTable' to type 'ProductDataTable'.
Any ideas?
ASKER
I get this message...
Object reference not set to an instance of an object.
Object reference not set to an instance of an object.
That probably means (dv.ToTable()) as ProductDataTable is returning null.
What if you try:
ProductDataTable filteredTable = (dv.ToTable()) as DataTable;
return (ProductDataTable)filtered Table;
ProductDataTable filteredTable = (dv.ToTable()) as DataTable;
return (ProductDataTable)filtered
ASKER
I get this...
Cannot implicitly convert type 'System.Data.DataTable' to 'ProductDataTable'. An explicit conversion exists (are you missing a cast?)
Then I changed to
DataTable filteredTable = (dv.ToTable()) as DataTable;
return (ProductDataTable)filtered Table;
The project would build, but I get
Unable to cast object of type 'System.Data.DataTable' to type 'ProductDataTable'
Cannot implicitly convert type 'System.Data.DataTable' to 'ProductDataTable'. An explicit conversion exists (are you missing a cast?)
Then I changed to
DataTable filteredTable = (dv.ToTable()) as DataTable;
return (ProductDataTable)filtered
The project would build, but I get
Unable to cast object of type 'System.Data.DataTable' to type 'ProductDataTable'
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
dstanley9,
Thank you! That is what I needed!
Thank you! That is what I needed!
and ProductsDataTable extends DataTable?
If not it shouldn't cast, as it has no idea what to do with the object.
If not it shouldn't cast, as it has no idea what to do with the object.
ProductDataTable filteredTable = (dv.ToTable()) as ProductDataTable;
return filteredTable;;