ı want to update datagritview in c# . ı need to this codes.firt ı use edit and ı will edit after the termineting ı click save how can ı write ı use the access database
my last codes but can not work

urun = comboBox1.Text;
boyut = comboBox1.Text;
model = model1.Text;
sayi = sayi1.Text;
aciklama = aciklama1.Text;

baglan.Open();

veri = new OleDbCommand("UPDATE stok set urun='" + urun + "',model='" + model + "',sayi='" + sayi + "',boyut='" + boyut + "',aciklama='" + aciklama + "' where no= " + no + ";", baglan);


veri.ExecuteNonQuery();

baglan.Close();

Dani AI

Generated

— the fastest fix is to stop concatenating values into the SQL string and use a parameterized UPDATE. pointed you in the right direction: for Access (OleDb) use parameter placeholders and add parameters in the same order. The posted snippet also looks like it reads the same combobox twice, which would leave one column with the wrong value.

Example pattern (safe, minimal):

private void btnSave_Click(object sender, EventArgs e)
{
    string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Path\YourDb.accdb;";
    using (var conn = new OleDbConnection(connString))
    using (var cmd = conn.CreateCommand())
    {
        cmd.CommandText = "UPDATE [stok] SET [urun]=?, [model]=?, [sayi]=?, [boyut]=?, [aciklama]=? WHERE [no]=?";
        cmd.Parameters.AddWithValue("@p1", comboUrun.Text);
        cmd.Parameters.AddWithValue("@p2", txtModel.Text);
        cmd.Parameters.AddWithValue("@p3", int.Parse(txtSayi.Text));    // parse numeric columns
        cmd.Parameters.AddWithValue("@p4", comboBoyut.Text);
        cmd.Parameters.AddWithValue("@p5", txtAciklama.Text);
        cmd.Parameters.AddWithValue("@p6", int.Parse(txtNo.Text));
        conn.Open();
        int affected = cmd.ExecuteNonQuery();
    }

    // Refresh the grid: either re-fill your DataTable via the DataAdapter or replace the edited row's cells.
}

Quick notes and troubleshooting

  • OleDb uses positional ? parameters — order of AddWithValue must match the ? order.
  • Use using so connections always close. Convert numeric columns with int.Parse/TryParse or pass DBNull.Value when appropriate.
  • If you get "syntax error" or "missing operator", wrap names in square brackets (see example) — Access reserves some words.
  • If the provider error appears on deployment, check x86/x64 ACE driver mismatch (compile to x86 or install the correct ACE).
  • If ExecuteNonQuery returns 0, verify the WHERE key value exists.

Further reading: parameter rules for OleDb and parameterized queries () and using a DataAdapter to persist edits from a DataTable ().

Recommended Answers

All 2 Replies

Hi,
you want to update what? Database or datagridview?
This code looks like more to be use to update database, just that UPDATE statement isnt sufficient enough).
Is has to be like: "UPDATE TableName SET Field1 = @field1, Field2 = @field2 WHERE ColditionColumn = @field3"

Am I right?

ok but ı coudnt undertand clearly can you write for all of codes please

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.