Click here to Skip to main content
15,917,583 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to update the record what i check only bout i update all the table with my code i try to do that with this code bout i this it should be have something like ( where id=@id) how can i do that to make it update the check record only not the first id only this code update the first row ???

What I have tried:

C#
using (SqlConnection con = new SqlConnection("Connection"))
            {
                con.Open();
                

                foreach (DataGridViewRow item in dataGridView1.Rows)
            {
                if (bool.Parse(item.Cells[0].Value.ToString()))
                {
                        using (SqlCommand command1 = new SqlCommand("UPDATE indebtedness SET username=@username WHERE id=@id", con))
                        {
                            command1.Parameters.AddWithValue("@username", username.Text);
                            command1.Parameters.AddWithValue("@id", dataGridView1.Rows[0].Cells[15].Value);
                            command1.ExecuteNonQuery();
                        }
                
            
                }
                MessageBox.Show("Successfully ....");
            }
Posted
Updated 4-Feb-19 2:13am
v5

If you want to update username based on id, you're going into right direction. Small improvement of your code should help.

C#
//...
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); //read id from second column; change it to your needs                           
        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:
C#
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); //read previous name from second column
    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.
 
Share this answer
 
v2
Comments
RickZeeland 4-Feb-19 8:26am    
Exactly, was just about to suggest that, 5d !
Maciej Los 4-Feb-19 8:31am    
Thank you, Rick!
el_tot93 5-Feb-19 0:59am    
thank you so match for the help Maciej Los ..
Maciej Los 5-Feb-19 2:00am    
You're very welcome.
See examples here: SQL UPDATE Statement[^] e.g.
UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;

Of course in your code you should use parameterized queries:
using (SqlCommand command1 = new SqlCommand("UPDATE indebtedness SET username=@username WHERE id=@id", con))
{
  command1.Parameters.AddWithValue("@username", username.Text);
  command1.Parameters.AddWithValue("@id", userid.Text);
  command1.ExecuteNonQuery();
}

If you don't have an id field or Primary Key in your table, see article here: Create Primary Keys - SQL Server | Microsoft Docs[^]
 
Share this answer
 
v4
Comments
el_tot93 4-Feb-19 7:11am    
bout i don't know how to use WHERE in this code
RickZeeland 4-Feb-19 7:23am    
See updated solution :)
el_tot93 4-Feb-19 7:25am    
bro i don't have userid i have checkbox i want the update the record who have true check in checkbox
RickZeeland 4-Feb-19 7:37am    
Yo man, then your database design is not good, it is recommended to always have an ID field that is unique. I urge you to add it to your table !
el_tot93 4-Feb-19 8:00am    
see the update to the code the code update the first row only

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