C
Charts
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;
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;