views:

259

answers:

1

As you can probably very soon see, I am a complete newbie at VB.NET, and I am having some trouble getting output from a stored procedure in SQL Server 2005.

This is the code I use

    Dim con As New SqlConnection
    Dim cmd As New SqlCommand("esp_getDates", con)
    Dim par As New SqlParameter("@PlaceID", SqlDbType.Int, 3906)
    con.ConnectionString = "Data Source=localhost\SQLEXPRESS;Initial Catalog=ProgramDB;User ID=test;Password=test"
    con.Open()
    cmd.CommandType = CommandType.StoredProcedure
    cmd.Parameters("@PlaceID").Direction = ParameterDirection.Output
    cmd.ExecuteNonQuery()
    con.Close()

I get the error;

An SqlParameter with ParameterName '@PlaceID' is not contained by this SqlParameterCollection.

Does anyone see what I'm doing wrong / have any suggestions how I can fix it? Code examples would be very helpful and any help is much appreciated.

+6  A: 

You aren't actually adding the parameter to the cmd.Parameters collection:

cmd.Parameters.Add(par)

Alternatively, just add the parameter without instantiating the Parameter object explicitly:

cmd.Parameters.Add("@PlaceID", SqlDbType.Int)
cmd.Parameters("@PlaceID").Value = 3906

Also, I'd follow the principle of using a variable as close to the declaration as possible and reorganize things this way:

Dim con As New SqlConnection("Data Source=localhost\SQLEXPRESS;Initial Catalog=ProgramDB;User ID=test;Password=test")
con.Open()

Dim cmd As New SqlCommand("esp_getDates", con)

Try
    cmd.CommandType = CommandType.StoredProcedure
    cmd.Parameters.Add("@PlaceID", SqlDbType.Int)
    cmd.Parameters("@PlaceID").Value = 3906
    cmd.ExecuteNonQuery()

Finally
    If cmd IsNot Nothing Then cmd.Dispose()
    If cn IsNot Nothing AndAlso cn.State <> ConnectionState.Closed Then cn.Close()
End Try
HardCode
Edited - I messed up the declaration and addition of the parameter.
HardCode
Oh, let me see ...
cc0
Hang on, now I'm confused. I think I missed the part about OUTPUT parameter. You don't need to pass in a parameter to return a single Place ID? "GetDates" is returning a Place ID? Can you post the Stored Procedure, too?
HardCode
Ah yes I got an error again saying I had too many parameters. I'm really sorry but I'm not allowed to share the procedure as it's quite large, but basically it returns a list of Place IDs (small integers). The integer you send with the parameter (here 3906) specifies a group of Place IDs.
cc0
If I understand you ... If you are looking to return a set of data (i.e. the results of the query), then you need to use something other than cmd.ExecuteNonQuery (note the "Non" in NonQuery). You'd need to receive the results via a DataTable or DataSet object, using a DataAdapter to Fill() it. You cannot return a list of IDs into a single SqlDbType.Int parameter. If you are sending the parameter of 3906, you do NOT want to declare the parameter as OUTPUT. Can you post just the parameter declarations from the stored procedure?
HardCode
Ahh, I see. I assume this is what you are looking for here;"ALTER PROCEDURE [dbo].[esp_getDates] @PlaceID intAS ..."Also, when executed from the MS SQL Management GUI it looks like this;"USE [ProgramDB]GODECLARE @return_value intEXEC @return_value = [dbo].[esp_getDates] @PlaceID = 3906SELECT 'Return Value' = @return_valueGO"Thank you so much for helping out!
cc0
Okay. I see. @return_value is the return value of the stored procedure, indicating success or not. It's not required to declare it in the VB code if you don't want to return it. However, @PlaceID is an INPUT parameter. It's not required to declare it in the SP with INPUT, as it will default to that.
HardCode
Ah, I see. I will keep experimenting with this and see if I can get it working. Thanks so much for the help there :]Do you have any further recommendation as to how to format the output to JSON?
cc0
No, I don't work with JSON. Sorry.
HardCode
Quite alright, thanks again so much for the help! :]
cc0