C# & return value from stored procedure

Hi

I'm not sure if i'm posting in the right place or it should be C#. Apologies for that .. i'm using the new Visual Web Developer 2005 Express with C# language.

My problem is that i'm trying to get the return value from my stored procedure..


Code:

protected void Button1_Click(object sender, EventArgs e)
    {
        SqlCommand oSqlComm = new SqlCommand();

        SqlConnection oSqlConn = new SqlConnection();

        oSqlConn.ConnectionString = SqlDataSource1.ConnectionString;

        oSqlComm.Connection = oSqlConn;
        oSqlConn.Open();

        oSqlComm.CommandText = "check_refnumber";
        oSqlComm.CommandType = CommandType.StoredProcedure;

        oSqlComm.Parameters.AddWithValue("@p_year_1", DateTime.Now.ToString("yy"));
        oSqlComm.Parameters.AddWithValue("@p_month_2", DateTime.Now.ToString("MM"));
        oSqlComm.Parameters.AddWithValue("@out", ParameterDirection.Output);

        oSqlComm.ExecuteNonQuery();

       
       // Response.Write ("test");
        //Response.Write (oSqlComm.Parameters["@out"].Value);
        TextBox1.Text = oSqlComm.Parameters["@out"].Value.ToString();

    }



code for the stored procedure is as follow


Code:

ALTER PROCEDURE check_refnumber
(
  @p_year_1 int,
  @p_month_2 int,
  @out int OUTPUT
)

AS
BEGIN


SET @out = (SELECT max(p_num) FROM Temporary where p_year = @p_year_1 AND p_month = @p_month_2)


END



i'm trying to return the max value of p_num from my table to textbox1 or to any integer variable.
the only value i'm getting back is "output"

any help would be appreciated

thanks..


Answer this question

C# & return value from stored procedure

  • Thomas Norlund

    Hi

    Does it also applies to asp.net 2.0

    i'm using visual web developer 2005 express edition beta 2.. and i can't get it to work ..


    Thanks

  • Dave27

    You can't use AddWithValue for an output parameter. You need to setup a SqlParameter object (where you can set the ParameterDirection) and use AddRange to add it to the Parameters collection.

  • C# & return value from stored procedure