zozig
asked on
DataAdapter Update help
Hi Everyone,
Here is my scenario, I am retrieving a dataset from a webservice and I would like to update my database with the information from the dataset. I'm attempting to perform this with a data adapter and I keep stumbling across this error:
"Update unable to find TableMapping"
Here is a sample of what I am working with:
// Variables
SqlDataAdapter adptAppSession;
DataSet dsAppSession;
// Creating the DataAdpater to be used for update
adptAppSession = new SqlDataAdapter("select * from AppSessions", dbConnection);
// Calling the web services
dsAppSession = webService.getAppSessionTa ble("01/01 /2005","01 /30/2005") ;
// Updating the data adapter
adptAppSession.Update(dsAp pSession);
Can someone please help me with this, it seems like a common thing to do to retrieve a dataset from a webservice and update a database. What am i missing?? Does the structure of the dataset matter? I thought it would infer a schema and match it to the dataAdapter, do I have to do this implicitly and if so, how do I do it? Specific code examples would be greatly appreciated. Thanks
Zozig
Here is my scenario, I am retrieving a dataset from a webservice and I would like to update my database with the information from the dataset. I'm attempting to perform this with a data adapter and I keep stumbling across this error:
"Update unable to find TableMapping"
Here is a sample of what I am working with:
// Variables
SqlDataAdapter adptAppSession;
DataSet dsAppSession;
// Creating the DataAdpater to be used for update
adptAppSession = new SqlDataAdapter("select * from AppSessions", dbConnection);
// Calling the web services
dsAppSession = webService.getAppSessionTa
// Updating the data adapter
adptAppSession.Update(dsAp
Can someone please help me with this, it seems like a common thing to do to retrieve a dataset from a webservice and update a database. What am i missing?? Does the structure of the dataset matter? I thought it would infer a schema and match it to the dataAdapter, do I have to do this implicitly and if so, how do I do it? Specific code examples would be greatly appreciated. Thanks
Zozig
The thing with ADO.NET 1.x is that Updates use the RowState property of the DataRows. So you'd need Rows that are marked as Modified (the UpdateCommand will be fired), Added (the InsertCommand will be fired) and/or Deleted (the DeleteCommand will be fired)
also, unless there's table mappings occurring, you'd be better off using the DataTable as well as the GetChanges() method
// Calling the web services
dsAppSession = webService.getAppSessionTa ble("01/01 /2005","01 /30/2005") ;
if (dsAppSession.HasChanges)
{
adptAppSession.Update(dsAp pSession.T ables[0].G etChanges( ));
}
also, unless there's table mappings occurring, you'd be better off using the DataTable as well as the GetChanges() method
// Calling the web services
dsAppSession = webService.getAppSessionTa
if (dsAppSession.HasChanges)
{
adptAppSession.Update(dsAp
}
ASKER
jonvaugan,
I've added some code that utilizes two datasets as you've mentioned, I now get no error but nothing gets updated to the database, here is the code I am using:
// A new dataset to merge with the one from the web service
dsSessionData = new DataSet();
adptAppSession.Fill(dsSess ionData);
// this is the data set from the webservice
dsAppSession = webService.getAppSessionTa ble("01/01 /2005","01 /30/2005") ;
// Merge the dataset with the webservice dataset
dsSessionData.Merge(dsAppS ession,tru e,MissingS chemaActio n.Add);
// Update the dataset
adptAppSession.Update(dsSe ssionData) ;
Is that what you are recommending? If so, why does nothing get updated, I've created xml files from the webservice dataset and confirmed that I am definately getting data back. Is there another method to use besides merge? Thanks.
Zozig
I've added some code that utilizes two datasets as you've mentioned, I now get no error but nothing gets updated to the database, here is the code I am using:
// A new dataset to merge with the one from the web service
dsSessionData = new DataSet();
adptAppSession.Fill(dsSess
// this is the data set from the webservice
dsAppSession = webService.getAppSessionTa
// Merge the dataset with the webservice dataset
dsSessionData.Merge(dsAppS
// Update the dataset
adptAppSession.Update(dsSe
Is that what you are recommending? If so, why does nothing get updated, I've created xml files from the webservice dataset and confirmed that I am definately getting data back. Is there another method to use besides merge? Thanks.
Zozig
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Jonvaughan,
I've inspected the Datset after the merge and I've noticed that the Row state is 'unchanged' for all the rows. One item that is confusing me is the tablename. The tablename for the DataSet from the webservice states that the name is "Item" and the table I wish to update is AppSessions. I've placed a tablemapping on the dataAdapter to see if that would resolve any descrepancies but still no luck. Any ideas? Here is the code on the dataAdapter:
dsSessionData.Merge(dsAppS ession,fal se,Missing SchemaActi on.Add);
adptAppSession.TableMappin gs.Add("It em", "AppSessions");
adptAppSession.MissingSche maAction = MissingSchemaAction.AddWit hKey;
adptAppSession.Update(dsSe ssionData, "AppSessio ns");
Thanks.
I've inspected the Datset after the merge and I've noticed that the Row state is 'unchanged' for all the rows. One item that is confusing me is the tablename. The tablename for the DataSet from the webservice states that the name is "Item" and the table I wish to update is AppSessions. I've placed a tablemapping on the dataAdapter to see if that would resolve any descrepancies but still no luck. Any ideas? Here is the code on the dataAdapter:
dsSessionData.Merge(dsAppS
adptAppSession.TableMappin
adptAppSession.MissingSche
adptAppSession.Update(dsSe
Thanks.
at which point do you get the error.
You need to create the dataset from the dataAdapter ... through using
adptAppSession.fill(dsAppS
that will create the dataset which is matched to the dataAdapter and contains the entries recovered by "select * from AppSessions".
then you get a dataSet from the Web Service. Which is a separate DataSet.
you then need to use the new dataset to update the existing one ... that will create the new records which will be marked for Insert / Update / Delete
then when you use the Update() method it will update the DB.
Jon