output parameters not working in if return resultset

Ask a Question related to ASP.NET Data Grid Control, Design and Development.

  1. #1

    Default output parameters not working in if return resultset

    I am calling a stored procedure in SQL Server to return resultset to fill a
    datagrid in ASP.NET (using C#). I also want to return error code in ether
    return value or output parameters for ASP.NET calling program. I execute the
    stored procedure and tried to get hold of either return value or output
    parameters. However I got the error that the object reference is not set for
    the parameter which hold return value or output parameters. I enclosed
    sample SQL Server stored procedure and ASP.NET C# code. I used output
    parameters as an example here. Only code difference between return value and
    output parameters is that I need to set Direction as ReturnValue instead of
    Output.

    I also found that if remove select statement in stored procedure, i.e., if I
    don’t want to return resultset, both return value and output parameters will
    work for the same code. Is that mean that I cannot have any return value or
    output parameters if I want to return resultset? If so, is there way that I
    can get some error code back to my calling program.
    Thanks,
    Charts

    CREATE PROCEDURE spAuthors
    @contract bit,
    @errorcode int OUT
    AS
    SET NOCOUNT ON
    SELECT [au_id], [au_lname], [au_fname], [phone], [address], [city], [state],
    [zip], [contract] FROM [pubs].[dbo].[authors]
    where contract=@contract
    SET NOCOUNT OFF
    set @errorcode=1
    return 0
    GO


    conPubs = new SqlConnection( @"Server=myserver;Integrated Security=SSPI;
    Database=Pubs" );
    cmdSelect = new SqlCommand();
    cmdSelect.CommandText = "[dbo].[spAuthors]";
    cmdSelect.CommandType = System.Data.CommandType.StoredProcedure;
    cmdSelect.Connection = conPubs;
    cmdSelect.Parameters.Add("@Contract", SqlDbType.Int).Value = Contract;
    retValParam = new SqlParameter("@errorcode", SqlDbType.Int);
    retValParam.Direction = ParameterDirection.Output;
    cmdSelect.Parameters.Add(retValParam);

    conPubs.Open();
    dtrAuthors = cmdSelect.ExecuteReader();

    intretValParam=(int)retValParam.Value;



    Charts Guest

  2. Similar Questions and Discussions

    1. #39707 [NEW]: Sending more then one parameter to SP will not return output parameters
      From: aspen dot olmsted at alliance dot biz Operating system: Windows XP SP2 PHP version: 5.2.0 PHP Bug Type: PDO related...
    2. return query resultset from custom tag
      Hi, How to return an entire query resultset from custom tag to the calling page ? Thanks for your help. vmrao
    3. How do I use URL parameters to return a specific field?
      I have a form, when users complete it I would like to have a confirmation page that returns the value of the "first_name" field from the form, eg....
    4. How to return a resultset/table from a sql function?
      Hi, Is it possible to return the following (parameterized) qyery from a sql or plpsql function, and if so, what is the syntax? SELECT{ (SELECT...
    5. Displaying Resultset output in Excel format in a NEW window
      Hi, I have written an ASP script that connects to a database and runs a stored procedure and displays the results in text format ...using the...
  3. #2

    Default RE: output parameters not working in if return resultset

    Hi Charts,

    Welcome to ASPNET newsgroup.
    Regarding on the Calling SQLSERVER Stored Prrocedure with output and
    return value question, here are some of my understanding and suggestions:

    For output parameter, we can just create the parameter with the correct
    name and set the Direction to "ParameterDirection.Output"

    for return value, we should always create the Parameter with the name of
    "ReturnValue", and set the Direction to
    "ParameterDirection.ReturnValue"

    Also, for SqlCommand.ExecuteReader call, we need to close the Reader before
    we try accessing the OutputValue or ReturnValue (otherwise the parameter
    remain empty....)

    Here is a simple codesnippet demostrate the things I mentioned above:

    =====================================
    SqlConnection conn = new SqlConnection("Data Source=localhost;Initial
    Catalog=Northwind;Integrated Security=True");
    conn.Open();


    SqlCommand comm = new SqlCommand("sampleprocedure", conn);
    comm.CommandType = CommandType.StoredProcedure;

    SqlParameter param = comm.Parameters.Add("@EmployeeIDParm",
    SqlDbType.Int);
    param.Value = 1;

    param = comm.Parameters.Add("@MaxQuantity", SqlDbType.Int);
    param.Value = 1;
    param.Direction = ParameterDirection.Output;

    param = comm.Parameters.Add("@ReturnValue", SqlDbType.Int);
    param.Direction = ParameterDirection.ReturnValue;

    SqlDataReader sqlrdr = comm.ExecuteReader();
    sqlrdr.Close();


    Response.Write("<br>@MaxQuantity: " +
    comm.Parameters["@MaxQuantity"].Value);
    Response.Write("<br>@ReturnValue: " +
    comm.Parameters["@ReturnValue"].Value);

    =====================================

    Hope helps. Thanks,

    Steven Cheng
    Microsoft Online Support

    Get Secure! [url]www.microsoft.com/security[/url]
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)

    --------------------
    | Thread-Topic: output parameters not working in if return resultset
    | thread-index: AcXf81TcF9YVjQyhRpGDLzTWtgz9tg==
    | X-WBNR-Posting-Host: 24.173.128.186
    | From: =?Utf-8?B?Q2hhcnRz?= <Acharts@newsgroup.nospam>
    | Subject: output parameters not working in if return resultset
    | Date: Wed, 2 Nov 2005 13:21:04 -0800
    | Lines: 51
    | Message-ID: <5E2351E9-40A1-4345-91D6-F0CED439013A@microsoft.com>
    | MIME-Version: 1.0
    | Content-Type: text/plain;
    | charset="Utf-8"
    | Content-Transfer-Encoding: 8bit
    | X-Newsreader: Microsoft CDO for Windows 2000
    | Content-Class: urn:content-classes:message
    | Importance: normal
    | Priority: normal
    | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
    | Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridc ontrol
    | NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
    | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGXA03.phx.gbl
    | Xref: TK2MSFTNGXA01.phx.gbl
    microsoft.public.dotnet.framework.aspnet.datagridc ontrol:5898
    | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridc ontrol
    |
    | I am calling a stored procedure in SQL Server to return resultset to fill
    a
    | datagrid in ASP.NET (using C#). I also want to return error code in ether
    | return value or output parameters for ASP.NET calling program. I execute
    the
    | stored procedure and tried to get hold of either return value or output
    | parameters. However I got the error that the object reference is not set
    for
    | the parameter which hold return value or output parameters. I enclosed
    | sample SQL Server stored procedure and ASP.NET C# code. I used output
    | parameters as an example here. Only code difference between return value
    and
    | output parameters is that I need to set Direction as ReturnValue instead
    of
    | Output.
    |
    | I also found that if remove select statement in stored procedure, i.e.,
    if I
    | don’t want to return resultset, both return value and output parameters
    will
    | work for the same code. Is that mean that I cannot have any return value
    or
    | output parameters if I want to return resultset? If so, is there way
    that I
    | can get some error code back to my calling program.
    | Thanks,
    | Charts
    |
    | CREATE PROCEDURE spAuthors
    | @contract bit,
    | @errorcode int OUT
    | AS
    | SET NOCOUNT ON
    | SELECT [au_id], [au_lname], [au_fname], [phone], [address], [city],
    [state],
    | [zip], [contract] FROM [pubs].[dbo].[authors]
    | where contract=@contract
    | SET NOCOUNT OFF
    | set @errorcode=1
    | return 0
    | GO
    |
    |
    | conPubs = new SqlConnection( @"Server=myserver;Integrated
    Security=SSPI;
    | Database=Pubs" );
    | cmdSelect = new SqlCommand();
    | cmdSelect.CommandText = "[dbo].[spAuthors]";
    | cmdSelect.CommandType = System.Data.CommandType.StoredProcedure;
    | cmdSelect.Connection = conPubs;
    | cmdSelect.Parameters.Add("@Contract", SqlDbType.Int).Value = Contract;
    | retValParam = new SqlParameter("@errorcode", SqlDbType.Int);
    | retValParam.Direction = ParameterDirection.Output;
    | cmdSelect.Parameters.Add(retValParam);
    |
    | conPubs.Open();
    | dtrAuthors = cmdSelect.ExecuteReader();
    |
    | intretValParam=(int)retValParam.Value;
    |
    |
    |
    |

    Steven Cheng[MSFT] Guest

  4. #3

    Default RE: output parameters not working in if return resultset

    That works! Thanks so much! Charts

    "Steven Cheng[MSFT]" wrote:
    > Hi Charts,
    >
    > Welcome to ASPNET newsgroup.
    > Regarding on the Calling SQLSERVER Stored Prrocedure with output and
    > return value question, here are some of my understanding and suggestions:
    >
    > For output parameter, we can just create the parameter with the correct
    > name and set the Direction to "ParameterDirection.Output"
    >
    > for return value, we should always create the Parameter with the name of
    > "ReturnValue", and set the Direction to
    > "ParameterDirection.ReturnValue"
    >
    > Also, for SqlCommand.ExecuteReader call, we need to close the Reader before
    > we try accessing the OutputValue or ReturnValue (otherwise the parameter
    > remain empty....)
    >
    > Here is a simple codesnippet demostrate the things I mentioned above:
    >
    > =====================================
    > SqlConnection conn = new SqlConnection("Data Source=localhost;Initial
    > Catalog=Northwind;Integrated Security=True");
    > conn.Open();
    >
    >
    > SqlCommand comm = new SqlCommand("sampleprocedure", conn);
    > comm.CommandType = CommandType.StoredProcedure;
    >
    > SqlParameter param = comm.Parameters.Add("@EmployeeIDParm",
    > SqlDbType.Int);
    > param.Value = 1;
    >
    > param = comm.Parameters.Add("@MaxQuantity", SqlDbType.Int);
    > param.Value = 1;
    > param.Direction = ParameterDirection.Output;
    >
    > param = comm.Parameters.Add("@ReturnValue", SqlDbType.Int);
    > param.Direction = ParameterDirection.ReturnValue;
    >
    > SqlDataReader sqlrdr = comm.ExecuteReader();
    > sqlrdr.Close();
    >
    >
    > Response.Write("<br>@MaxQuantity: " +
    > comm.Parameters["@MaxQuantity"].Value);
    > Response.Write("<br>@ReturnValue: " +
    > comm.Parameters["@ReturnValue"].Value);
    >
    > =====================================
    >
    > Hope helps. Thanks,
    >
    > Steven Cheng
    > Microsoft Online Support
    >
    > Get Secure! [url]www.microsoft.com/security[/url]
    > (This posting is provided "AS IS", with no warranties, and confers no
    > rights.)
    >
    > --------------------
    > | Thread-Topic: output parameters not working in if return resultset
    > | thread-index: AcXf81TcF9YVjQyhRpGDLzTWtgz9tg==
    > | X-WBNR-Posting-Host: 24.173.128.186
    > | From: =?Utf-8?B?Q2hhcnRz?= <Acharts@newsgroup.nospam>
    > | Subject: output parameters not working in if return resultset
    > | Date: Wed, 2 Nov 2005 13:21:04 -0800
    > | Lines: 51
    > | Message-ID: <5E2351E9-40A1-4345-91D6-F0CED439013A@microsoft.com>
    > | MIME-Version: 1.0
    > | Content-Type: text/plain;
    > | charset="Utf-8"
    > | Content-Transfer-Encoding: 8bit
    > | X-Newsreader: Microsoft CDO for Windows 2000
    > | Content-Class: urn:content-classes:message
    > | Importance: normal
    > | Priority: normal
    > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
    > | Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridc ontrol
    > | NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
    > | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGXA03.phx.gbl
    > | Xref: TK2MSFTNGXA01.phx.gbl
    > microsoft.public.dotnet.framework.aspnet.datagridc ontrol:5898
    > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridc ontrol
    > |
    > | I am calling a stored procedure in SQL Server to return resultset to fill
    > a
    > | datagrid in ASP.NET (using C#). I also want to return error code in ether
    > | return value or output parameters for ASP.NET calling program. I execute
    > the
    > | stored procedure and tried to get hold of either return value or output
    > | parameters. However I got the error that the object reference is not set
    > for
    > | the parameter which hold return value or output parameters. I enclosed
    > | sample SQL Server stored procedure and ASP.NET C# code. I used output
    > | parameters as an example here. Only code difference between return value
    > and
    > | output parameters is that I need to set Direction as ReturnValue instead
    > of
    > | Output.
    > |
    > | I also found that if remove select statement in stored procedure, i.e.,
    > if I
    > | don’t want to return resultset, both return value and output parameters
    > will
    > | work for the same code. Is that mean that I cannot have any return value
    > or
    > | output parameters if I want to return resultset? If so, is there way
    > that I
    > | can get some error code back to my calling program.
    > | Thanks,
    > | Charts
    > |
    > | CREATE PROCEDURE spAuthors
    > | @contract bit,
    > | @errorcode int OUT
    > | AS
    > | SET NOCOUNT ON
    > | SELECT [au_id], [au_lname], [au_fname], [phone], [address], [city],
    > [state],
    > | [zip], [contract] FROM [pubs].[dbo].[authors]
    > | where contract=@contract
    > | SET NOCOUNT OFF
    > | set @errorcode=1
    > | return 0
    > | GO
    > |
    > |
    > | conPubs = new SqlConnection( @"Server=myserver;Integrated
    > Security=SSPI;
    > | Database=Pubs" );
    > | cmdSelect = new SqlCommand();
    > | cmdSelect.CommandText = "[dbo].[spAuthors]";
    > | cmdSelect.CommandType = System.Data.CommandType.StoredProcedure;
    > | cmdSelect.Connection = conPubs;
    > | cmdSelect.Parameters.Add("@Contract", SqlDbType.Int).Value = Contract;
    > | retValParam = new SqlParameter("@errorcode", SqlDbType.Int);
    > | retValParam.Direction = ParameterDirection.Output;
    > | cmdSelect.Parameters.Add(retValParam);
    > |
    > | conPubs.Open();
    > | dtrAuthors = cmdSelect.ExecuteReader();
    > |
    > | intretValParam=(int)retValParam.Value;
    > |
    > |
    > |
    > |
    >
    >
    Charts Guest

  5. #4

    Default RE: output parameters not working in if return resultset

    You're welcome Charts,

    Good luck!

    Steven Cheng
    Microsoft Online Support

    Get Secure! [url]www.microsoft.com/security[/url]
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
    --------------------
    | Thread-Topic: output parameters not working in if return resultset
    | thread-index: AcXghHn0SgrvcU5bTCiBGYCU26YYlQ==
    | X-WBNR-Posting-Host: 24.173.128.186
    | From: =?Utf-8?B?Q2hhcnRz?= <Acharts@newsgroup.nospam>
    | References: <5E2351E9-40A1-4345-91D6-F0CED439013A@microsoft.com>
    <C36dCwF4FHA.1144@TK2MSFTNGXA01.phx.gbl>
    | Subject: RE: output parameters not working in if return resultset
    | Date: Thu, 3 Nov 2005 06:40:03 -0800
    | Lines: 153
    | Message-ID: <7D70814D-F4FD-4031-9D42-6C450F1B62C4@microsoft.com>
    | MIME-Version: 1.0
    | Content-Type: text/plain;
    | charset="Utf-8"
    | Content-Transfer-Encoding: 8bit
    | X-Newsreader: Microsoft CDO for Windows 2000
    | Content-Class: urn:content-classes:message
    | Importance: normal
    | Priority: normal
    | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
    | Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridc ontrol
    | NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
    | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGXA03.phx.gbl
    | Xref: TK2MSFTNGXA01.phx.gbl
    microsoft.public.dotnet.framework.aspnet.datagridc ontrol:5904
    | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridc ontrol
    |
    | That works! Thanks so much! Charts
    |
    | "Steven Cheng[MSFT]" wrote:
    |
    | > Hi Charts,
    | >
    | > Welcome to ASPNET newsgroup.
    | > Regarding on the Calling SQLSERVER Stored Prrocedure with output and
    | > return value question, here are some of my understanding and
    suggestions:
    | >
    | > For output parameter, we can just create the parameter with the
    correct
    | > name and set the Direction to "ParameterDirection.Output"
    | >
    | > for return value, we should always create the Parameter with the name
    of
    | > "ReturnValue", and set the Direction to
    | > "ParameterDirection.ReturnValue"
    | >
    | > Also, for SqlCommand.ExecuteReader call, we need to close the Reader
    before
    | > we try accessing the OutputValue or ReturnValue (otherwise the
    parameter
    | > remain empty....)
    | >
    | > Here is a simple codesnippet demostrate the things I mentioned above:
    | >
    | > =====================================
    | > SqlConnection conn = new SqlConnection("Data Source=localhost;Initial
    | > Catalog=Northwind;Integrated Security=True");
    | > conn.Open();
    | >
    | >
    | > SqlCommand comm = new SqlCommand("sampleprocedure", conn);
    | > comm.CommandType = CommandType.StoredProcedure;
    | >
    | > SqlParameter param = comm.Parameters.Add("@EmployeeIDParm",
    | > SqlDbType.Int);
    | > param.Value = 1;
    | >
    | > param = comm.Parameters.Add("@MaxQuantity", SqlDbType.Int);
    | > param.Value = 1;
    | > param.Direction = ParameterDirection.Output;
    | >
    | > param = comm.Parameters.Add("@ReturnValue", SqlDbType.Int);
    | > param.Direction = ParameterDirection.ReturnValue;
    | >
    | > SqlDataReader sqlrdr = comm.ExecuteReader();
    | > sqlrdr.Close();
    | >
    | >
    | > Response.Write("<br>@MaxQuantity: " +
    | > comm.Parameters["@MaxQuantity"].Value);
    | > Response.Write("<br>@ReturnValue: " +
    | > comm.Parameters["@ReturnValue"].Value);
    | >
    | > =====================================
    | >
    | > Hope helps. Thanks,
    | >
    | > Steven Cheng
    | > Microsoft Online Support
    | >
    | > Get Secure! [url]www.microsoft.com/security[/url]
    | > (This posting is provided "AS IS", with no warranties, and confers no
    | > rights.)
    | >
    | > --------------------
    | > | Thread-Topic: output parameters not working in if return resultset
    | > | thread-index: AcXf81TcF9YVjQyhRpGDLzTWtgz9tg==
    | > | X-WBNR-Posting-Host: 24.173.128.186
    | > | From: =?Utf-8?B?Q2hhcnRz?= <Acharts@newsgroup.nospam>
    | > | Subject: output parameters not working in if return resultset
    | > | Date: Wed, 2 Nov 2005 13:21:04 -0800
    | > | Lines: 51
    | > | Message-ID: <5E2351E9-40A1-4345-91D6-F0CED439013A@microsoft.com>
    | > | MIME-Version: 1.0
    | > | Content-Type: text/plain;
    | > | charset="Utf-8"
    | > | Content-Transfer-Encoding: 8bit
    | > | X-Newsreader: Microsoft CDO for Windows 2000
    | > | Content-Class: urn:content-classes:message
    | > | Importance: normal
    | > | Priority: normal
    | > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
    | > | Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridc ontrol
    | > | NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
    | > | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGXA03.phx.gbl
    | > | Xref: TK2MSFTNGXA01.phx.gbl
    | > microsoft.public.dotnet.framework.aspnet.datagridc ontrol:5898
    | > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridc ontrol
    | > |
    | > | I am calling a stored procedure in SQL Server to return resultset to
    fill
    | > a
    | > | datagrid in ASP.NET (using C#). I also want to return error code in
    ether
    | > | return value or output parameters for ASP.NET calling program. I
    execute
    | > the
    | > | stored procedure and tried to get hold of either return value or
    output
    | > | parameters. However I got the error that the object reference is not
    set
    | > for
    | > | the parameter which hold return value or output parameters. I
    enclosed
    | > | sample SQL Server stored procedure and ASP.NET C# code. I used output
    | > | parameters as an example here. Only code difference between return
    value
    | > and
    | > | output parameters is that I need to set Direction as ReturnValue
    instead
    | > of
    | > | Output.
    | > |
    | > | I also found that if remove select statement in stored procedure,
    i.e.,
    | > if I
    | > | don’t want to return resultset, both return value and output
    parameters
    | > will
    | > | work for the same code. Is that mean that I cannot have any return
    value
    | > or
    | > | output parameters if I want to return resultset? If so, is there way
    | > that I
    | > | can get some error code back to my calling program.
    | > | Thanks,
    | > | Charts
    | > |
    | > | CREATE PROCEDURE spAuthors
    | > | @contract bit,
    | > | @errorcode int OUT
    | > | AS
    | > | SET NOCOUNT ON
    | > | SELECT [au_id], [au_lname], [au_fname], [phone], [address], [city],
    | > [state],
    | > | [zip], [contract] FROM [pubs].[dbo].[authors]
    | > | where contract=@contract
    | > | SET NOCOUNT OFF
    | > | set @errorcode=1
    | > | return 0
    | > | GO
    | > |
    | > |
    | > | conPubs = new SqlConnection( @"Server=myserver;Integrated
    | > Security=SSPI;
    | > | Database=Pubs" );
    | > | cmdSelect = new SqlCommand();
    | > | cmdSelect.CommandText = "[dbo].[spAuthors]";
    | > | cmdSelect.CommandType = System.Data.CommandType.StoredProcedure;
    | > | cmdSelect.Connection = conPubs;
    | > | cmdSelect.Parameters.Add("@Contract", SqlDbType.Int).Value =
    Contract;
    | > | retValParam = new SqlParameter("@errorcode", SqlDbType.Int);
    | > | retValParam.Direction = ParameterDirection.Output;
    | > | cmdSelect.Parameters.Add(retValParam);
    | > |
    | > | conPubs.Open();
    | > | dtrAuthors = cmdSelect.ExecuteReader();
    | > |
    | > | intretValParam=(int)retValParam.Value;
    | > |
    | > |
    | > |
    | > |
    | >
    | >
    |

    Steven Cheng[MSFT] Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139