473,387 Members | 1,650 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,387 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 2201
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.