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
Madhanmohan DevarajanPosted Oct 27, 2016, 12:07 PM
Roshan GuragainPosted Oct 27, 2016, 5:31 PM
{
int selectedIndex = BooksGrid.SelectedRows[0].Index;
// gets the RowID from the first column in the grid
int rowID = int.Parse(BooksGrid[0, selectedIndex].Value.ToString());
string sql = "DELETE FROM Table1 WHERE RowID = @RowID";
// your code for deleting it from the database
// then your code for refreshing the DataGridView
}
Satbr BrsatPosted Oct 27, 2016, 11:56 AM
Madhanmohan Devarajanthanks,
Madhanmohan DevarajanPosted Oct 27, 2016, 9:36 AM
Md GhousePosted Oct 27, 2016, 9:21 AM
Bikesh SrivastavaPosted Oct 27, 2016, 9:13 AM