菜鸟请教一个DataList的问题,先谢了~!
DELETE 语句与 COLUMN REFERENCE 约束 'FK__titleauth__au_id__0519C6AF' 冲突。该冲突发生于数据库 'pubs',表 'titleauthor', column 'au_id'。语句已终止。
请问这个错误该怎么处理?
我的代码如下:
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
BindDataList();
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dlstAuthors.CancelCommand += new System.Web.UI.WebControls.DataListCommandEventHandler(this.dlstAuthors_CancelCommand);
this.dlstAuthors.EditCommand += new System.Web.UI.WebControls.DataListCommandEventHandler(this.dlstAuthors_EditCommand);
this.dlstAuthors.UpdateCommand += new System.Web.UI.WebControls.DataListCommandEventHandler(this.dlstAuthors_UpdateCommand);
this.dlstAuthors.DeleteCommand += new System.Web.UI.WebControls.DataListCommandEventHandler(this.dlstAuthors_DeleteCommand);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void BindDataList()
{
SqlConnection conPubs;
SqlCommand cmdSelect;
SqlDataReader dtrAuthors;
conPubs = new SqlConnection("Server=LCH;UID=sa;PWD=123;Database=Pubs");
cmdSelect = new SqlCommand("Select au_id,au_lname,phone From Authors Order by au_lname",conPubs);
conPubs.Open();
dtrAuthors = cmdSelect.ExecuteReader();
dlstAuthors.DataSource = dtrAuthors;
dlstAuthors.DataBind();
dtrAuthors.Close();
conPubs.Close();
}
private void dlstAuthors_EditCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{
dlstAuthors.EditItemIndex = e.Item.ItemIndex;
BindDataList();
}
private void dlstAuthors_CancelCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{
dlstAuthors.EditItemIndex = -1;
BindDataList();
}
private void dlstAuthors_DeleteCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{
SqlConnection conPubs;
string strDelete;
SqlCommand cmdDelete;
string strAuthorID;
strAuthorID = dlstAuthors.DataKeys[(int)e.Item.ItemIndex].ToString();
conPubs = new SqlConnection("Server=LCH;UID=sa;PWD=123;Database=Pubs");
strDelete = "Delete From Authors Where au_id = @authorID";
cmdDelete = new SqlCommand(strDelete,conPubs);
cmdDelete.Parameters.Add("@authorID",strAuthorID);
conPubs.Open();
cmdDelete.ExecuteNonQuery();
conPubs.Close();
dlstAuthors.EditItemIndex = -1;
BindDataList();
}
private void dlstAuthors_UpdateCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{
SqlConnection conPubs;
string strUpdate;
SqlCommand cmdUpdate;
string strAuthorID;
strAuthorID = dlstAuthors.DataKeys[(int)e.Item.ItemIndex].ToString();
TextBox txtLastName = (TextBox)e.Item.FindControl("txtLastName");
TextBox txtPhone = (TextBox)e.Item.FindControl("txtPhone");
conPubs = new SqlConnection("Server=LCH;UID=sa;PWD=123;Database=Pubs");
strUpdate = "Update authors Set au_lname=@lastname,phone=@phone Where au_id=@authorID";
cmdUpdate = new SqlCommand(strUpdate,conPubs);
cmdUpdate.Parameters.Add("@authorID",strAuthorID);
cmdUpdate.Parameters.Add("@lastname",txtLastName.Text);
cmdUpdate.Parameters.Add("@phone",txtPhone.Text);
conPubs.Open();
cmdUpdate.ExecuteNonQuery();
conPubs.Close();
dlstAuthors.EditItemIndex = -1;
BindDataList();
}
编译能够运行,并且更新和取消功能都有,但是我删除页面就出现以上的那个错误。错误行数是删除代码里的:cmdDelete.ExecuteNonQuery();请各位大侠帮忙。