OleDbException:没有给出一个或多个必需参数的值

时间:2012-01-11 22:22:25

标签: asp.net asmx

当前问题:在本地运行asmx文件以检查它是否正常工作时,不断出现的错误是没有给出值,如下所示。我现在已添加参数但错误仍然出现。

但是现在,我觉得错误不在asmx中,因为人们已经将SQL更改为正确运行。这是传递所需信息的aspx文件。是否可能存在传递已设置的会话变量的问题?

 Dim Secure As New Visa.VISAServices()
    Dim authHeader As New Visa.AuthenticationHeader()

    authHeader.username = "HSBC"
    authHeader.password = "password"
    Secure.AuthenticationHeaderValue = authHeader

    Session("sessionsortnum") = sortnum.Text
    Session("sessionaccount") = account.Text


    'Change type for the sortcode
    Dim sortcode As String = CType(Session.Item("sessionsortnum"), String)
    Dim accountnumber As String = CType(Session.Item("sessionaccount"), String)

    lblVisa.Text = Secure.AuthenticateCardSecure(pin.Text, sortnum.Text, account.Text)

    'NEEDS TO STAY HERE DO NOT MOVE!
    Session("sessioncase") = lblVisa.Text
    Dim selectcase As String = CType(Session.Item("sessioncase"), String)

    'Tests - Being sent to the session var
    testsort.Text = sortcode
    testbank.Text = accountnumber
    testselectc.Text = selectcase

这是从一个连接的服务传递会话的方式,并且sortcode,accountnumber和selectcase都正确显示。

这是调用asmx的文件:

Private Sub checkbalance(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim sortcode As String = CType(Session.Item("sessionsortnum"), String)
    Dim accountnumber As String = CType(Session.Item("sessionaccount"), String)
    Dim selectcase As String = CType(Session.Item("sessioncase"), String)
    testsort.Text = sortcode
    testbank.Text = accountnumber
    testselectc.Text = selectcase

    Select Case testselectc.Text
        Case "1111111111"
            'Dim lloydsbalance As New Lloyds.
            'screen.Text = lloydsbalance.overdraft(accountnumber)
        Case "2222222222"
            Dim barclaysbalance As New Barclays.Transactions()
            screen.Text = barclaysbalance.overdraft(accountnumber, sortcode)
        Case "3333333333"
            Dim santanderbalance As New Santander.Santander()
            screen.Text = santanderbalance.overdraft(accountnumber, sortcode)
Case "4444444444"
Dim HSBCbalance As New HSBC.HSBC()
screen.Text = HSBCbalance.OverDraft(accountnumber)

        Case "5555555555"
            Dim halifaxbalance As New Halifax.Halifax()
            screen.Text = halifaxbalance.overdraft(accountnumber)
        Case "6666666666"
            Dim natwestbalance As New Natwest.natwest()
            screen.Text = natwestbalance.overdraft(accountnumber, sortcode)

    End Select
End Sub

原始问题:每当我在本地运行asmx只是为了检查它是否正常工作时,我只是得到一个错误。我已经检查了我正在调用的函数,但是我找不到错误。我希望有人能发现我做错了什么。

错误:

  

System.Data.OleDb.OleDbException:没有给出一个或多个值   必需参数。在   System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult   hr)at   System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS   dbParams,Object& executeResult)at   System.Data.OleDb.OleDbCommand.ExecuteCommandText(对象&安培;   executeResult)at   System.Data.OleDb.OleDbCommand.ExecuteCommand(的CommandBehavior   行为,对象& executeResult)at   System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(的CommandBehavior   行为,String方法)at   System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior)   在HSBC.over(Int64账号)在HSBC.OverDraft(Int64   ACCOUNTNUMBER)

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'                                                   Overdraft Call
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

<WebMethod()> Public Function OverDraft(ByVal accountnumber As Long) As String
    Return over(accountnumber)
End Function

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'                                   Overdraft function
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

'Creates a service web method

Private Function over(ByVal accountnumber As Long) As String
    'Open the connection to the database

    Dim BalanceDr As OleDbDataReader
    Dim connectioncmd As OleDbCommand
    Dim connection As OleDbConnection
    Dim connx As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("../database/HSBc.mdb") & ";"
    Dim SQL As String = "SELECT Customer_Overdraft FROM Customer WHERE Account_Number=?"
    Dim overdraft As String
    connection = New OleDbConnection(connx)
    connectioncmd = New OleDbCommand(SQL, connection)
    connection.Open()
    'Create a DataReader that will return information.
    BalanceDr = _
    connectioncmd.ExecuteReader(CommandBehavior.CloseConnection)

    If BalanceDr.Read() Then
        'A row was returned
        overdraft = BalanceDr(0)
    Else
        'A row was not returned
        overdraft = "No Balance For Account Found"
    End If
    BalanceDr.Close()
    Return overdraft
End Function

accountnumber存储在一个会话中,该会话是从其他人创建的服务中返回的,我知道这是正确传递的,因为在任何页面上我都会检查它是否是正确的发送和使用。

有人有什么建议吗?我还为数据库添加了所有必需的权限。

2 个答案:

答案 0 :(得分:1)

您需要将命令参数设置为传入的帐号:

connectioncmd = New OleDbCommand(Sql, connection)
        Dim p As OleDb.OleDbParameter = connectioncmd.CreateParameter()
        p.Value = accoutnumber
        connectioncmd.Parameters.Add(p)

答案 1 :(得分:0)

非常接近;您的查询:

 "SELECT Customer_Overdraft FROM Customer WHERE Account_Number=?" 

是参数化查询;因此,您需要将OleDbParameter添加到命令对象中。

您的函数应该类似于(不在Visual Studio附近,因此无法进行语法检查):

Private Function over(ByVal accountnumber As Long) As String  

   'Open the connection to the database  

    Dim BalanceDr As OleDbDataReader  
    Dim connectioncmd As OleDbCommand  
    Dim connection As OleDbConnection  
    Dim connx As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("../database/HSBc.mdb") & ";"  
    Dim SQL As String = "SELECT Customer_Overdraft FROM Customer WHERE Account_Number=?"  
    Dim overdraft As String  
    connection = New OleDbConnection(connx)  
    connectioncmd = New OleDbCommand(SQL, connection) 
    'Add A Parameter!
    connectioncmd.Parameters.AddWithValue("accnumber", accountnumber)

    connection.Open()

    'Create a DataReader that will return information.  
    BalanceDr = _  
    connectioncmd.ExecuteReader(CommandBehavior.CloseConnection)  

    If BalanceDr.Read() Then  
        'A row was returned  
        overdraft = BalanceDr(0)  
    Else  
        'A row was not returned  
        overdraft = "No Balance For Account Found"  
    End If  
    BalanceDr.Close()  
    Return overdraft  
End Function  

我还建议你使用using语句。这一点尤其重要,因为您没有显式关闭数据库连接,而是依赖于将调用BalanceDr.Close()的事实。 “使用”将为您简化此操作。

您的功能也会类似:

Private Function over(ByVal accountnumber As Long) As String  

   'Open the connection to the database  

    Dim BalanceDr As OleDbDataReader  
    Dim connectioncmd As OleDbCommand  
    Dim connection As OleDbConnection  
    Dim connx As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("../database/HSBc.mdb") & ";"  
    Dim SQL As String = "SELECT Customer_Overdraft FROM Customer WHERE Account_Number=?"  
    Dim overdraft As String  

    Using connection = New OleDbConnection(connx)  
        Using connectioncmd = New OleDbCommand(SQL, connection) 
          'Add A Parameter!
          connectioncmd.Parameters.AddWithValue("accnumber", accountnumber)

          connection.Open()

          'Create a DataReader that will return information.  
          Using BalanceDr = connectioncmd.ExecuteReader()  

            If BalanceDr.Read() Then  
                'A row was returned  
                overdraft = BalanceDr(0)  
            Else  
                'A row was not returned  
                overdraft = "No Balance For Account Found"  
            End If

      End Using 

      End Using
    End Using

    Return overdraft

End Function