JNF
JNF

Reputation: 3730

Could not find stored procedure ''

I'm trying to send my SQL DB a query as a stored procedure. It's not really stored in the DB, I'm creating it on the fly. Here is the code (excuse my VB):

Dim idOfChosenRecord As Integer = {Some value}
Dim myQuery As String  "SELECT [field] FROM [myDB].[myTable] WHERE [id]=@id"
Dim cmd As SqlCommand = New SqlCommand(myQuery, myConnectionInitializedAndOpen)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add(New SqlParameter("@id", idOfChosenRecord))
Dim Reader As SqlDataReader = cmdView.ExecuteReader()

A similar function is running well right before my code (only it's calling a real stored procedure), and all over the module, as a matter of fact. The difference is I'm trying to create the query on the spot.

The error message is the strange title of this question: Could not find stored procedure '' - caught on the last line of the above code.

How would this be fixed (preferably without digging into the DB itself...)?

tx

Upvotes: 0

Views: 4726

Answers (4)

NeilD
NeilD

Reputation: 2298

You need to change it to

cmd.CommandType = CommandType.Text

A Stored Procedure is different from running a text query (like you're doing here).

Upvotes: 0

Mahmoud Gamal
Mahmoud Gamal

Reputation: 79929

You can't do it this way. You can't create stored procedure on the fly. Once you choose the CommandType.StoredProcedure then it will expect you to pass the procedure name in myQuery. So you have to use cmd.CommandType = CommandType.Text

Upvotes: 0

András Ottó
András Ottó

Reputation: 7695

You should change :

cmd.CommandType = CommandType.StoredProcedure

To

cmd.CommandType = CommandType.Text

Upvotes: 0

Andrey
Andrey

Reputation: 60065

Replace

cmd.CommandType = CommandType.StoredProcedure

with

cmd.CommandType = CommandType.Text

The problem is that you specified that you want to call stored procedure when in fact you have just a query. So you should specify that this is a query.

Upvotes: 5

Related Questions