Link to home
Start Free TrialLog in
Avatar of tora111
tora111

asked on

Assign datatable from DataSet

I want to assign a datatable to another datatable (create from a datasoruces)

            Dim mdtPO As New OrderTrackingDataSet.OrderMasterDataTable         '<---  data table created by datasources
            cnn.Open()
            mDA = New System.Data.OleDb.OleDbDataAdapter("Select * from " & mTableName, mCnn)
            mDA.Fill(mDS, mTableName)
            mCB = New System.Data.OleDb.OleDbCommandBuilder(mDA)
            mdtPO = mDS.Tables(mTableName)
            cnn.Close()

However I got the following error message !
"Unable to cast object of type 'System.Data.DataTable' to type 'OrderMasterDataTable'."

So how can I assign datatable "mDS.Tables(mTableName)" to mdtPO ?

Thanks in advance!

Avatar of Arthur_Wood
Arthur_Wood
Flag of United States of America image

try this:

     Dim mdtPO As New OrderTrackingDataSet.OrderMasterDataTable         '<---  data table created by datasources
     cnn.Open()
     mDA = New System.Data.OleDb.OleDbDataAdapter("Select * from " & mTableName, mCnn)
     mDA.Fill(mDS, mTableName)
     mCB = New System.Data.OleDb.OleDbCommandBuilder(mDA)
     mdtPO = cType( mDS.Tables(mTableName), OrderTrackingDataSet.OrderMasterDataTable)
     cnn.Close()

AW
Avatar of tora111
tora111

ASKER

Arthur_Wood,

It got the same error "Unable to cast object of type 'System.Data.DataTable' to type 'OrderMasterDataTable'."

It seems cannot assign to a datataboe to  a datatable(from data sources, xsd) directly !!!
 
I would guess that 'OrderMasterDataTable' is a class which inherits from a datatable with some methods thrown in for fun and profit.  You might need to add the records one at a time.
Avatar of Bob Learned
How is mDS declared and set?

Bob
Avatar of tora111

ASKER

OrderTrackingDataSet a xsd  is created by data sources

mDS is a dataset
mDs should be OrderTrackingDataSet, and not just plain ol' DataSet.

Bob
Avatar of tora111

ASKER

Bob, it doesn't a matter! My objective is to allow add/change/delete the "OrderTrackingDataSet.OrderMasterDataTable" by coding.

Is it possible to use coding method to manipulate a xsd datatable ?
It does matter if you are trying to cast a DataTable from the strong type DataSet into a strong type.

Bob
Avatar of tora111

ASKER

I see, in other words, is it possible to manipulate a datatable from a strong type dataset by coding method ? Because I don't know how to ...., could you give me some example for it !
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

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 tora111

ASKER

Thanks Bob, I'm wonder if we can use tableadapter and bindingsource to manipulate the strong type dataset table then it'll be more simple.

But I don't how to change the database source (for example a mdb file), I don't want to hard code the database because sometimes we need to deploy to different location and mdb file!!!