Satbr Brsat

Satbr Brsat

  • NA
  • 6
  • 1.1k

How to delete rows in a datagridview and database

Oct 27 2016 8:14 AM
I am developing an application in C # with access to a database created in MS Access 2007. I have a WindowsForm to delete records from the database, but I'm only able to delete rows in the datagriedview (except the first) and the database is not being updated.

I get this error when I try to delete a row:

System.Data.OleDbException (0x80040E10): There was provided no value to one or more required parameters.
 
Here is my code:
 
private void btn_remover_Click(object sender, EventArgs e) { using (OleDbConnection oleConn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Stock_Material.accdb")) { try {                    oleConn.Open(); if (MessageBox.Show("Tem a certeza que pretende remover este registo(s)", " Confirmação", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == System.Windows.Forms.DialogResult.Yes) foreach (DataGridViewRow row in dataGridView_remover.SelectedRows) { int rowId = Convert.ToInt32(row.Cells[0].Value); if (rowId > 0) {                               dataGridView_remover.Rows.RemoveAt(row.Index); OleDbCommand delcmd = new OleDbCommand("Delete from product where id_Produto= " + rowId + "");                            delcmd.Connection = oleConn;                            delcmd.ExecuteNonQuery(); } } } catch (Exception ex ) { MessageBox.Show(ex.ToString()); }                oleConn.Close(); } }

How can I solve these problems:

  • The database is not updated when i delete a record in the datagriedview.
  • Don't let me delete the first line in datagriedview

Thanks in advance


Answers (6)