tags:

views:

42

answers:

3

Dear All,

Can anyone please help me on the following:

I have created a SQL Server stored procedure as follows:

create procedure prcGet_sub_menu_list
     @sub_menu     char(5)
as
begin
     select
          'menu_code'     =     menu_code
          'menu_name'     =     menu_name
     from sub_menu_master
     where menu_code      =     @sub_menu
end
return

Now i am calling this procedure from VB.NET, but i get an error like 'Stored procedure prcGet_sub_menu_list expects parameter @sub_menu which was not supplied'. Please help me on the same. The code which i have in VB.NET is as follows:

Imports System.Data
Imports System.Data.SqlClient

Dim sqlConn as New SqlClient.SqlConnection
sqlConn.ConnectionString = "........"
sqlConn.Open()

Dim menuCode as string
menuCode = cboDetails.selectedItem

Dim sqlCmd as New SqlCommand
sqlCmd.Connection = Connection.sqlConn
sqlCmd.CommandType = CommandType.StoredProcedure
sqlCmd.CommandText = "prcGet_sub_menu_list"
sqlCmd.Parameter.Add("menuCode", SqlDbType.Char)

Dim sqlDA as New SqlDataAdapter()
sqlDA.SelectCommand = sqlCmd
Dim sqlDT as New DataTable
sqlDA.Fill(sqlDT)

This is the code that i have written and it gives me the error: 'Stored procedure prcGet_sub_menu_list expects parameter @sub_menu which was not supplied'.

Please give me some help on the same.

Regards, George

+1  A: 

As the message implies, you should add a parameter named "sub_menu" in the same way as you're adding the "menuCode" parameter. You should probably also give it a value:

sqlCmd.Parameter.Add("sub_menu", SqlDbType.Char).Value = "Blah"

(Of course, I don't know what the correct type is, so Char is just an example.)

Evgeny
Dear Evgeny, Thanks for the reply. I tried the same, and it works when a value is given at the end like .value = "MENU1", but when i pass a variable there, it does not work and gives me an error. I have given the statement like sqlCmd.Parameters.Add(New SqlParameter("@menu_code", SqlDbType.Char, 5)).Value = menuCode. Please help on the same.
George Trevour Dsouza
+1  A: 

You need to use the correct parameter name

Something like

sqlCmd.Parameter.Add("@sub_menu", SqlDbType.Char)

And assign it a value

Something like

sqlCmd.Parameters("@sub_menu").Value = val

Have a look at SqlCommand.Parameters Property

and maybe Lesson 07: Using Stored Procedures

astander
A: 

Hi,

You have to give the same parameter name and type like in your stored procedure.

  sqlCmd.Parameters.Add(New SqlParameter("@sub_menu", SqlDbType.Char, 5)).Value = "Joe"

Geetha.

Geetha
Dear Geetha, Thanks a lot for the reply. I tried the same, and it works, but instead of the value "Joe" which you have given, when i pass a variable there, it does not work and gives me an error. I have given the statement like sqlCmd.Parameters.Add(New SqlParameter("@menu_code", SqlDbType.Char, 5)).Value = menuCode. Please help on the same.
George Trevour Dsouza
Procedure or Function 'prc_test' expects parameter '@menu_code', which was not supplied. This is the error code. I dont get this error when i pass the value as .value = "MENU1" and it works fine by giving me the correct output, but not when i pass a variable. Please advice. Thanks.
George Trevour Dsouza
http://msdn.microsoft.com/en-us/library/yy6y35y8.aspx
Geetha
Yes it is having a value. Dont have any idea as to why this sort of an error is getting displayed.
George Trevour Dsouza
Try it by changing the datatype in stored procedure and in sqldbtype as varchar/nvarchar.
Geetha
Dear Geetha, sorry, the variable was not having a value and that was the reason for the error. Now it is working fine and thanks a lot for the help and support given.
George Trevour Dsouza
I am new to stackoverflow and where will i mark that. Please do let me know that.
George Trevour Dsouza
I have done it and thanks once again for the help.
George Trevour Dsouza