Sql Parameter Collection的索引无效

时间:2018-04-01 08:37:27

标签: asp.net sql-server

在我的asp.net应用程序中,当我尝试使用以下函数插入时,出现错误

  

此SqlParameterCollection的索引36无效,Count = 36

查询我用于插入:

ALTER PROCEDURE [dbo].[Insert]
    @DocType VARCHAR (50),
    @DocDate DATETIME ,
    @DocNumber VARCHAR (50),
    @Description VARCHAR (100),
    @BatchID VARCHAR (50),
    @CustomerID VARCHAR (50),
    @CustomerName VARCHAR (50),
    @Address VARCHAR (100),
    @SalesPersonID VARCHAR (50),
    @TerritoryID VARCHAR (50),
    @CurrencyID VARCHAR (50),
    @PaymentTerms VARCHAR (50),
    @ShippingMethod VARCHAR (50),
    @TaxSchedualID VARCHAR (50),
    @PONumber VARCHAR (50),
    @Cost MONEY,
    @Sales MONEY,
    @TradeDiscount MONEY,
    @Freight MONEY,
    @Miscellaneous MONEY,
    @Tax MONEY,
    @Total MONEY,
    @Cash MONEY,
    @Check1 MONEY,
    @CreditCard MONEY,
    @TermsDiscTaken MONEY,
    @OnAccount MONEY,
    @UserCreated VARCHAR (50),
    @ProjectNumber VARCHAR (100),
    @PaymentType VARCHAR (100),
    @CheqDueDate Datetime,
    @CheqBookID VARCHAR (100),
    @CheqNumber VARCHAR (100),
    @CreditCardID VARCHAR (100),
    @DeliveryDate datetime,
    @PaymentNo varchar(50) = NULL
AS
BEGIN
    SET NOCOUNT ON;

    IF NOT EXISTS (SELECT * FROM MyTable WHERE DocNumber = @DocNumber)
    BEGIN
        INSERT INTO MyTable
        (
            Address,
            BatchID,
            Cash,
            Check1,
            Cost,
            CreditCard,
            CurrencyID,
            CustomerID,
            CustomerName,
            Description,
            DocDate,
            DocNumber,
            DocType,
            Freight,
            Miscellaneous,
            OnAccount,
            PaymentTerms,
            PONumber,
            Sales,
            SalesPersonID,
            ShippingMethod,
            Tax,
            TaxSchedualID,
            TermsDiscTaken,
            TerritoryID,
            Total,
            TradeDiscount,
            UserCreated,
            ProjectNumber,
            IsDeleted,
            PaymentType,
            CheqDueDate,
            CheqBookID,
            CheqNumber,
            CreditCardID,
            DeliveryDate,
            PaymentNo
        )
        VALUES
        (
            @Address,
            @BatchID,
            @Cash,
            @Check1,
            @Cost,
            @CreditCard,
            @CurrencyID,
            @CustomerID,
            @CustomerName,
            @Description,
            @DocDate,
            @DocNumber,
            @DocType,
            @Freight,
            @Miscellaneous,
            @OnAccount,
            @PaymentTerms,
            @PONumber,
            @Sales,
            @SalesPersonID,
            @ShippingMethod,
            @Tax,
            @TaxSchedualID,
            @TermsDiscTaken,
            @TerritoryID,
            @Total,
            @TradeDiscount,
            @UserCreated,
            @ProjectNumber,
            0,
            @PaymentType,
            @CheqDueDate,
            @CheqBookID,
            @CheqNumber,
            @CreditCardID,
            @DeliveryDate,
            @PaymentNo
        )

        SELECT SCOPE_IDENTITY()
    END
    ELSE
    BEGIN
        UPDATE MyTable
        SET     Address = @Address,
                BatchID = @BatchID,
                Cash = @Cash,
                Check1 = @Check1,
                Cost = @Cost,
                CreditCard = @CreditCard,
                CurrencyID = @CurrencyID,
                CustomerID = @CustomerID,
                CustomerName = @CustomerName,
                Description = @Description,
                DocDate = @DocDate,
                DocType = @DocType,
                Freight = @Freight,
                Miscellaneous = @Miscellaneous,
                OnAccount = @OnAccount,
                PaymentTerms = @PaymentTerms,
                PONumber = @PONumber,
                Sales = @Sales,
                SalesPersonID = @SalesPersonID,
                ShippingMethod = @ShippingMethod,
                Tax = @Tax,
                TaxSchedualID = @TaxSchedualID,
                TermsDiscTaken = @TermsDiscTaken,
                TerritoryID = @TerritoryID,
                Total = @Total,
                TradeDiscount = @TradeDiscount,
                UserCreated = @UserCreated,
                ProjectNumber = @ProjectNumber,
                PaymentType =@PaymentType,
                CheqDueDate = @CheqDueDate,
                CheqBookID =@CheqBookID,
                CheqNumber = @CheqNumber,
                CreditCardID = @CreditCardID,
                DeliveryDate = @DeliveryDate,
                PaymentNo = @PaymentNo
        WHERE DocNumber=@DocNumber

        SELECT ID FROM MyTableWHERE DocNumber = @DocNumber
    END
END

我的C#代码是:

public static int Insert(DOL.ReceivablesTrn obj)
{
        MSSQL DB = new MSSQL();
        DB.OpenConnection();

        DB.LoadSpParameters("InsertReceivablesTrn", obj.DocType, obj.DocDate, obj.DocNumber, obj.Description, obj.BatchID, obj.CustomerID,
            obj.CustomerName, obj.Address, obj.SalesPersonID, obj.TerritoryID, obj.CurrencyID, obj.PaymentTerms, obj.ShippingMethod,
            obj.TaxSchedualID, obj.PONumber, obj.Cost, obj.Sales, obj.TradeDiscount, obj.Freight, obj.Miscellaneous, obj.Tax,
            obj.Total, obj.Cash, obj.Check1, obj.CreditCard, obj.TermsDiscTaken, obj.OnAccount, obj.UserCreated,obj.ProjectNumber,
            obj.PaymentType,obj.CheqDueDate,obj.CheqBookID,obj.CheqNumber,obj.CreditCardID,obj.DeliveryDate,obj.PaymentNo);

        object res = DB.ExecuteValue();
        DB.CloseConnection();

        return Convert.ToInt32(res);
    }

如何解决这个问题?是因为参数数量不匹配吗?

1 个答案:

答案 0 :(得分:0)

您还没有告诉我们您提到的错误来自哪里,但我的心灵感应技巧告诉我它来自LoadSpParameters

您还没有提供您的个人课程MSSQL的源代码,但我的精神力量告诉我,其中的代码使用的是基于1的索引而不是基于零的索引。尝试将其从0循环到Count-1,或使用foreach而不是for循环。

还要指出你的MSSQL类(你还没有提供)封装了像SqlConnection这样的一次性东西,所以它应该实现IDisposable本身并在using块中使用。 / p>

现在有些算命......我预测你会接受这个作为正确的答案,并为我的惊人诊断能力提供支持。附:我不是真的有特殊的权力,但我真的认为这可能是正确的答案。快乐"愚人节'天"