Updating database with dataTable (or dataSet) Hello, I need some help on the updating databse. I could use sqlCommandBuilder but because my query includes 2 tables I have to create my own code for update data back to database.  <div>The code below gets the values, depending on the sql query into dataTabe (or dataSet - can I ask here what is the difference between these two, what to use?) and then the vaules get changed through a textBox where user addes some values and new values are again the the dataTable (and dataSet too).  <div> <div>So from here on my goal is to put these new values back into the same place in the database, were they were before. I`ll need some help on this stage.</div> <div> <div style="color:Black;background-color:White"> <pre> DataSet dataSet = <span style="color:Blue">new</span> DataSet(); SqlDataAdapter dataAdapter = <span style="color:Blue">new</span> SqlDataAdapter(<span style="color:#A31515">&quot;SELECT IDComponent, CurrentCycle FROM Components, Aircrafts WHERE Aircrafts.IDAircraft = '&quot;</span> + IDAvijona + <span style="color:#A31515">&quot;' &quot;</span> + <span style="color:#A31515">&quot;AND Aircrafts.IDAircraft = Components.IDAircraftFK&quot;</span>, povezava); dataAdapter.Fill(dataSet, <span style="color:#A31515">&quot;Components&quot;</span>); DataTable dataTable = dataSet.Tables[<span style="color:#A31515">&quot;Components&quot;</span>]; <span style="color:Green">//dataSet.Tables[0].TableName = &quot;Cycles&quot;;</span> <span style="color:Blue">foreach</span> (DataRow dataRow <span style="color:Blue">in</span> dataSet.Tables[<span style="color:#A31515">&quot;Components&quot;</span>].Rows) { <span style="color:Blue">if</span> (dataRow[<span style="color:#A31515">&quot;CurrentCycle&quot;</span>].Equals(System.DBNull.Value)) { <span style="color:Green">//Če je v bazi vrednost NULL gre koda tukaj skozi - ne zgodi se nič!</span> dataRow[<span style="color:#A31515">&quot;CurrentCycle&quot;</span>] = DBNull.Value; } <span style="color:Blue">else</span> { dataRow[<span style="color:#A31515">&quot;CurrentCycle&quot;</span>] = Convert.ToInt32(dataRow[<span style="color:#A31515">&quot;CurrentCycle&quot;</span>]) + Convert.ToInt32(textBoxCycles.Text); } } <span style="color:Blue">for</span> (<span style="color:Blue">int</span> i = 0; i &lt; dataTable.Rows.Count; i++) { <span style="color:Blue">if</span> (dataTable.Columns[<span style="color:#A31515">&quot;CurrentCycle&quot;</span>] == <span style="color:Blue">null</span>) { } <span style="color:Blue">else</span> { DataRow dataRow2 = dataTable.Rows[i]; SqlCommand cmd2 = <span style="color:Blue">new</span> SqlCommand(<span style="color:#A31515">&quot;UPDATE Components SET CurrentCycle = @currentCycle WHERE IDComponent = @idComponent&quot;</span>, povezava); cmd2.Parameters.Add(<span style="color:Blue">new</span> System.Data.SqlClient.SqlParameter(<span style="color:#A31515">&quot;@currentCycle&quot;</span>, dataRow2[<span style="color:#A31515">&quot;CurrentCycle&quot;</span>])); SqlParameter param2 = cmd2.Parameters.Add(<span style="color:#A31515">&quot;@idComponent&quot;</span>, SqlDbType.Int); cmd2.Parameters[<span style="color:#A31515">&quot;@idComponent&quot;</span>].Value = (<span style="color:Blue">int</span>)dataRow2[<span style="color:#A31515">&quot;CurrentCycle&quot;</span>]; param2.SourceVersion = DataRowVersion.Current; dataAdapter.UpdateCommand = cmd2; dataAdapter.Update(dataSet, <span style="color:#A31515">&quot;Components&quot;</span>); } } </pre> </div> This is my code I did, but a code for updating into the database is not correct, I get an error: &quot;Concurrency violation: the UpdateCommand affected 0 of the expected 1 records&quot;.</div> <div>Can I ask someone for a bit of a help, please. I am trying to work this out for 3 days, but so far still didn`t find the way through.</div> <div><br/></div> <div>PS: I forgot to say that &quot;CurrentCycle&quot; is the changing value. In the UPDATE query I pulled out the IDComponent too (this is the component`s ID), which goes together with the CurrentCycle. So I though I need the ID to put the particular value back to the database, otherwise how can the program know where to put it? I am right?</div> <div>Sometimes I can get out the te database hundreds for CurrentCycle values, and they are not in a row ( I mean one has Id 23, another 24, 3rd 30, 4th 34, and so on...)</div> </div> </div> <hr class=sig> I am a rookie at C#, so please don`t be mad if my questions goona sound stupid...© 2009 Microsoft Corporation. All rights reserved.Wed, 17 Jun 2009 18:34:34 Z846c9147-fac0-4333-81a1-e754459c4e24http://social.msdn.microsoft.com/Forums/en-US/ssdsgetstarted/thread/846c9147-fac0-4333-81a1-e754459c4e24#846c9147-fac0-4333-81a1-e754459c4e24http://social.msdn.microsoft.com/Forums/en-US/ssdsgetstarted/thread/846c9147-fac0-4333-81a1-e754459c4e24#846c9147-fac0-4333-81a1-e754459c4e24Mitja Boncahttp://social.msdn.microsoft.com/Profile/en-US/?user=Mitja%20BoncaUpdating database with dataTable (or dataSet) Hello, I need some help on the updating databse. I could use sqlCommandBuilder but because my query includes 2 tables I have to create my own code for update data back to database.  <div>The code below gets the values, depending on the sql query into dataTabe (or dataSet - can I ask here what is the difference between these two, what to use?) and then the vaules get changed through a textBox where user addes some values and new values are again the the dataTable (and dataSet too).  <div> <div>So from here on my goal is to put these new values back into the same place in the database, were they were before. I`ll need some help on this stage.</div> <div> <div style="color:Black;background-color:White"> <pre> DataSet dataSet = <span style="color:Blue">new</span> DataSet(); SqlDataAdapter dataAdapter = <span style="color:Blue">new</span> SqlDataAdapter(<span style="color:#A31515">&quot;SELECT IDComponent, CurrentCycle FROM Components, Aircrafts WHERE Aircrafts.IDAircraft = '&quot;</span> + IDAvijona + <span style="color:#A31515">&quot;' &quot;</span> + <span style="color:#A31515">&quot;AND Aircrafts.IDAircraft = Components.IDAircraftFK&quot;</span>, povezava); dataAdapter.Fill(dataSet, <span style="color:#A31515">&quot;Components&quot;</span>); DataTable dataTable = dataSet.Tables[<span style="color:#A31515">&quot;Components&quot;</span>]; <span style="color:Green">//dataSet.Tables[0].TableName = &quot;Cycles&quot;;</span> <span style="color:Blue">foreach</span> (DataRow dataRow <span style="color:Blue">in</span> dataSet.Tables[<span style="color:#A31515">&quot;Components&quot;</span>].Rows) { <span style="color:Blue">if</span> (dataRow[<span style="color:#A31515">&quot;CurrentCycle&quot;</span>].Equals(System.DBNull.Value)) { <span style="color:Green">//Če je v bazi vrednost NULL gre koda tukaj skozi - ne zgodi se nič!</span> dataRow[<span style="color:#A31515">&quot;CurrentCycle&quot;</span>] = DBNull.Value; } <span style="color:Blue">else</span> { dataRow[<span style="color:#A31515">&quot;CurrentCycle&quot;</span>] = Convert.ToInt32(dataRow[<span style="color:#A31515">&quot;CurrentCycle&quot;</span>]) + Convert.ToInt32(textBoxCycles.Text); } } <span style="color:Blue">for</span> (<span style="color:Blue">int</span> i = 0; i &lt; dataTable.Rows.Count; i++) { <span style="color:Blue">if</span> (dataTable.Columns[<span style="color:#A31515">&quot;CurrentCycle&quot;</span>] == <span style="color:Blue">null</span>) { } <span style="color:Blue">else</span> { DataRow dataRow2 = dataTable.Rows[i]; SqlCommand cmd2 = <span style="color:Blue">new</span> SqlCommand(<span style="color:#A31515">&quot;UPDATE Components SET CurrentCycle = @currentCycle WHERE IDComponent = @idComponent&quot;</span>, povezava); cmd2.Parameters.Add(<span style="color:Blue">new</span> System.Data.SqlClient.SqlParameter(<span style="color:#A31515">&quot;@currentCycle&quot;</span>, dataRow2[<span style="color:#A31515">&quot;CurrentCycle&quot;</span>])); SqlParameter param2 = cmd2.Parameters.Add(<span style="color:#A31515">&quot;@idComponent&quot;</span>, SqlDbType.Int); cmd2.Parameters[<span style="color:#A31515">&quot;@idComponent&quot;</span>].Value = (<span style="color:Blue">int</span>)dataRow2[<span style="color:#A31515">&quot;CurrentCycle&quot;</span>]; param2.SourceVersion = DataRowVersion.Current; dataAdapter.UpdateCommand = cmd2; dataAdapter.Update(dataSet, <span style="color:#A31515">&quot;Components&quot;</span>); } } </pre> </div> This is my code I did, but a code for updating into the database is not correct, I get an error: &quot;Concurrency violation: the UpdateCommand affected 0 of the expected 1 records&quot;.</div> <div>Can I ask someone for a bit of a help, please. I am trying to work this out for 3 days, but so far still didn`t find the way through.</div> <div><br/></div> <div>PS: I forgot to say that &quot;CurrentCycle&quot; is the changing value. In the UPDATE query I pulled out the IDComponent too (this is the component`s ID), which goes together with the CurrentCycle. So I though I need the ID to put the particular value back to the database, otherwise how can the program know where to put it? I am right?</div> <div>Sometimes I can get out the te database hundreds for CurrentCycle values, and they are not in a row ( I mean one has Id 23, another 24, 3rd 30, 4th 34, and so on...)</div> </div> </div> <hr class=sig> I am a rookie at C#, so please don`t be mad if my questions goona sound stupid...Wed, 17 Jun 2009 08:18:04 Z2009-06-17T08:35:08Zhttp://social.msdn.microsoft.com/Forums/en-US/ssdsgetstarted/thread/846c9147-fac0-4333-81a1-e754459c4e24#e919fbd8-6f2c-4b02-b1b1-859f9c4a55fahttp://social.msdn.microsoft.com/Forums/en-US/ssdsgetstarted/thread/846c9147-fac0-4333-81a1-e754459c4e24#e919fbd8-6f2c-4b02-b1b1-859f9c4a55faMitja Boncahttp://social.msdn.microsoft.com/Profile/en-US/?user=Mitja%20BoncaUpdating database with dataTable (or dataSet) I got it: <div> <pre lang="x-c#"> for (int i = 0; i &lt; dataTable.Rows.Count; i++) { if (dataTable.Columns[&quot;CurrentCycle&quot;] == null) //HERE IS THE PROBLEM NOW! { continue; } else { DataRow dataRow2 = dataTable.Rows[i]; SqlCommand cmd2 = new SqlCommand(&quot;UPDATE Components SET CurrentCycle = @currentCycle WHERE IDComponent = @idComponent&quot;, povezava); cmd2.Parameters.Add(new System.Data.SqlClient.SqlParameter(&quot;@currentCycle&quot;, SqlDbType.Int)); //, (int)dataRow2[&quot;CurrentCycle&quot;])); cmd2.Parameters[&quot;@currentCycle&quot;].Value = (int)dataRow2[&quot;CurrentCycle&quot;]; SqlParameter param2 = cmd2.Parameters.Add(&quot;@idComponent&quot;, SqlDbType.Int); cmd2.Parameters[&quot;@idComponent&quot;].Value = (int)dataRow2[&quot;IDComponent&quot;]; //param2.SourceVersion = DataRowVersion.Current; dataAdapter.UpdateCommand = cmd2; dataAdapter.Update(dataSet, &quot;Components&quot;); } }</pre> I have only one problem. I need to specify if the row of a CurrentCycle is empty. If it is empty (there is no value) it must not go through the code else. </div> <div><br/></div> <div>OR maybe a better solution:</div> <div>How to get into  dataTable a NULL value if there is a null value in a database?</div> <div>So if there is a null value in a databse, dataTable will get a null value too, and with updating the null value will go again into database?</div> <div>This is the code I have while dataRow read database:</div> <div><br/></div> <div> <pre lang="x-c#"> foreach (DataRow dataRow in dataSet.Tables[&quot;Components&quot;].Rows) { if (dataRow[&quot;CurrentCycle&quot;].Equals(System.DBNull.Value)) { //The code to set the value in a dataTable to NULL!! }</pre> <br/></div><hr class="sig">I am a rookie at C#, so please don`t be mad if my questions goona sound stupid...Wed, 17 Jun 2009 09:59:52 Z2009-06-17T09:59:52Zhttp://social.msdn.microsoft.com/Forums/en-US/ssdsgetstarted/thread/846c9147-fac0-4333-81a1-e754459c4e24#a995f28e-0ed1-4b2c-914f-85eda0212b7ehttp://social.msdn.microsoft.com/Forums/en-US/ssdsgetstarted/thread/846c9147-fac0-4333-81a1-e754459c4e24#a995f28e-0ed1-4b2c-914f-85eda0212b7eKapil Muni Guptahttp://social.msdn.microsoft.com/Profile/en-US/?user=Kapil%20Muni%20GuptaUpdating database with dataTable (or dataSet) <p>This forum is for SQL Data Services, a Microsoft's Cloud database service offering. You will have a better chance of getting a fast answer in one of the <br/>.net(C#) specific forum. please go to forum home and raise it in ADO.Net forum <a href="http://social.msdn.microsoft.com/Forums/en-US/categories"><span style="color:#0033cc">http://social.msdn.microsoft.com/Forums/en-US/categories</span></a></p> <p>Thanks<br/>Kapil</p>Wed, 17 Jun 2009 12:35:45 Z2009-06-17T12:35:45Z