【问题标题】:asp.net - sqldatasource - detailsview - use stored proc to insert recordasp.net - sqldatasource - detailsview - 使用存储过程插入记录
【发布时间】:2009-02-26 15:18:52
【问题描述】:

我正在尝试使用存储过程通过 detailsview 和 sqldatasource 插入记录。我收到以下错误:

过程或函数“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"));

请注意,如果我使用注释掉的内联语句,则插入可以工作。

我的存储过程如下所示:

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 
            firstname=@firstname, 
            lastname= @lastname, 
            active = @Active
        where 
            custid = @custid
    end
    /* SET NOCOUNT ON */
    RETURN

我不明白输入参数如何在 sqldatasource、detailsview 等之间进行交互。它如何与 insline 语句一起使用而不与存储过程一起使用? sql datasource 和 detailsview 在事件方面是如何工作的?谷歌搜索和打印书籍Professional asp.net 3.5 in c# and VB 帮助不大。

提前感谢您阅读我的问题。

【问题讨论】:

    标签: asp.net detailsview sqldatasource


    【解决方案1】:

    我想你可能错过了SqlDataSource1.InsertCommandType = CommandType.StoredProcedure

    【讨论】:

      【解决方案2】:

      快速查看后,似乎没有为插入参数分配默认值。试试 Parameter 类的构造函数...

      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>
      

      【讨论】:

        【解决方案3】:

        内联语句的工作方式与 sproc 类似,不同之处在于您使用嵌入在字符串中的 @param 语句创建字符串。

        你应该只需要在你的存储过程中拉入你的更新和插入语句。然后您必须创建参数链接。

        如果您想进行这种拖放操作,请将更新语句插入到向导中。最坏的情况是添加参数映射。所以,拖放在这里并没有真正提供太多。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-05-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多