Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Calling a Stored Procedure with Output Parameter
Message
From
27/06/2010 09:28:14
Timothy Bryan
Sharpline Consultants
Conroe, Texas, United States
 
General information
Forum:
ASP.NET
Category:
The Mere Mortals .NET Framework
Miscellaneous
Thread ID:
01470620
Message ID:
01470716
Views:
34
>>>
>>>See sample code here http://www.mikesdotnetting.com/Article/54/Getting-the-identity-of-the-most-recently-added-record
>>>
>>>
>>>//C#
>>>string query = "AddCategory";
>>>int ID;
>>>string connect = @"Server=.\SQLExpress;Database=Northwind;Trusted_Connection=Yes;";
>>>using (SqlConnection conn = new SqlConnection(connect))
>>>{
>>>  using (SqlCommand cmd = new SqlCommand(query, conn))
>>>  {
>>>    cmd.CommandType = CommandType.StoredProcedure;
>>>    cmd.Parameters.AddWithValue("@Category", Category.Text);
>>>    cmd.Parameters.Add("@CategoryID", SqlDbType.Int, 0, "CategoryID");
>>>    cmd.Parameters["@CategoryID"].Direction = ParameterDirection.Output;
>>>    conn.Open();
>>>    cmd.ExecuteNonQuery();
>>>    ID = (int)cmd.Parameters["@CategoryID"].Value;
>>>  }
>>>}
>>
>>I did it exactly as you show it and the @NextNumber parameter is still "0". I can drill right into the cmd object parameters and I can see the named parameter and it is zero. When I run the SP in SSMS I get zero for the value also but the other value is correct. How you get that other value is what is obviously missing.
>>Tim
>
>Tim,
>
>I don't know what you're missing. I know that this code worked fine for me.

The only difference between yours and what I used: You have two parameters and you are returning an integer. Try to do a similar one with only the parameter for return value and one with returning a string value. My final attempt code I just posted for Bonnie to see. Try that and then maybe the difference lies in needing to pass in another parameter even if not used, or it is a problem to get a string back.

Also, what does the stored procedure look like you ran this code against?
Tim
Timothy Bryan
Previous
Reply
Map
View

Click here to load this message in the networking platform