Groups | Blog | Home
all groups > sql server programming > november 2004 >

sql server programming : Stored procedure always returns NULL in ADO.NET


konnyro NO[at]SPAM yahoo.com
11/6/2004 10:42:33 PM
I have a simple stored procedure, it always returns NULL value,
but if I run it in the query anl. it'll work just fine.
Can't figure it out...
Any hint will be appreciated

Thanks,

Dean,

if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[usp_GetTotalWeight]') and OBJECTPROPERTY(id,
N'IsProcedure') = 1)
drop procedure [dbo].[usp_GetTotalWeight]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
create procedure usp_GetTotalWeight @OrderID int
AS
BEGIN
set nocount on
Select 44 as 'Weight'
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

/**********************************************************/
C# code:
/**********************************************************/

System.Data.OleDb.OleDbDataReader objDataReader;
Int64 curTotal=0;
string[] ParamName = {"OrderID"};
System.Data.OleDb.OleDbType[] ParamType
={System.Data.OleDb.OleDbType.Integer};
string[] ParamValue={Convert.ToString(OrderID)};
objDataReader=GPC_obj_DataBase.fn_GetReader("usp_GetTotalWeight",ParamName,ParamType,ParamValue);

int temp=objDataReader["Weight"]
....

/**********************************************************/
public System.Data.OleDb.OleDbDataReader fn_GetReader(string
strSQLStatement,string[] spParamName, System.Data.OleDb.OleDbType[]
spParamType, string[] spParamValue)
{
if(GPC_obj_DataReader!=null)
{
GPC_obj_DataReader.Close();
}
System.Data.OleDb.OleDbCommand GPC_obj_Command =new
System.Data.OleDb.OleDbCommand();
GPC_obj_Command.CommandText = strSQLStatement;
GPC_obj_Command.CommandType=CommandType.StoredProcedure;
GPC_obj_Command.Connection = ActiveConnection;
for (int i=0; i < spParamName.GetLength(0); i++)
{
GPC_obj_Command.Parameters.Add(spParamName[i],spParamType[i]);
GPC_obj_Command.Parameters[i].Value=spParamValue[i];
}

GPC_obj_DataReader= GPC_obj_Command.ExecuteReader();
return GPC_obj_DataReader;
Adam Machanic
11/7/2004 2:09:07 AM
Why are you using the OleDbDataReader instead of the SqlDataReader?

Have you tried using the GetInt32 method instead of using the
objDataReader["Weight"]?

int temp = objDataReader.GetInt32(0);

Also, as a style note, you might consider getting rid of the 'obj' prefix --
Everything in .NET is an object. Repeating that fact in a variable name
gives you no additional information; it just increases the amount of typing
you need to do :)


--
Adam Machanic
SQL Server MVP
http://www.sqljunkies.com/weblog/amachanic
--


[quoted text, click to view]
objDataReader=GPC_obj_DataBase.fn_GetReader("usp_GetTotalWeight",ParamName,P
aramType,ParamValue);
[quoted text, click to view]

Dan Guzman
11/7/2004 9:38:13 AM
To add to Adam's response, it looks like your code is missing a call to the
data reader Read method. You might also consider using ExecuteScalar
instead of ExecuteReader for a single-row single-column result.

--
Hope this helps.

Dan Guzman
SQL Server MVP

[quoted text, click to view]

Dean Kim
11/7/2004 1:20:17 PM
Thanks for the hints, i've tried to change it, still got nothing...


*** Sent via Developersdex http://www.developersdex.com ***
Dean Kim
11/7/2004 9:40:33 PM
I got it,

Thanks to everyone

*** Sent via Developersdex http://www.developersdex.com ***
Dan Guzman
11/7/2004 9:46:01 PM
[quoted text, click to view]

Below is a working code snippet you might try. The code you originally
posted is incomplete so I can't determine the underlying cause of the
problem.

try
{
int OrderID = 0;
OleDbConnection connection = new OleDbConnection(
"Provider=SQLOLEDB;" +
"Server=MyServer;" +
"Initial Catalog=MyDatabase;" +
"Integrated Security=SSPI");
connection.Open();

OleDbCommand GPC_obj_Command = new
OleDbCommand("usp_GetTotalWeight", connection);
GPC_obj_Command.CommandType=CommandType.StoredProcedure;

OleDbParameter orderIdParameter = new OleDbParameter(
"OrderID",
OleDbType.Integer);
GPC_obj_Command.Parameters.Add(orderIdParameter);
orderIdParameter.Value = OrderID;

int temp = (int) GPC_obj_Command.ExecuteScalar();
Console.WriteLine(temp.ToString());
connection.Close();
}
catch (OdbcException ex)
{
Console.WriteLine(ex.ToString());
}

--
Hope this helps.

Dan Guzman
SQL Server MVP

[quoted text, click to view]

AddThis Social Bookmark Button