使用存储过程插入DB

时间:2011-12-22 03:22:15

标签: asp.net

我有一个FormView,它的默认模式设置为“insert”。表单上有一堆用户输入数据的控件。按下插入按钮后,出现此错误:过程或函数sp_fc_vm_insertDebtorContact指定了太多参数

我很确定存储过程是正确的,所以我想知道它是否是我的代码。这是SqlDataSource代码:

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AuditDevConnectionString2 %>"            

        InsertCommand="sp_fc_vm_insertDebtorContact" 
        InsertCommandType="StoredProcedure" 
        SelectCommand="sp_fc_vm_insertDebtorContact" 
        SelectCommandType="StoredProcedure" >
        <InsertParameters>
            <asp:QueryStringParameter DefaultValue="0" Name="DebtorKey" 
            QueryStringField="DebtorKey" Type="Int32" />
            <asp:Parameter Name="LastName" />
            <asp:Parameter Name="FirstName" />
            <asp:Parameter Name="Title" />
            <asp:Parameter Name="Authority" DefaultValue="0" />
            <asp:Parameter Name="Extension" />
            <asp:Parameter Name="Email" />
            <asp:Parameter Name="Comments" />
            <asp:Parameter Name="Phone" />
            <asp:Parameter Name="Attention" DefaultValue="0" />
            <asp:Parameter Name="Fax" />
            <asp:Parameter Name="Ownership" DefaultValue="0" />
            <asp:Parameter Name="Guarantor" DefaultValue="0" />
            <asp:Parameter Name="Notices" DefaultValue="0" />
            <asp:Parameter Name="Addr1" />
            <asp:Parameter Name="Addr2" />
            <asp:Parameter Name="City" />
            <asp:Parameter Name="State" />
            <asp:Parameter Name="ZipCode" />
            <asp:Parameter Name="CellPhone" />
            <asp:Parameter Name="Country" />
        </InsertParameters>
    </asp:SqlDataSource>

这是存储过程 -

ALTER proc [dbo].[sp_fc_vm_insertDebtorContact] (@DebtorKey int,  @LastName varchar(50), 
      @FirstName varchar(20), @Title nvarchar(30), @Authority bit, @Extension nvarchar(10), @Email nvarchar(50),
      @Comments ntext, @Phone nvarchar(20), @Attention bit, @Fax nvarchar(20), @Ownership money, @Guarantor bit,
      @Notices bit, @Addr1 nvarchar(30), @Addr2 nvarchar(30), @City nvarchar(30), @State nvarchar(30),  
      @ZipCode nvarchar(10), @CellPhone varchar(20), @Country nvarchar(30))

AS

INSERT INTO [FSDev].dbo.Contacts (ClientKey, DebtorKey, lastname, NameKey, firstname, title, authority, extention,
Email, Notes, Phone, attention, Fax, brokerskey, inactive, [ownership], guarantor, notices, addr1, addr2,
city, [state], zipcode, cellphone, country)

VALUES (0, @DebtorKey, @LastName, LEFT(UPPER(@FirstName)+UPPER(@LastName),20), @FirstName, @Title, @Authority,
@Extension, @Email, @Comments, @Phone, @Attention, @Fax, 0, 0, @Ownership, @Guarantor, 0, @Addr1, @Addr2,
@City, @State, @ZipCode, @CellPhone, @country)

存储过程在SQL中运行良好,所以我猜我做错了。

1 个答案:

答案 0 :(得分:0)

你可以这样做

 private void InsertCommand(Object sender, SqlDataSourceCommandEventArgs e)
{

    SqlParameterCollection _colSqlParameter = new SqlParameterCollection();
    _colSqlParameter.Add("LastName", SqlDbType.VarChar).Value = "Last Name Valye";
    _colSqlParameter.Add("LastName", SqlDbType.VarChar).Value = "First Name Value";
    _colSqlParameter.Add("Title", SqlDbType.VarChar).Value = "Title Value";
    _colSqlParameter.Add("Extension", SqlDbType.VarChar).Value = "Extension Value";
    e.Command.Parameters.Add(_colSqlParameter);
}

<asp:SqlDataSource
    id="SQLFORINSERTDEMO"
    runat="server"
    ConnectionString=""
    InsertCommandType = "StoredProcedure"
    InsertCommand="SP_INSERT"        
    OnInserting="InsertCommand"
  </asp:SqlDataSource>