Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Calling a Stored Procedure with Output Parameter
Message
 
 
To
25/06/2010 20:28:21
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:
01470671
Views:
25
>>>>>>>>You're right, I miscounted. Did you also pass 10 0s ?
>>>>>>>
>>>>>>>Yes, as a string
>>>>>>
>>>>>>Did you test SP by itself just in case - though it's so simple I don't see a reason why it would not work.
>>>>>
>>>>>Just did and it seems to work. @NextNumber is "0000001045" just like I set it in the SP and the return value is 0 as expected. I will step into the C# code to see why it doesn't get back to there.
>>>>>Tim
>>>>
>>>>What is your C# code? Did you remove extra parameter's declaration?
>>>
>>>Yes, I tried it about a dozen ways. I tried this too.
>>>
>>>SqlConnection conn = new SqlConnection(this.GetConnectionString("PantryWare"));
>>>			
>>>
>>>SqlCommand cmd = new SqlCommand();
>>>object newVal;
>>>cmd.CommandText = "dbo.GetNextNumber";
>>>cmd.CommandType = CommandType.StoredProcedure;
>>>SqlParameter param = new SqlParameter("@NextNumber", "0000000000");
>>>param.Direction = ParameterDirection.Output;
>>>cmd.Parameters.Add(param);
>>>cmd.Connection = conn;
>>>conn.Open();
>>>
>>>newVal = cmd.ExecuteScalar();
>>>conn.Close();
>>>
>>>string myNumber = newVal.ToString();
>>>
>>>
>>>newVal comes back Null.
>>>Tim
>>
>>Try
>>
>>string myNumber = cmd.Parameters("@NextNumber").Value.ToString();
>
>I tried that earlier and I just tried again with the SQL native code. The parameter visible name is @NextNumber and its value is 0

Try running cmd.ExecuteNonQuery();

This should be very simple - I don't understand why it doesn't work. I need to close UT for a day now - check http://forums.asp.net/54.aspx forum - this question is one of the most asked.

Also, grab the value of the parameter before closing connection.

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;
  }
}
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform