简体   繁体   中英

Update DataGridView C#

I need some help.I want to update a table in DataGridView . I'm using this code:

private void UpdateData()
{                 
    cmd = new SQLiteCommand("Update Test_Rack set TestRack = '" + cboTestRack.Text + "',Number = '" + comboBox1.Text + "' ,Line = '" + textBox3.Text + "' where id = '" + rid1 + "'", DBcon);

    if (DBcon.State == ConnectionState.Closed)
        DBcon.Open();

    cmd.ExecuteNonQuery();
    DBcon.Close();
    label3.Visible = true;
    label3.Text = "Update";
    IncarcaDatele(id);
    cboTestRack.Text = "";
    comboBox1.Text = "";
    textBox3.Text = "";   
}

The code is working but I have a problem:it update a field and the rest are empty.

在此处输入图片说明

You missed comma , after TestRack = '" + cboTestRack.Text + "' and before Line .

Also I strongly recommend that you always use parameterized queries to avoid SQL Injection like this:

cmd = new SQLiteCommand("Update Test_Rack set TestRack = @TestRack , ...");
cmd.Parameters.AddWithValue("@TestRack", cboTestRack.Text);

Although specify the type directly and use the Value property is more better than AddWithValue . Check this: Can we stop using AddWithValue() already?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM