Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdating SQL table with the contents of a datatable
    primarykey
    data
    text
    <p>I am creating an application in WPF using C# where users populate a datagrid and the information is then stored in a DataTable called smb1. The following code works for inserting the data into the SQL database but when I modify the code for updating it does not work. Does anyone know how I can modify my code to allow Updates as there are no errors thrown when I run my application in Visual Studio. I must add that the Equipment column cannot be edited in the datagrid so the returned data is the same as the data taken from the database so that the updated rows will be matched to the equipment rows in the SQL database using the WHERE clause. Below is the original insertion code plus my attempt for updating the database.</p> <p><strong>Insert Code</strong></p> <pre><code>SqlConnection con = new SqlConnection(MyConnectionString); string SqlCmdText = "Insert into SHIFTLOG Values(@EQUIPMENT,@BATCHNO,@PRODUCTNO,@STATUS,@DATE,@PERIOD,@MACHINE)"; SqlCommand sc = new SqlCommand(SqlCmdText, con); con.Open(); foreach (DataRow row in smb1.Rows) { sc.Parameters.Clear(); sc.Parameters.AddWithValue("@EQUIPMENT", row["EQUIPMENT"]); sc.Parameters.AddWithValue("@BATCHNO", row["BATCHNO"]); sc.Parameters.AddWithValue("@PRODUCTNO", row["PRODUCTNO"]); sc.Parameters.AddWithValue("@STATUS", row["STATUS"]); sc.Parameters.AddWithValue("@DATE", DateTime.Now.ToString("yyyy-MM-dd")); sc.Parameters.AddWithValue("@PERIOD", DateTime.Now.ToString("tt")); sc.Parameters.AddWithValue("@MACHINE", "SMB1"); sc.ExecuteNonQuery(); } con.Close(); </code></pre> <p><strong>Attempt for Update Code</strong></p> <pre><code>SqlConnection con = new SqlConnection(MyConnectionString); string SqlCmdText = "UPDATE SHIFTLOG SET EQUIPMENT='@EQUIPMENT',BATCHNO='@BATCHNO',PRODUCTNO='@PRODUCTNO',STATUS='@STATUS',DATE='2013-09-12',PERIOD='@PERIOD',MACHINE='@MACHINE' WHERE EQUIPMENT='@EQUIPMENT'"; SqlCommand sc = new SqlCommand(SqlCmdText, con); con.Open(); foreach (DataRow row in smb1.Rows) { sc.Parameters.Clear(); sc.Parameters.AddWithValue("@EQUIPMENT", row["EQUIPMENT"]); sc.Parameters.AddWithValue("@BATCHNO", row["BATCHNO"]); sc.Parameters.AddWithValue("@PRODUCTNO", row["PRODUCTNO"]); sc.Parameters.AddWithValue("@STATUS", row["STATUS"]); sc.Parameters.AddWithValue("@PERIOD", DateTime.Now.ToString("tt")); sc.Parameters.AddWithValue("@MACHINE", row["MACHINE"]); sc.ExecuteNonQuery(); } con.Close(); </code></pre> <p>Thanks for any help.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload