错误:无法约束..;在RadGrid中绑定记录时

时间:2015-09-25 08:51:00

标签: c# sql asp.net stored-procedures tableadapter

以下是我用RadGrid绑定Stored Procedure的代码 我使用Stored Procedure文件调用.xsd - TableAdapter方法

HTML code:

<telerik:RadGrid ID="rgData" runat="server" ShowFooter="true"
OnNeedDataSource="rgData_NeedDataSource"
AllowSorting="True" AllowFilteringByColumn="True" ShowGroupPanel="True" GroupLoadMode="Client"
EnableEmbeddedSkins="False" ImagesPath="../App_Themes/MetroRed/Grid" Skin="MetroRed">
   <ClientSettings AllowDragToGroup="True"></ClientSettings>
   <GroupingSettings ShowUnGroupButton="false" CaseSensitive="false"></GroupingSettings>
   <MasterTableView ShowGroupFooter="true" CommandItemDisplay="None" AutoGenerateColumns="false" AllowMultiColumnSorting="true">
   <CommandItemSettings ShowAddNewRecordButton="false" />
    <Columns>
           <telerik:GridBoundColumn HeaderText="Billing ID" DataField="BillingID" SortExpression="BillingID" FilterDelay="2000" ShowFilterIcon="false">
       </telerik:GridBoundColumn>
       <telerik:GridBoundColumn HeaderText="Direct Cost" DataField="DCIDescription" SortExpression="DCIDescription" FilterDelay="2000" ShowFilterIcon="false">
       </telerik:GridBoundColumn>
           <telerik:GridBoundColumn HeaderText="Business Unit" DataField="BUName" SortExpression="BUName" FilterDelay="2000" ShowFilterIcon="false">
           </telerik:GridBoundColumn>
    </Columns>
   </MasterTableView>
</telerik:RadGrid>

C#代码:

private DataTable _dtData = null; 

protected void rgData_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        try
        {
            _dtData = SDM.Invoice.GetInvoiceBySPID(_SPID);
            rgData.DataSource = _dtData;
        }
        catch (Exception ex)
        {
        }
    }

BLL文件夹中的类文件:

#region Invoice
private static SDM_Invoice _Invoice = null;
public static SDM_Invoice Invoice
{
    get
    {
        if (_Invoice == null)
        _Invoice = new SDM_Invoice();

        return _Invoice;
    }
}
#endregion

private SDM_Tran_GenerateInvoiceTableAdapter _GenerateInvoiceTableAdapter = null;
protected SDM_Tran_GenerateInvoiceTableAdapter Adapter
{
    get
    {
        if (_GenerateInvoiceTableAdapter == null)
                _GenerateInvoiceTableAdapter = new SDM_Tran_GenerateInvoiceTableAdapter();

            return _GenerateInvoiceTableAdapter;
    }
} 

public SDMDAL.SDM_Tran_GenerateInvoiceDataTable GetInvoiceBySPID(string SPID)
{
    return Adapter.GetInvoiceBillingBySPID(SPID);
}

绑定RadGrid的存储过程:

ALTER PROCEDURE [dbo].[Select_InvoiceBillingBySPID]
    @SPID as nvarchar(50)
    AS
BEGIN

    SET NOCOUNT ON; 

SELECT 
--B.ID,
B.BillingID, 
DCIDescription,
(SELECT BUName FROM dbo.SDM_Master_BU WITH (NOLOCK) WHERE BUID=AfoBUID) as BUName

FROM SDM_Tran_Billing B WITH (NOLOCK),
dbo.SDM_Tran_DCI D WITH (NOLOCK), 
dbo.SDM_Tran_Allocation A WITH (NOLOCK)

WHERE B.BfoAllocationID=AllocationID 
and A.AfoDCIID=D.DCIID 
and D.DCIfoSPID=@SPID
and B.BfoStatusID=1

END

.xsd文件 - 表适配器

enter image description here

每当我运行我的网页时,我都会收到以下错误:

  

无法解决约束。一行或多行包含值   违反非空,唯一或外键约束。

我认为错误是因为我在存储过程中使用3个不同的表(具有自己的主键)绑定RadGrid中的数据,然后将此存储过程调用到具有其自己的主键的其他表适配器中。但不确定我理解的原因是否正确。

注意当我在Sql查询向导中运行Stored Proc时它工作正常,但是当我运行我的页面时,我遇到了错误。

但我必须使用3个表(如上所述)绑定RadGrid。

请告诉我如何解决此错误。提前致谢。

1 个答案:

答案 0 :(得分:0)

替换为以下代码行:

_dtData = SDM.Invoice.GetInvoiceBySPID(_SPID); 

使用:

_dtData = SDM.Billing.GetBillingBySPID(_SPID);

因为,Invoice TableAdapter / Table已经在其数据库表中拥有主键,而我正在使用其他具有自己主键的数据库表,其中Invoice TableAdapter / Table因此它给了我错误。 问题已解决。