Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

I am using the following code in "Save" Button of my windows application
of c#. I am using dataGridview with defined columns to enter the data.
I am using for loop of datagirdview to insert and update the data into table.
I am able to insert the data using the insert command
but after editing the records in datagridview and after modify the rows,
this following code updates all the records of the table with only last row of the datagridview and there is no error message. plz help to solve this.

thanks and regards

Madhu


con.Open();

foreach (DataGridViewRow row in dataGridView1.Rows)
{
cmd = new SqlCommand("UPDATE ORDER_DETL SET PROD_DESC = @b,quality =@c,sizecd=@d
where order_no = @a", con);
{

try
{
cmd.Parameters.AddWithValue("@a", textBox1.Text);
cmd.Parameters.AddWithValue("@b", row.Cells["prod_desc"].Value);
cmd.Parameters.AddWithValue("@c", row.Cells["quality"].Value);
cmd.Parameters.AddWithValue("@d", row.Cells["sizecd"].Value);
cmd.ExecuteNonQuery();

}

catch (Exception ex)
{

}

}
}

MessageBox.Show("record updated");

What I have tried:

I assumed that it is not navigating between the rows of grid I tried the following.
But it is not working.

int currentRow = dataGridView1.SelectedRows[0].Index;
if (currentRow < dataGridView1.RowCount)
{
dataGridView1.Rows[++currentRow].Selected = true;
}
Posted
Updated 31-Aug-18 8:38am
Comments
Eric Lynch 31-Aug-18 14:10pm    
You're hiding any exceptions by catching them without doing anything. Show a message box or log it. And, if there is an exception, share that exception, it might help to diagnose the problem. Also, ExecuteNonQuery returns the number of rows actually affected (in this case updated), you should share that as well to help diagnose the problem.
Member 13936784 1-Sep-18 3:27am    
Hi

After hiding try and catch blocks,
it throws the following exception,

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Additional information: Parameterized Query '(@a nvarchar(1),@b nvarchar(4000),@c nvarchar(4000),@d nvarchar(' expects parameter @b, which was not supplied.

plz suggest if u can understand what exactly is the problem.

regards

madhu

1 solution

SQL
where order_no = @a
C#
cmd.Parameters.AddWithValue("@a", textBox1.Text);

The text in textBox1 does not change between iterations of your loop. You are updating the same row every time.

At a guess, you should be pulling the value from the current row, as you are doing for the other parameters.
 
Share this answer
 
Comments
Eric Lynch 31-Aug-18 15:48pm    
Great catch and almost certainly the correct solution!

Though, proper exception handling and checking of return values, neither of which occur in the OP's code, is still good practice.

On the plus side, kudos to the OP for correctly using SQL parameters. Its nice to see that for a change :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900