>>
>>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
Hi Tim,
if I'm following this conversation correctly, then there seems to be some difficulty in getting back the output parameter from a stored procedure. Is that right? What alternatives do I have that I can try?