473,320 Members | 1,979 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Update & Insert in ADO.NET

Yog
I have a datagrid binded to a dataset and once the user updates the grid my
dataset is reflected.

When i wanted to save the data to tables, however i needed to insert this
data into another table instead of the same table.

I am getting the error in the line
oDa.Update(oDs.Tables[0].Select("", "", DataViewRowState.CurrentRows));

"Update requires a valid UpdateCommand when passed DataRow collection with
modified rows.System.InvalidOperationException: Update requires a valid
UpdateCommand when passed DataRow collection with modified rows. at
System.Data.Common.DbDataAdapter"

----------------------------------------------------------------------------------------
below is the code

Expand|Select|Wrap|Line Numbers
  1. public void CreateFieldMapping(DataSet oDs)
  2. {
  3.  
  4. string sMethodName = "[public void CreateFieldMapping(DataSet oDs)]";
  5.  
  6. //===============================================================================
  7. //--- Establish local variable
  8. //===============================================================================
  9. string sProcName;
  10. //string sConnString = Common.BuildConnectionString();
  11. SqlDataAdapter oDa = new SqlDataAdapter();
  12. SqlTransaction oTrn = null;
  13. SqlConnection oCn =  GetConnection();
  14. SqlCommand oInsCmd = null;
  15.  
  16. try
  17.  
  18. //===============================================================================
  19. //--- Open the Connection and create the Transactio
  20. //===============================================================================
  21. oCn.Open();
  22. oTrn = oCn.BeginTransaction();
  23.  
  24. //===============================================================================
  25. //--- Set up the INSERT Comman
  26. //===============================================================================
  27. sProcName = "UEPSP_CONF_MAP_FIELDS_INSERT";
  28. oInsCmd = new SqlCommand(sProcName, oCn, oTrn);
  29. oInsCmd.CommandType = CommandType.StoredProcedure;
  30. oInsCmd.Parameters.Add(new SqlParameter("@MAP_FIELD_CK", SqlDbType.Int,
  31. 4, "MAP_FIELD_CK"));
  32. oInsCmd.Parameters.Add(new SqlParameter("@GROUP_CK", SqlDbType.Int,
  33. 4,"GROUP_CK"));
  34. oInsCmd.Parameters.Add(new SqlParameter("@MAP_TYPE", SqlDbType.VarChar,
  35. 1, "MAP_TYPE"));
  36. oInsCmd.Parameters.Add(new SqlParameter("@MAP_SEQ_NO", SqlDbType.Int, 4,
  37. "MAP_SEQ_NO"));
  38. oInsCmd.Parameters.Add(new SqlParameter("@EFF_DT", SqlDbType.DateTime,
  39. 10, "EFF_DT"));
  40. oInsCmd.Parameters.Add(new SqlParameter("@TERM_DT", SqlDbType.DateTime,
  41. 10, "TERM_DT"));
  42. oInsCmd.Parameters.Add(new SqlParameter("@DISPLAY_NAME",
  43. SqlDbType.VarChar, 30, "DISPLAY_NAME"));
  44. oInsCmd.Parameters.Add(new SqlParameter("@TABLE_NAME",
  45. SqlDbType.VarChar, 30, "TABLE_NAME"));
  46. oInsCmd.Parameters.Add(new SqlParameter("@FIELD_NAME",
  47. SqlDbType.VarChar, 30, "FIELD_NAME"));
  48. oInsCmd.Parameters.Add(new SqlParameter("@FIELD_TYPE",
  49. SqlDbType.VarChar, 20, "FIELD_TYPE"));
  50. oInsCmd.Parameters.Add(new SqlParameter("@MAPPED_FIELD_MIN",
  51. SqlDbType.VarChar, 10, "MAPPED_FIELD_MIN"));
  52. oInsCmd.Parameters.Add(new SqlParameter("@MAPPED_FIELD_MAX",
  53. SqlDbType.VarChar, 10, "MAPPED_FIELD_MAX"));
  54.  
  55. //oInsCmd.UpdatedRowSource = UpdateRowSource.OutputParameters;
  56. oDa.InsertCommand = oInsCmd;
  57.  
  58. //===============================================================================
  59. //--- Add the record(s
  60. //===============================================================================
  61. Common.ShowRowStates(oDs.Tables[0], "*** BEFORE FIELDS INSERTS ***");
  62. oDa.Update(oDs.Tables[0].Select("", "", DataViewRowState.CurrentRows));
  63. Common.ShowRowStates(oDs.Tables[0], "*** AFTER ALL ***");
  64.  
  65. oTrn.Commit();
  66. oCn.Close();
  67. }
  68. catch (CustomException exCustom)
  69.  
  70. //===============================================================================
  71. //--- Rollback the transactio
  72. //===============================================================================
  73. oTrn.Rollback();
  74.  
  75. //---------------------------------------------------------
  76. //--- Push the custom Exception on the stack and re-throw it.
  77. //---------------------------------------------------------
  78. exCustom.CallStack.Push(this.m_sClassName, sMethodName);
  79. throw(exCustom);
  80.  
  81. }
  82. catch (Exception ex)
  83. {
  84. //===============================================================================
  85. //--- Rollback the transaction
  86. //===============================================================================
  87. oTrn.Rollback();
  88.  
  89. //---------------------------------------------------------
  90. //--- Create and throw a custom Exception.
  91. //---------------------------------------------------------
  92. throw(new CustomException("", this.m_sClassName, sMethodName, ex));
  93. }
  94. finally
  95. {
  96. oInsCmd.Dispose();
  97. oDa.Dispose();
  98. oTrn.Dispose();
  99. oCn.Dispose();
  100. }
  101. oCn.Close();
  102. //return oDs;
  103.  
  104. }
  105.  
Is there a easy way to do this ? Or do we have to loop around the dataset
and insert one by one?
Jul 21 '05 #1
1 2194
Yog,

Did you try it with a fillschema?

http://msdn.microsoft.com/library/de...chematopic.asp

In my opinion is the newsgroup

microsoft.public.dotnet.framework.adonet

A much better newsgroup for this kind of questions by the way.

Cor
Jul 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
25
by: Neo Geshel | last post by:
This works: <form> <asp:TextBox id="name" /> <%= name.ClientID %> </form> But this DOES NOT work: <form>
1
by: Yog | last post by:
I have a datagrid binded to a dataset and once the user updates the grid my dataset is reflected. When i wanted to save the data to tables, however i needed to insert this data into another...
4
by: Ed L. | last post by:
I think I'm seeing table-level lock contention in the following function when I have many different concurrent callers, each with mutually distinct values for $1. Is there a way to reimplement...
1
by: Joaquin | last post by:
OK, I hate myself for asking this, it's the worst question I have ever asked but it's worse not to ask: ¿How do I update a datatable in vs 2005? I mean, I am using a typed dataset, a...
1
by: abhi81 | last post by:
Hello All, I have a table on which I have created a insert,Update and a Delete trigger. All these triggers write a entry to another audit table with the unique key for each table and the timestamp....
2
by: technocraze | last post by:
Hi guys, I have encountered this error when updating the values to the MS Acess table. Error : Update on linked table failed. ODBC sql server error Timeout expired. MS Acess is my front end and...
3
by: Dilruba | last post by:
asp, vbscript, Ms Access I am using vbscript to insert & update ms accees. Insert operation is working , but update operation is not working. I have used the following codes: ...
0
by: magnolia | last post by:
i created a trigger that will record the changes made to a table .everything works fine except the insert query.whenerever i try to insert a record it fires insert and update triger at the same time...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.