Link to home
Start Free TrialLog in
Avatar of JRockFL
JRockFLFlag for United States of America

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.ToTable();

}
Unable to cast object of type 'System.Data.DataTable' to type 'ProductDataTable'.

Any ideas?
Avatar of jrsteele
jrsteele

What if you try:

ProductDataTable filteredTable = (dv.ToTable()) as ProductDataTable;
return filteredTable;;
Avatar of JRockFL

ASKER

I get this message...

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)filteredTable;
Avatar of JRockFL

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)filteredTable;

The project would build, but I get
Unable to cast object of type 'System.Data.DataTable' to type 'ProductDataTable'
ASKER CERTIFIED SOLUTION
Avatar of dstanley9
dstanley9

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JRockFL

ASKER

dstanley9,

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.