I have a problem with my code in deleting of a record in datagridview and database(see attatchment)...for some reason I cant delete the data in my database...heres the code:
private void btnDelete_Click(object sender, EventArgs e)
{
int selectedindex=dataGridView1.CurrentCell.RowIndex;
string deleted;
try
{
cnn.Open();
if (MessageBox.Show("Are you sure you want to delete this feature?", "Confirm delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
dataGridView1.Rows.RemoveAt(selectedindex);
deleted = dataGridView1.Rows[selectedindex].Cells["MovieID"].Value.ToString();
cmd3=new SqlCommand("ALTER TABLE dbo.MovieCopy DROP CONSTRAINT FK_MovieCopy_Movies",cnn);
cmd1=new SqlCommand("Delete From Movies where movieid=@movieid",cnn);
cmd2 = new SqlCommand("Delete From MovieCopy where movieid=@movieid", cnn);
cmd1.Parameters.AddWithValue("@movieid", deleted);
cmd2.Parameters.AddWithValue("@movieid", deleted);
cmd1.ExecuteNonQuery();
cmd2.ExecuteNonQuery();
dataGridView1.Refresh();
cmd4 = new SqlCommand("ALTER TABLE dbo.MovieCopy ADD CONSTRAINT FK_MovieCopy_Movies FOREIGN KEY (MovieId) REFERENCES dbo.Movies(MovieID) ON DELETE CASCADE", cnn);
}
}
catch (Exception ex)
{
MessageBox.Show("Input Error!" + ex.Message);
}
finally
{
cnn.Close();
}
}