If you want to update
username
based on
id
, you're going into right direction. Small improvement of your code should help.
if (bool.Parse(item.Cells[0].Value))
{
using (SqlCommand command1 = new SqlCommand("UPDATE indebtedness SET username=@username WHERE id=@id", con))
{
command1.Parameters.AddWithValue("@username", username.Text);
command1.Parameters.AddWithValue("@id", item.Cells[1].Value);
command1.ExecuteNonQuery();
}
}
[EDIT]
In case you don't have id's, you're in trouble! Imagine, you've got the following record set:
RowNo: Selected Name
0: true Maciej
1: false Maicej
2: true Macije
3: false Maciej
4: true Maciek
And in a textbox, you've typed: "
Maciek
"
You can use an update statement like this:
using (SqlCommand command1 = new SqlCommand("UPDATE indebtedness SET username=@username WHERE usename=@prevname", con))
{
command1.Parameters.AddWithValue("@username", username.Text);
command1.Parameters.AddWithValue("@prevname", item.Cells[1].Value);
command1.ExecuteNonQuery();
}
After executing above code, the names would changed into:
RowNo: Selected Name
0: true Maciej => Maciek
1: false Maicej
2: true Macije => Maciek
3: false Maciej
4: true Maciek => Maciek
Note: it's not quite understandable what you're trying to achieve and based n what criteria.