By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,458 Members | 1,278 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,458 IT Pros & Developers. It's quick & easy.

How to update DataGrid's data to the DB using DataSet ??

P: n/a
To all,

I am writing a C# application; I've a DataGrid to show and edit data;
then I tried to update the updated data by calling ADO_Adapter.update
(ADO_DataSet, "TableName");

But it pop up a DBConcurrencyException!

Anyone get ideas on how to properly use DataGrids and DataSets together

PS. I can post the code if you guys need to know

From Jason (Kusanagihk)

Nov 17 '05 #1
Share this Question
Share on Google+
2 Replies

P: n/a
To all,

I've got one more question....
It seems that DataSet is a set of offline data dragged from DB; so is
it really proper to populate a DataGrid when the data shown can still
be modified? (since it's offline and not the Adodc components from the
VB6 era....)

So, if I really can't update the DataSet directly, should I update DB
info by overriding a DataGrid's eventHandler(s) ??? like the datacell's
clicking event ???

From Jason (Kusanagihk)

Nov 17 '05 #2

P: n/a
To all,

I've solved most of the problem now!
PS. hope this message helps those who met DBConcurrencyException like
my case!

1) I've linked up the DataSet and DataGrid by using SetDataBindings
2) I've added a button to handle all updates within the DataGrid
3) within that button's code, I've setup the required SqlCommand like
Expand|Select|Wrap|Line Numbers
  1. try
  2. {
  3. if (this.mADO_CommandUpdateDGCustomers == null)
  4. {
  5. this.mADO_CommandUpdateDGCustomers = new SqlCommand
  6. ("Update Customers set " +
  7. "CompanyName=@companyName, ContactName=@contactName
  8. where CustomerID=@customerID",
  9. mADO_Connection);
  11. oParam = new SqlParameter ("@companyName",
  12. System.Data.SqlDbType.VarChar, 255, "CompanyName");
  13. oParam.SourceVersion =
  14. System.Data.DataRowVersion.Current;
  15. mADO_CommandUpdateDGCustomers.Parameters.Add (oParam);
  17. oParam = new SqlParameter ("@contactName",
  18. System.Data.SqlDbType.VarChar, 255, "ContactName");
  19. oParam.SourceVersion =
  20. System.Data.DataRowVersion.Current;
  21. mADO_CommandUpdateDGCustomers.Parameters.Add (oParam);
  23. oParam = new SqlParameter ("@customerID",
  24. System.Data.SqlDbType.VarChar, 255, "CustomerID");
  25. oParam.SourceVersion =
  26. System.Data.DataRowVersion.Original;
  27. mADO_CommandUpdateDGCustomers.Parameters.Add (oParam);
  29. this.mADO_Adapter.UpdateCommand =
  30. mADO_CommandUpdateDGCustomers;
  31. }
  32. }
  33. catch (Exception ex)
  34. {
  35. return false;
  36. }
  37. finally
  38. {
  39. oParam = null;
  40. }
  41. return true;
4) please note how I used the SourceVersion and SourceColumn to map the
columns's data with the DataSet's Table's structure (ie. column)

5) finally; apply mADO_Adapter.Update (DataSetName, "TableName");
6) if no exception pops up, you've done it! Check for the DB records if
they were updated or not!

From Jason (Kusanagihk)

Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.