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. Database & SysAdmin
  3. Database
  4. stored procedure Output parameter value is Null

stored procedure Output parameter value is Null

Scheduled Pinned Locked Moved Database
databasecomhelpquestion
6 Posts 4 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.
  • F Offline
    F Offline
    For_IT
    wrote on last edited by
    #1

    Hi I am using following stored procedure ----------------------------------- set ANSI_NULLS ON set QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[GetUserControlTemplateCode] @product_id int, @genmask_code nvarchar(max) output AS BEGIN SET NOCOUNT ON; Select dbo.cp_product.product_type, dbo.cp_genmask.genmask_code from dbo.cp_genmask inner join dbo.cp_product on dbo.cp_genmask.genmask_product_type=dbo.cp_product.product_type where dbo.cp_product.product_id=@product_id END -------------------------------- when I execute the above code using exec GetUserControlTemplateCode 1,'x' I get correct values for x i.e. output parameter. My code behind to call stored procedure is as follows --------------------------------- protected void Page_Load(object sender, EventArgs e) { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sConnectionString2"].ConnectionString); SqlCommand Com = con.CreateCommand(); Com.CommandType = CommandType.StoredProcedure; Com.CommandText = "GetUserControlTemplateCode"; //Create parameter object to provide input SqlParameter parInput = Com.Parameters.Add("@product_id", SqlDbType.Int); parInput.Direction = ParameterDirection.Input; parInput.Value = nProductID; // Create parameter to hold output SqlParameter parOutput = Com.Parameters.Add("@genmask_code", SqlDbType.NVarChar,300); parOutput.Direction = ParameterDirection.Output; //Open the connection con.Open(); //Execute command try { Com.ExecuteNonQuery(); } catch (Exception ex) { Response.Write(ex.ToString()); } string sTemplateCode = Convert.ToString(parOutput.Value); sTemplateCode = sTemplateCode.Replace("ProductID", nProductID.ToString()); Control myControl = ParseControl(sTemplateCode); PlaceHolderForProducts.Controls.Add(myControl); con.Close(); } ----------------------------------- when I execute the code, i do not get any exception, but the value of output parameter is null. Can someone please help me to understand whats missing here? Thanks.

    N C 2 Replies Last reply
    0
    • F For_IT

      Hi I am using following stored procedure ----------------------------------- set ANSI_NULLS ON set QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[GetUserControlTemplateCode] @product_id int, @genmask_code nvarchar(max) output AS BEGIN SET NOCOUNT ON; Select dbo.cp_product.product_type, dbo.cp_genmask.genmask_code from dbo.cp_genmask inner join dbo.cp_product on dbo.cp_genmask.genmask_product_type=dbo.cp_product.product_type where dbo.cp_product.product_id=@product_id END -------------------------------- when I execute the above code using exec GetUserControlTemplateCode 1,'x' I get correct values for x i.e. output parameter. My code behind to call stored procedure is as follows --------------------------------- protected void Page_Load(object sender, EventArgs e) { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sConnectionString2"].ConnectionString); SqlCommand Com = con.CreateCommand(); Com.CommandType = CommandType.StoredProcedure; Com.CommandText = "GetUserControlTemplateCode"; //Create parameter object to provide input SqlParameter parInput = Com.Parameters.Add("@product_id", SqlDbType.Int); parInput.Direction = ParameterDirection.Input; parInput.Value = nProductID; // Create parameter to hold output SqlParameter parOutput = Com.Parameters.Add("@genmask_code", SqlDbType.NVarChar,300); parOutput.Direction = ParameterDirection.Output; //Open the connection con.Open(); //Execute command try { Com.ExecuteNonQuery(); } catch (Exception ex) { Response.Write(ex.ToString()); } string sTemplateCode = Convert.ToString(parOutput.Value); sTemplateCode = sTemplateCode.Replace("ProductID", nProductID.ToString()); Control myControl = ParseControl(sTemplateCode); PlaceHolderForProducts.Controls.Add(myControl); con.Close(); } ----------------------------------- when I execute the code, i do not get any exception, but the value of output parameter is null. Can someone please help me to understand whats missing here? Thanks.

      N Offline
      N Offline
      nguyenvhn
      wrote on last edited by
      #2

      I did not see the statement setting value for @genmask_code so infact, it have no return value. Also I think exec GetUserControlTemplateCode 1,'x' will not work because of lacking OUTPUT modifier. It should be: exec GetUserControlTemplateCode 1, @x OUTPUT print @x

      1 Reply Last reply
      0
      • F For_IT

        Hi I am using following stored procedure ----------------------------------- set ANSI_NULLS ON set QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[GetUserControlTemplateCode] @product_id int, @genmask_code nvarchar(max) output AS BEGIN SET NOCOUNT ON; Select dbo.cp_product.product_type, dbo.cp_genmask.genmask_code from dbo.cp_genmask inner join dbo.cp_product on dbo.cp_genmask.genmask_product_type=dbo.cp_product.product_type where dbo.cp_product.product_id=@product_id END -------------------------------- when I execute the above code using exec GetUserControlTemplateCode 1,'x' I get correct values for x i.e. output parameter. My code behind to call stored procedure is as follows --------------------------------- protected void Page_Load(object sender, EventArgs e) { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sConnectionString2"].ConnectionString); SqlCommand Com = con.CreateCommand(); Com.CommandType = CommandType.StoredProcedure; Com.CommandText = "GetUserControlTemplateCode"; //Create parameter object to provide input SqlParameter parInput = Com.Parameters.Add("@product_id", SqlDbType.Int); parInput.Direction = ParameterDirection.Input; parInput.Value = nProductID; // Create parameter to hold output SqlParameter parOutput = Com.Parameters.Add("@genmask_code", SqlDbType.NVarChar,300); parOutput.Direction = ParameterDirection.Output; //Open the connection con.Open(); //Execute command try { Com.ExecuteNonQuery(); } catch (Exception ex) { Response.Write(ex.ToString()); } string sTemplateCode = Convert.ToString(parOutput.Value); sTemplateCode = sTemplateCode.Replace("ProductID", nProductID.ToString()); Control myControl = ParseControl(sTemplateCode); PlaceHolderForProducts.Controls.Add(myControl); con.Close(); } ----------------------------------- when I execute the code, i do not get any exception, but the value of output parameter is null. Can someone please help me to understand whats missing here? Thanks.

        C Offline
        C Offline
        Colin Angus Mackay
        wrote on last edited by
        #3

        Your stored procedure returns a set of data. The result set contains two columns, product_type and genmask_code. Use ExecuteReader() or use a DataAdapter to get the information out, or change the stored procedure. To populate the @genmask_code output parameter you can do something like this:

        SELECT @genmask_code = dbo.cp_genmask.genmask_code
        FROM ...

        NOTE: You should only do this if you expect the SELECT statement to retrieve only one row.


        Scottish Developers events: * .NET debugging, tracing and instrumentation by Duncan Edwards Jones and Code Coverage in .NET by Craig Murphy * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

        M 1 Reply Last reply
        0
        • C Colin Angus Mackay

          Your stored procedure returns a set of data. The result set contains two columns, product_type and genmask_code. Use ExecuteReader() or use a DataAdapter to get the information out, or change the stored procedure. To populate the @genmask_code output parameter you can do something like this:

          SELECT @genmask_code = dbo.cp_genmask.genmask_code
          FROM ...

          NOTE: You should only do this if you expect the SELECT statement to retrieve only one row.


          Scottish Developers events: * .NET debugging, tracing and instrumentation by Duncan Edwards Jones and Code Coverage in .NET by Craig Murphy * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

          M Offline
          M Offline
          Mike Dimmick
          wrote on last edited by
          #4

          Colin Angus Mackay wrote:

          NOTE: You should only do this if you expect the SELECT statement to retrieve only one row.

          You won't get an error if it does retrieve more than one row, but if it does, you'll simply get the value from the last row returned by the query. Unless you put an ORDER BY clause in, however, which row that is will be unpredictable. If no rows are returned, the value of the variable will be unchanged. Stability. What an interesting concept. -- Chris Maunder

          F 1 Reply Last reply
          0
          • M Mike Dimmick

            Colin Angus Mackay wrote:

            NOTE: You should only do this if you expect the SELECT statement to retrieve only one row.

            You won't get an error if it does retrieve more than one row, but if it does, you'll simply get the value from the last row returned by the query. Unless you put an ORDER BY clause in, however, which row that is will be unpredictable. If no rows are returned, the value of the variable will be unchanged. Stability. What an interesting concept. -- Chris Maunder

            F Offline
            F Offline
            For_IT
            wrote on last edited by
            #5

            Thanks for your replies, I changed my stored procedure as follows to assign the value to output variable: ALTER PROCEDURE [dbo].[GetUserControlTemplateCode] @product_id int, @genmask_code nvarchar(max) output AS BEGIN SET NOCOUNT ON; Select dbo.cp_product.product_type, @genmask_code=dbo.cp_genmask.genmask_code from dbo.cp_genmask inner join dbo.cp_product on dbo.cp_genmask.genmask_product_type=dbo.cp_product.product_type where dbo.cp_product.product_id=@product_id END when I execute this I get following error: A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations. where should I do data assignment operation?

            F 1 Reply Last reply
            0
            • F For_IT

              Thanks for your replies, I changed my stored procedure as follows to assign the value to output variable: ALTER PROCEDURE [dbo].[GetUserControlTemplateCode] @product_id int, @genmask_code nvarchar(max) output AS BEGIN SET NOCOUNT ON; Select dbo.cp_product.product_type, @genmask_code=dbo.cp_genmask.genmask_code from dbo.cp_genmask inner join dbo.cp_product on dbo.cp_genmask.genmask_product_type=dbo.cp_product.product_type where dbo.cp_product.product_id=@product_id END when I execute this I get following error: A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations. where should I do data assignment operation?

              F Offline
              F Offline
              For_IT
              wrote on last edited by
              #6

              I changed the code as follows ALTER PROCEDURE [dbo].[GetUserControlTemplateCode] @product_id int, @genmask_code nvarchar(max) output AS BEGIN SET NOCOUNT ON; Declare @product_type int Select b.product_type, @genmask_code=dbo.cp_genmask.genmask_code from dbo.cp_genmask as a inner join dbo.cp_product as b on a.genmask_product_type=b.product_type where b.product_id=@product_id END I executed this code using using exec GetUserControlTemplateCode 1, @genmask_code output print @genmask_code It gives me message as command executed successfully, but I can not see any data in output window. Again when I call this procedure from code behind code it shows null value... Please suggest where I am wrong.

              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