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

Update & Insert in ADO.NET

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.