SgDotNet
Singapore Professional .NET User Group -For Cool Developers

Converting System.data.datatable to typed dataset.datatable.

rated by 0 users
This post has 2 Replies | 1 Follower

Top 100 Contributor
Posts 24
priyaraj Posted: 01-09-2008 11:09 AM

i am getting error "Unable to cast object of type System.data.datatable to type ReceiveproductDetailsDatatable" while trying to convert system.data.datatable to typed dataset.datatable.

I have created a strongly typed dataset with datatable  ProductDetails. The declaration is as below.

ReceiveProducts.ReceiveProductDetailsDataTable ProductDataTbl = new ReceiveProducts.ReceiveProductDetailsDataTable();

 I getting the product details from database and i want to put this in session for editing.

.ReceiveProductDetailsDataTable ProductDataTbl = new ReceiveProducts.ReceiveProductDetailsDataTable();

 I getting the product details from database and i want to put this in session for editing.

 I getting the product details from database and i want to put this in session for editing.

DataSet dsReceive = new DataSet();

DataSet dsReceive = new DataSet();

dsReceive = objBZ.GetReceiveViewDetails(ReceiveListID);

DataTable dt = new DataTable();

DataTable dt = new DataTable();

ProductDataTbl = (ReceiveProducts.ReceiveProductDetailsDataTable)dt;

ReceiveProducts.ReceiveProductDetailsDataTable)dt;

The above statement gives error  "Unable to cast object of type System.data.datatable to type ReceiveproductDetailsDatatable" while trying to convert system.data.datatable to typed dataset.datatable "

Session["Products"] = ProductDataTbl;

"Products"] = ProductDataTbl;

 

How should i do the casting to avoid this error?

Top 500 Contributor
Posts 4

Hi,

 Your code doesn't make any sense. You have not assigned the object dt in the code.

 Suren.

Top 10 Contributor
Posts 2,284

You cannot cast a more generic object into a more specific object when it is in reality not. When an instance of the Animal class is instantiated, its memory structure is not complete enough to make it properly represent a Rabbit object.

You can cast a ReceiveProductDetailsDataTable into a DataTable, but not a DataTable into a ReceiveProductDetailsDataTable. That can only be done if the actual DataTable object being referenced is indeed the more-specific instance ReceiveProductDetailsDataTable.

The melody of logic will always play out the truth. ~ Narumi Ayumu, Spiral

Page 1 of 1 (3 items) | RSS
Copyright SgDotNet 2004-2008
Powered by Community Server (Commercial Edition), by Telligent Systems