Macros
Macros

Reputation: 7119

Parameter gets lost in query

Ok, I'm stumped here - the following code throws an error

Procedure or function 'importsp_CreateDiallerBatch' expects parameter '@BatchName, which was not supplied`

Code:

        Dim cmd As SqlCommand = New SqlCommand()
        cmd.CommandText = "importsp_CreateDiallerBatch"
        cmd.Connection = cnSQL
        cmd.Parameters.AddWithValue("@BatchName", BatchName)

        Dim IdParameter As SqlParameter = New SqlParameter()
        IdParameter.Direction = ParameterDirection.InputOutput
        IdParameter.SqlDbType = SqlDbType.Int
        IdParameter.Value = -1
        IdParameter.ParameterName = "@BatchID"
        cmd.Parameters.Add(IdParameter)

        cnSQL.Open()
        cmd.ExecuteNonQuery()

When debugging the code, BatchName definitely has a value, and checking the parameters collection of cmd right before executing the query shows 2 params, both named and with values set exactly as expected. I must have written code like this a thousand times - am I missing something here?

Upvotes: 1

Views: 101

Answers (1)

Macros
Macros

Reputation: 7119

Ok appears that I forgot the line cmd.CommandType = CommandType.StoredProcedure. Adding this in made it work.

Upvotes: 1

Related Questions