When I tried to delete row from datagridview refer to table Famille, I get this error is :
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll Additional information: The DELETE statement conflicted with the REFERENCE constraint "FK_NatureCharge_Famille". The conflict occurred in database "Tresorerie", table "dbo.NatureCharge", column 'IdFam'. The statement has been terminated.
Table Famille
IdFam int
NomFam varchar(50)
NomFam varchar(50)
Table NatureCharge
IdNat int
NomNat varchar(50)
NomNat varchar(50)
IdFam int
By the way, the Famille part can delete multiple row in datagridview. I tried to add delete the NaturCharge 1st (it begins with comment delete Nature)
The code in the rar file :
The code in the rar file :
an error shows with line (it is for Famille part) `cmd.ExecuteNonQuery();``
Additional information: The DELETE statement conflicted with the REFERENCE constraint "FK_NatureCharge_Famille". The conflict occurred in database "Tresorerie", table "dbo.NatureCharge", column 'IdFam'.
It looks The DELETE statement doesn't seem to be correct
and the same error...
Francis SusaimichaelPosted Nov 4, 2015, 4:25 AM
Francis SusaimichaelPosted Nov 4, 2015, 7:17 AM
Med AminPosted Nov 4, 2015, 6:02 AM
Francis SusaimichaelPosted Nov 4, 2015, 5:58 AM
Med AminPosted Nov 4, 2015, 4:59 AM
foreach (DataGridViewRow row in selectedRows)
{
int IdFamDeletedValue = Convert.ToInt32(row.Cells["IdFam"].Value);
conn = new SqlConnection(connstring);
conn.Open();
comm = new SqlCommand("DELETE FROM NAtureCharge WHERE IdFam ='" + IdFamDeletedValue + "'", conn);
try
{
comm.ExecuteNonQuery();
//MessageBox.Show("Deleted...");
}
catch (Exception)
{
MessageBox.Show("Not Deleted");
}
finally
{
conn.Close();
}
}