2015-09-25 46 views
0

下面是我用來綁定RadGrid使用Stored Procedure的代碼。
我使用調用Stored Procedure.xsd文件 - TableAdapter方法錯誤:無法約束..;當在RadGrid中綁定記錄時

HTML代碼:

<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#代碼:BLL文件夾內

private DataTable _dtData = null; 

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

類文件:

#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 [email protected] 
and B.BfoStatusID=1 

END 

.xsd文件 - 表適配器

enter image description here

每當我跑我的網頁,我得到以下錯誤:

Failed to unable constraints. One or more rows contain values violating non null, unique or foreign key constraints.

我認爲這個錯誤是因爲我在存儲過程中使用3個不同的表(具有它們自己的主鍵)綁定RadGrid中的數據,然後將此存儲過程調用到具有其自己的主鍵的其他表適配器中。但不知道我理解的理由是否正確。

請注意,當我在Sql查詢嚮導中運行Stored Proc時,它的工作正常,但是當我運行我的頁面時,出現以上錯誤。

但我必須使用3個表(如上所述)綁定RadGrid。

請讓我知道如何解決此錯誤。提前致謝。

回答

0

代替下面的代碼行:

_dtData = SDM.Invoice.GetInvoiceBySPID(_SPID); 

與:

_dtData = SDM.Billing.GetBillingBySPID(_SPID); 

以來,Invoice的TableAdapter /表已經在它的數據庫中的表和
主鍵我使用其他數據庫表,其具有它自己的主鍵,Invoice TableAdapter/Table因此它給了我錯誤。 問題已解決。

相關問題