Click here to Skip to main content
15,881,752 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Updating Date & time for all the Rows?

What I have tried:

Modify the comment. Deleted
C#
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.Columns[e.ColumnIndex].Name == "Submit")
{

using (OleDbConnection con1 = new OleDbConnection(con))
{
con1.Open();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
cmd = new OleDbCommand();
cmd.Connection = con1;
string query1 = "Update Spldetails set ID = @id,Status = @sts,Comment =@cmnt,Approvedhrs =@aphr,Reviewer = ('" + label5.Text + "'),Revieweddate = ('"+dateTimePicker3.Value+"') where ID =@id";
cmd.Parameters.AddWithValue("@id", dataGridView1.Rows[i].Cells[0].Value);
cmd.Parameters.AddWithValue("@sts", dataGridView1.Rows[i].Cells[11].Value);
cmd.Parameters.AddWithValue("@cmnt", dataGridView1.Rows[i].Cells[12].Value);
cmd.Parameters.AddWithValue("@aphr", dataGridView1.Rows[i].Cells[13].Value);
cmd.Parameters.AddWithValue("('" + label5.Text + "')", dataGridView1.Rows[i].Cells[14].Value);
cmd.Parameters.AddWithValue("('" + dateTimePicker3.Value + "')", dataGridView1.Rows[i].Cells[15].Value);

cmd.CommandText = query1;
cmd.ExecuteNonQuery();

//spldetailsBindingSource.EndEdit();
//spldetailsTableAdapter.Update(updatedata);

}
MessageBox.Show("Your Details have been Submitted successfully....!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

its working but i am updating for one row & it has updated Reviewer & revieweddate for all the rows.

can anyone please solve my problem sir?.
Posted
Updated 8-May-18 20:20pm
v2

If you are updating only one row. You should not have the for loop in the first place. It updates values on your database for all the rows on the GridView1.
Try removing the loop.
 
Share this answer
 
SQL does not do things without good reason: and your WHERE clause specifically limits the rows that are updated to only those where the ID column matches the given value.
Having said that, there are some things I don't like about this query:
1) You obviously know about parameterised queries, so why are you mixing them in with string concatenation? Just use parameterised queries throughout - it's a lot safer and it makes the query more readable.
2) Why are you doing this at all:
C#
cmd.Parameters.AddWithValue("('" + label5.Text + "')", dataGridView1.Rows[i].Cells[14].Value);
cmd.Parameters.AddWithValue("('" + dateTimePicker3.Value + "')", dataGridView1.Rows[i].Cells[15].Value);
Those aren't parameters at all!
3) Why are you setting the ID value to the value it already is? Since you only modify rows where the value is already equal to @ID, why set it to @ID again?

I'd start by finding out what - exactly - is in your label, and finding a way to pass that via a parameter: it's entirely possible that is what is causing the problem you have noticed. Use the debugger to make absolutely certain!
Get rid of the last two "parameters", clean up your query, and see what is in the label.
 
Share this answer
 

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