Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Returning a parameter from a stored proc

Returning a parameter from a stored proc

Scheduled Pinned Locked Moved C#
questioncsharpdebugging
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jamesc69
    wrote on last edited by
    #1

    Hi, I have a stored proc that at the end executes the following line: Select @ID In my C# I have the following to try and grab it: SqlParameter AgentID = new SqlParameter("@ID", SqlDbType.Int, 4);             AgentID.Direction = ParameterDirection.ReturnValue;             sqlCmd.Parameters.Add(AgentID); int NewAgentID = 0;             try             {                   sqlCon.Open();                   sqlCmd.ExecuteScalar();                   sqlCon.Close();                   NewAgentID = int.Parse(sqlCmd.Parameters["@ID"].Value.ToString());                   return NewAgentID;             } In debug this seems to set NewAgentID to -1, however it should be a legitimately returned variable from the stored proc, any ideas what is wrong?

    B J 2 Replies Last reply
    0
    • J jamesc69

      Hi, I have a stored proc that at the end executes the following line: Select @ID In my C# I have the following to try and grab it: SqlParameter AgentID = new SqlParameter("@ID", SqlDbType.Int, 4);             AgentID.Direction = ParameterDirection.ReturnValue;             sqlCmd.Parameters.Add(AgentID); int NewAgentID = 0;             try             {                   sqlCon.Open();                   sqlCmd.ExecuteScalar();                   sqlCon.Close();                   NewAgentID = int.Parse(sqlCmd.Parameters["@ID"].Value.ToString());                   return NewAgentID;             } In debug this seems to set NewAgentID to -1, however it should be a legitimately returned variable from the stored proc, any ideas what is wrong?

      B Offline
      B Offline
      Bardy85
      wrote on last edited by
      #2

      You need to get C# to excute your stored procedure then get the return variable and store it in NewAgentID. I use Microsoft Database access application block. So in c# the code would look somehting like this.

      int NewAgentId = int.Parse(SqlHelper.ExecuteScalar(ConnectionString, CommandType.StoredProcedure, "Call for Stored Procedure").ToString());

      1 Reply Last reply
      0
      • J jamesc69

        Hi, I have a stored proc that at the end executes the following line: Select @ID In my C# I have the following to try and grab it: SqlParameter AgentID = new SqlParameter("@ID", SqlDbType.Int, 4);             AgentID.Direction = ParameterDirection.ReturnValue;             sqlCmd.Parameters.Add(AgentID); int NewAgentID = 0;             try             {                   sqlCon.Open();                   sqlCmd.ExecuteScalar();                   sqlCon.Close();                   NewAgentID = int.Parse(sqlCmd.Parameters["@ID"].Value.ToString());                   return NewAgentID;             } In debug this seems to set NewAgentID to -1, however it should be a legitimately returned variable from the stored proc, any ideas what is wrong?

        J Offline
        J Offline
        J a a n s
        wrote on last edited by
        #3

        jamesc69 wrote:

        I have a stored proc that at the end executes the following line: Select @ID

        This means that you are returning the value of @ID. try some thing like this

        sqlCon.Open();
        object result = sqlCmd.ExecuteScalar();
        sqlCon.Close();
        if (result != null)
            NewAgentID = int.Parse(result.ToString());

        J 1 Reply Last reply
        0
        • J J a a n s

          jamesc69 wrote:

          I have a stored proc that at the end executes the following line: Select @ID

          This means that you are returning the value of @ID. try some thing like this

          sqlCon.Open();
          object result = sqlCmd.ExecuteScalar();
          sqlCon.Close();
          if (result != null)
              NewAgentID = int.Parse(result.ToString());

          J Offline
          J Offline
          jamesc69
          wrote on last edited by
          #4

          Hi, Thanks all, this is the correct solution Thanks James

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups