简体   繁体   中英

Stored procedure error in vb.net

this is my function:

     Public Function getLocation(ByVal res As String, ByVal tblName As String) As DataTable
    Dim command As SqlCommand = New SqlCommand("jk_getLocation", cn)
    command.Parameters.AddWithValue("@id", res)
    command.Parameters.AddWithValue("@TableName", tblName)

    If resDT.Rows.Count <> 0 Then
        resDT.Rows.Clear()
    End If
    resDS.Clear()
    Dim da As New SqlDataAdapter(command)
    Try
        da.Fill(resDS, "nb")
        resDT = resDS.Tables(0)
    Catch exe As SqlException
        logFile("SP getLocation ----" + exe.Message)
    Catch ex As Exception
        logFile("SP getLocation ----" + ex.Message)
    End Try
    Return resDT
End Function

that's how i'm calling it:

resDT = nb.getLocation(res.ToString(), code)

that's my stored procedure:

ALTER procedure [dbo].[jk_getLocation]
@id varchar(100),
@TableName varchar(100)
as
begin
select * from b where id=@id and name=@TableName
end

when i execute my procedure from SQL it works fine and returns the correct data.. but when i run it from vb.net i keep getting Procedure or function 'jk_getLocation' expects parameter '@id', which was not supplied. although i'm passing all the parameters

You forgot to set the command type to CommandType.StoredProcedure

That said you may be wondering why you're getting the error

Procedure or function 'jk_getLocation' expects parameter '@id', which was not supplied

If you run SQL Profiler and execute your method as it currently is you'll see something like the following line

exec sp_executesql N'jk_getLocation',
            N'@id nvarchar(3),@TableName nvarchar(6)',
              @id=N'100',@TableName=N'foobar'

This is what SQL server executes when you use a CommandText SqlCommand with parameters (most often used with parameterized queries)

If you execute the above in SSMS you'll get the same error

You can fix this by either by adding

command.CommandType = CommandType.StoredProcedure

or by changing your command text to include the parameter names

Dim command As SqlCommand = New SqlCommand("jk_getLocation @id, @tablename", cn)

which would be executed as

exec sp_executesql N'jk_getLocation @id, @tablename', //Note the parameters declared here
            N'@id nvarchar(3),@TableName nvarchar(6)',
              @id=N'100',@TableName=N'foobar'

Try this:

Public Sub getLocation(resDT As DataTable, params As Hashtable)
    Dim command As SqlCommand = New SqlCommand

    command.Connection = cn
    command.CommandType = CommandType.StoredProcedure
    command.CommandText = "jk_getLocation"
    command.CommandTimeout = 10

    For i = 0 To ht.Count - 1
        command.Parameters.AddWithValue(params.Keys(i), params.Values(i))
    Next

    Dim adapter as New SqlDataAdapter(command)

    Try
        adapter.fill(resDT)
    Catch ex As Exception
        logFile("SP getLocation ----" + ex.Message)
    End Try
End Sub

To call the method:

Dim params As New Hashtable

params.Add("@id", res)
params.Add("@TableName", tblName)
nb.getLocation(resDT, params)
Public Function getLocation(ByVal res As String, ByVal tblName As String) As DataTable
    Dim command As SqlCommand = New SqlCommand()
    cn.Open()
    command.Connection = cn
    command.CommandType = CommandType.StoredProcedure
    command.CommandText = "jk_getLocation"
    command.Parameters.Add("@id", SqlDbType.VarChar).Value = res
    command.Parameters.Add("@TableName", SqlDbType.VarChar).Value = tblName

    Dim sda As SqlDataAdapter
    sda = New SqlDataAdapter(command)
    Dim dt As DataTable
    dt = New DataTable

    sda.Fill(dt)

    Return dt

End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM