SQLException未处理,Dataset -Table Adapter

时间:2013-05-30 17:53:22

标签: vb.net vb.net-2010

我有以下存储过程,我从我的Windows窗体应用程序DataSet中调用它,如下所示:

  Me.TransactionTableAdapter.spPaymentApply(130, iAmount, Now)

虽然我提供了CustomerID,(并插入代码以查看它是否真的存在 - 并且确实存在),但在执行期间出现以下错误: 过程或函数'PaymentApply'需要参数'@CustomerID',这是未提供的。

这是我的SP:

USE [dbPB]
GO
/****** Object:  StoredProcedure [dbo].[PaymentApply]    Script Date: 05/30/2013 18:34:01 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[PaymentApply]
   @CustomerID int,
   @Amount int,
   @AsOfDate datetime
AS
WITH Totals AS (
   SELECT
      T.*,
      RunningTotal =
         Coalesce (
            (SELECT Sum(S.Buyin - Coalesce(S.CreditPaid, 0))
            FROM [Transaction] S
            WHERE
               T.CustomerID = S.CustomerID
               AND S.Type = 'Credit'
               AND S.Buyin > Coalesce(T.CreditPaid, 0)
               AND (
                  T.Starttime > S.Starttime
                  OR (
                     T.Starttime = S.Starttime
                     AND T.TransactionID > S.TransactionID
                  )
               )
            ),
        0)
   FROM
      [Transaction] T
   WHERE
      CustomerID = @CustomerID
      AND T.Type = 'Credit'
      AND T.Buyin > Coalesce(T.CreditPaid, 0)
)
UPDATE T
SET
   T.EndTime = P.EndTime,
   T.CreditPaid = Coalesce(T.CreditPaid, 0) + P.CreditPaid
FROM
   Totals T
   CROSS APPLY (
      SELECT TOP 1
         V.*
      FROM
         (VALUES
            (T.Buyin - Coalesce(T.CreditPaid, 0), @AsOfDate),
            (@Amount - RunningTotal, NULL)
         ) V (CreditPaid, EndTime)
      ORDER BY
         V.CreditPaid,
         V.EndTime DESC
   ) P
WHERE
   T.RunningTotal <= @Amount
   AND @Amount > 0;
;

VB代码:

Private Sub btnTransProcess_Click(sender As Object, e As EventArgs) Handles btnTransProcess.Click
        Dim iAmount As Integer
        Dim drv As DataRowView = CType(Me.cboCustomerName.SelectedItem, DataRowView)
        Dim SelCustId As Integer
        SelCustId = drv.Item("CustomerID")
        Try
            iAmount = CType(txtCreditPayment.Text, Integer)
        Catch ex As Exception
            MessageBox.Show("Enter a valid Credit Payment Amount!", "Invalid Amount", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        End Try
        MsgBox(SelCustId)
        Me.TransactionTableAdapter.spPaymentApply(130, iAmount, Now)
        '
        'Dim command As New SqlCommand(queryString, connection)
        'command.Parameters.AddWithValue("@CustomerID", 123)
    End Sub

2 个答案:

答案 0 :(得分:2)

发生了什么事?

您没有提供参数。

但是......我仔细检查了......

也许你没有以正确的方式称呼它? 你尝试过这样的事情:

  Dim queryString As String = "exec PaymentApply" 

    Using connection As New SqlConnection(ConnStrg)
        connection.Open()
        Dim command As New SqlCommand(queryString, connection)
        command.Parameters.AddWithValue("@CustomerID", 123)
        // ... same for other non-nullable parameters
        Dim reader As SqlDataReader = command.ExecuteReader()
    End Using

End Try

我为什么要这样做?

从代码中调用存储过程时,不提供@CustomerID int参数。

由于参数不是nullable,因此它需要一个值,并且在您给它之前不会再进一步​​。

如果您希望它可以为空,您可以为其设置默认值。这样,如果您不提供新值,他将采用此值。例如:

您可以在代码中传递如下参数:

someCommand.Parameters.AddWithValue("@CustomerID", 123)

如果您希望参数可以为空,请在SQL中执行类似的操作

@CustomerID int = 123 

答案 1 :(得分:1)

听起来你正在使用表适配器而不是命令对象。

见:Table AdapterTableAdapter Query Configuration Wizard

您对Me.TransactionTableAdapter.spPaymentApply(130, iAmount, Now)的调用看起来应该提供130作为CustomerID-但如果代码编译/工作不正确,也许您应该使用配置向导。

或者,考虑使用命令对象。这里概述了两者的用法:

How to: Create and Execute an SQL Statement that Returns No Value