2009-02-26 49 views
1

我想使用存儲過程插入使用detailsview和sqldatasource的記錄。我收到以下錯誤:asp.net - sqldatasource - detailsview - 使用存儲過程插入記錄

過程或函數'CustRec_iu'需要參數'@firstname',它沒有提供。

我DetailsView的定義如下:

<asp:DetailsView ID="dvCustDetails1" runat="server" AutoGenerateRows="False" 
    DataSourceID="SqlDataSource1" Height="149px" Width="469px"> 
      <Fields> 
       <asp:BoundField DataField="FirstName" HeaderText="First Name" 
        SortExpression="FirstName" /> 
       <asp:BoundField DataField="LastName" HeaderText="Last Name" 
        SortExpression="LastName" /> 
       <asp:CommandField ButtonType="Button" ShowInsertButton="True" /> 
      </Fields> 
</asp:DetailsView> 

在後面的代碼,pageLoad的如下所示:

SqlDataSource1.ConnectionString = Connection.ConnectionStr; 

    //SqlDataSource1.InsertCommand = "INSERT INTO [customer] (firstname,lastname,active) values(@firstname,@lastname,@active)"; 
    SqlDataSource1.SelectCommand = "select firstname,lastname from customer"; 

    //SqlDataSource1.SelectCommand = "CustRec"; 
    SqlDataSource1.InsertCommand = "CustRec_iu"; 

    SqlDataSource1.InsertParameters.Clear(); 

    SqlDataSource1.InsertParameters.Add(new Parameter("firstname", DbType.String)); 
    SqlDataSource1.InsertParameters.Add(new Parameter("LastName", DbType.String)); 
    SqlDataSource1.InsertParameters.Add(new Parameter("Active",DbType.Int16,"1")); 

注意,如果我用註釋掉inline語句然後在插入的作品。

我的存儲過程如下所示:

ALTER PROCEDURE dbo.CustRec_iu 
    (
    @custid int = null, 
    @firstname varchar(100), 
    @lastname varchar(100), 
    @Active bit = 1 
    ) 

AS 
    if (isnull(@custid,0) = 0) 
    begin 
     insert into customer 
      (firstname,lastname,Active) 
     values 
      (@firstname,@lastname,@Active) 
    end 
    else 
    begin 
     update customer 
     set 
      [email protected], 
      lastname= @lastname, 
      active = @Active 
     where 
      custid = @custid 
    end 
    /* SET NOCOUNT ON */ 
    RETURN 

我不明白輸入參數之間的SqlDataSource互動如何,DetailsView的等等它是如何與insline聲明合作,而不是與存儲過程的工作? sql數據源和detailsview如何在事件方面工作?谷歌搜索和打印專業的asp.net 3.5在c#和VB中沒有太大的幫助。

預先感謝您閱讀我的問題。

回答

2

我想你可能會丟失SqlDataSource1.InsertCommandType = CommandType.StoredProcedure

+0

這個工作...非常感謝你... – 2009-02-27 02:44:38

0

快速查看後,它看起來像一個默認值沒有分配給插入參數。嘗試參數類的構造函數...

New Parameter(name, dbType, defaultValue) 

此外,您可能不需要在後面的代碼中執行sqldatasource。你可以嘗試以下。

<asp:DetailsView DataSourceID="SqlDataSource1" Height="149px" Width="469px"> 
    <Fields> 
     <asp:BoundField DataField="FirstName" HeaderText="First Name" SortExpression="FirstName" /> 
     <asp:BoundField DataField="LastName" HeaderText="Last Name" SortExpression="LastName" /> 
     <asp:CommandField ButtonType="Button" ShowInsertButton="True" /> 
    </Fields> 
</asp:DetailsView> 
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:YourConnectionString %>" 
    InsertCommand="CustRec_iu" 
    InsertCommandType="StoredProcedure" 
    SelectCommand="select firstname,lastname from customer" 
    SelectCommandType="Text"> 
     <InsertParameters> 
      <asp:Parameter Name="firstname" Type="String" /> 
      <asp:Parameter Name="LastName" Type="String" /> 
      <asp:Parameter Name="Active" Type="String" DefaultValue="1" /> 
     </InsertParameters> 
</asp:SqlDataSource> 
0

在線statesments工作就像存儲過程,除非你創建嵌入字符串中的@參數語句的字符串。

你只需要在你的sproc中插入你的更新和插入語句即可。然後您必須創建參數鏈接。

如果您想要進行此拖放,請將更新語句插入嚮導。最糟糕的情況是添加參數映射。所以,這裏的拖放並沒有真正提供。

相關問題