在 RadGrid 中单击添加时出错:启用约束失败。 1 行或多行包含违反约束的值
Error when click on Add in RadGrid: Failed to enable constraints. 1 or more rows contain values violating constraints
Whosebug 中有很多类似的问题,但 none 给出了答案..
当我点击 RadGrid 的 "Add New" 按钮时,出现以下错误:
Failed to enable constraints. One or more rows contain values
violating non-null, unique, or foreign-key constraints.
HTML代码:
<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 class 文件代码(BLL):
#region GET
public SDMDAL.SDM_Master_BUDataTable GetBU()
{
return Adapter.GetBU();
}
public DataTable GetAllRelationship()
{
DataTable dt = Adapter.GetRelationship();
return dt;
}
#endregion
数据库Table结构:
在这个 Table 中,RfoStatusID 是外键。
数据集Table适配器结构:(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 的添加新按钮时,添加面板都会打开并显示此日志记录异常。请让我知道我的代码有什么问题?
注意: 我是 Table 适配器方法的新手,也是第一次使用它。
请回复
我什至试图遵循这个 link:ASP.NET dataset getdataBy Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign key constraints
但这个错误仍然存在。请有人帮我解决它。
我创建了一个新的 TableAdapter 来绑定 DropDownList,它现在工作正常。以前我使用 1 个 TableAdapter 来绑定 RadGrid 以及这个 DropDownList,所以只有它显示错误。
Whosebug 中有很多类似的问题,但 none 给出了答案..
当我点击 RadGrid 的 "Add New" 按钮时,出现以下错误:
Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.
HTML代码:
<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 class 文件代码(BLL):
#region GET
public SDMDAL.SDM_Master_BUDataTable GetBU()
{
return Adapter.GetBU();
}
public DataTable GetAllRelationship()
{
DataTable dt = Adapter.GetRelationship();
return dt;
}
#endregion
数据库Table结构:
在这个 Table 中,RfoStatusID 是外键。
数据集Table适配器结构:(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 的添加新按钮时,添加面板都会打开并显示此日志记录异常。请让我知道我的代码有什么问题?
注意: 我是 Table 适配器方法的新手,也是第一次使用它。
请回复
我什至试图遵循这个 link:ASP.NET dataset getdataBy Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign key constraints 但这个错误仍然存在。请有人帮我解决它。
我创建了一个新的 TableAdapter 来绑定 DropDownList,它现在工作正常。以前我使用 1 个 TableAdapter 来绑定 RadGrid 以及这个 DropDownList,所以只有它显示错误。