statement conflicted with the REFERENCE constraint
BUTTON CLICK CODE
bindingSource1.EndEdit();
try
{
// Delete all the Channels for the current Folder.
int folderID = (int)RemoveFolderBox.SelectedValue;
deleteChannels(folderID);
开发者_如何学C// Delete the folder itself.
RSSDataSet1.FolderRow folder = rSSDataSet1.Folder.FindByFolderID(folderID);
folder.Delete();
int rowsAffected = folderTableAdapter.Update(rSSDataSet1);
if (rowsAffected > 0)
{
//statusLabel.Text = "Folder successfully deleted.";
}
}
catch (Exception ex)
{
MessageBox.Show("Problem when deleting folder:" + ex.Message);
}
deleteChannels-function:
private void deleteChannels(int folderID)
{
try
{
RSSDataSet1.ChannelRow[] channels = (RSSDataSet1.ChannelRow[])rSSDataSet1.Channel.Select("FolderID = " + folderID.ToString());
foreach (RSSDataSet1.ChannelRow channel in channels)
{
int channelID = channel.ChannelID;
channel.Delete();
}
int rowsAffected = channelTableAdapter1.Update(rSSDataSet1);
}
catch (Exception ex)
{
throw ex;
}
}
I'm getting
The DELETE statement conflicted with the REFERENCE constraint "FK_Channel_Folder". The conflict occurred in database "C:\USERS\ADMIN\DOCUMENTS\VISUAL STUDIO 2010\PROJECTS\PROJ RSS\PROJ RSS\BIN\DEBUG\RSS.MDF", table "dbo.Channel", column 'FolderID'. The statement has been terminated.
You have a foreign key reference that would be violated if the item you're attempting to remove was deleted. It looks like Channel
has Folder
entities that depend on it.
You will need to delete any dependent Folder
entities before removing the Channel
, or set up cascading deletes.
精彩评论