How to copy one datatable to another
Hi
I've got a typed Dataset class, myDatasetclass, which I generate an instance of (myDataset is the instance) and then store data locally inside the tables of this dataset instance.
Instead of "Filling" say, myDataset.tblUsers with a dataadapter directly however, my app sends this table to a windows service, which contacts a backend database, gets the data, and returns the 'myDataset.tblUsers', containing data, to my app.
Problem is, I need to be able to say something like:
Code Snippet
myDatasetclass myDataset = new myDatasetclass();
myDataset.tblUsers = myService.GetDatafromdbase(myDataset.tblUsers);
(this last part 'myDataset.tblUsers' is the table being sent to the service)
But it says myDataset.tblUsers (the first part above before the '=', is read-only, and can't be passed the new table with data which was returned from myService.
However, if I do this it works fine:
Code Snippet
Datasetclass.tblUsers mytblUsers = new Datasetclasss.tblUsers();
mytblUsers = myService.GetDatafromdbase(mytblUsers);
Only then, I'm filling a table by itself, instead of a table within the Dataset with all the relationships already established (because it's part of a class which establishes these relationships.)
It is becuase that DataTable can't be serialized and send as a result of WebService. Use Dataset for that, and you will get the table from dataset like this:
tblUsers = myService.GetDatafromdbase().Tables[0]; //if GetDatafromdbase returns a dataset that contain datatable tblUsers.
One more thing is that when you returning data, for this case table, better is to call it asynchronously, because every method has asinchronous method and event when that request is completed, it will be like this:
myService.GetDatafromdbaseCompleted += new GetDatafromdbaseCompletedEventHandler(OnUsersData);
myService.GetDatafromdbaseAsync();
for this you will create handler named OnUsersData:
private void OnUsersData(object sender, GetDatafromdbaseCompletedEventArgs e)
{
if (e.Result != null)
{
tblUsers = e.Result.Tables[0];
//do some other stuff if necessary, fill some grid, raise some other event, or any other action.
}
}
Thanks
Where you have:
Code Snippet
tblUsers = myService.GetDatafromdbase().Tables[0]
Do you mean:
Code Snippet
myDataset.tblUsers = myService.GetDatafromdbase().Tables[0]
Because tblUsers is partof the myDataset Dataset, and having this and = returns a 'read-only' error.
The myDataSet.tblUsers property is ReadOnly and can't be assigned to a new DataTable.
Instead you could try Merge:
myDataset.tblUsers.Merge(myService.GetDatafromdbase().Tables[0])
this will bring in all rows from the datatable.
How come I can still 'Fill' the table using an adapter if it is read-only?
eg:
adap.fill(myDataset.tblUsers);
What I did is auto-generated a typed dataset from my database, which also created a C# class file with all the tables and properties and relationships, whcih I intended to just create an instance of and write data to, but instead of using Fill, I'd use the
Code Snippet
myDataset.tblUsers = myService.GetDatafromdbase(myDataset.tblUsers);Where the section in brackets is the table being sent to the service empty to be returned containing data.
Would it be best then to just 'rebuilt' all of this dataset in code (thats a lot of code for 50+ tables and relationships), and redefine all the relationships in code, seeing as I can't make an instance of the class mentioned above to write to?
Eg:
Code Snippet
myDataset newdataset = new myDataset();
myDataset.tblUsers newtblUsers = new myDataset.tblUsers();
myDataset.tblUsers newtblUsers = new myDataset.tblUsers();
...etc...
then put the tables in the dataset and define all the relationships?
Seems strange I can't just create an instance of my entire dataset class and then write table data into it with:
myDataset.tblUsers = myService.GetDatafromdbase(myDataset.tblUsers);
when using an adapter I could fill the myDataset.tblUsers table with data regardless of it being read-only.
If i create an instance of the table, but not the table within the dataset, the above works too, but then its a table by itself without relationships.
Labels: ado, class, copy, dataset, datatable, generate, inside, instance, locally, mydataset, mydatasetclass, net, store, typed
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home