StackOverFlow中有许多类似的问题,但没有一个给出答案。
当我点击RadGrid的“添加新”按钮时,我得到以下错误:
无法启用约束。一行或多行包含值 违反非空,唯一或外键约束。
HTML code:
<telerik:GridTemplateColumn DataField="BURelationship" UniqueName="BURelationship" HeaderText="Relationship" SortExpression="BURelationship">
<ItemTemplate>
<asp:Label ID="lblRelationship" Text='<%# Eval("BURelationship") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlRelationship" runat="server" Width="150" />
</EditItemTemplate>
</telerik:GridTemplateColumn>
C#代码:
public DataTable _dtBU;
protected void rgBU_ItemDataBound(object sender, GridItemEventArgs e)
{
try
{
if (e.Item is GridEditableItem && e.Item.IsInEditMode)
{
_dtBU = SDM.BU.GetBU();
GridEditableItem item = e.Item as GridEditableItem;
DropDownList rlist = item.FindControl("ddlRelationship") as DropDownList;
rlist.DataTextField = "RName";
rlist.DataValueField = "RID";
rlist.DataSource = SDM.BU.GetAllRelationship();
rlist.DataBind();
foreach (DataRow dr in _dtBU.Rows)
{
if (dr["ID"].ToString() == item.GetDataKeyValue("ID").ToString())
{
rlist.SelectedValue = dr["BURelationship"].ToString();
break;
}
}
}
}
catch (Exception ex)
{
}
}
protected void rgBU_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
_dtBU = SDM.BU.GetBU();
rgBU.DataSource = _dtBU;
}
SDM.BU类文件代码(BLL):
#region GET
public SDMDAL.SDM_Master_BUDataTable GetBU()
{
return Adapter.GetBU();
}
public DataTable GetAllRelationship()
{
DataTable dt = Adapter.GetRelationship();
return dt;
}
#endregion
数据库表格结构:
DataSet表适配器结构:(DLL)
绑定DropDownList的存储过程是
ALTER PROCEDURE [dbo].[SDM_Select_Relationship]
AS
BEGIN
SET NOCOUNT ON;
SELECT distinct [RID], [RName]
FROM [SDM_DB].[dbo].[SDM_Master_Relationship](NOLOCK)
ORDER BY [RID]
END
请告诉我此错误的原因以及解决方法?我无法理解为什么会出现这个错误。每当我点击RadGrid的Add New按钮时,Add面板就会打开,并显示此Logging Exception。请告诉我我的代码有什么问题?
注意:我是表适配器方法的新手,第一次使用它。
请回复
我甚至试图关注此链接:ASP.NET dataset getdataBy Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign key constraints 但这个错误仍然是一样的。请有人帮我解决。