473,386 Members | 1,793 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Output parameters undefined? Huh?

The following code does not operate properly, and neither I nor the three
..Net experts can figure out why. It's a seeming mystery. The code is simple.
A SqlCommand is executed against a stored procedure, then a DataReader is
looped through. After the DataReader is exhausted, I attempt to read some
output parameters from the SqlCommand. However, both the output parameters
return <undefined value>.

I understand that one cannot examine the contents of output parameters from
a SqlCommand until after the DataReader is exhausted, but after that, it
should be fine. The Stored Procedure, when run outside the application (from
within Sql Server), returns the correct values, so the fault lies somewhere
here.

(The local methods GetInputParam and GetOutputParam simply build
SqlParameter objects by setting the Direction, Name, Size if necessary, and
data type. They are used all throughout the application and do not pose
problems, unless perhaps some other property needs to be filled.
GetSqlCommand is another local method which creates a SqlCommand object and
assigns the SqlParameter array to it. Same story.)
SqlDataReader vDataReader;
SqlCommand vSqlCommand = null;

SqlParameter[] aryParams = new SqlParameter[3];
aryParams[0] = GetInputParam("@IncludeInactive",
SqlDbType.Bit);
aryParams[1] = GetOutputParam
("@ErrorMessage",SqlDbType.NVarChar, 512);
aryParams[2] = GetOutputParam ("@ReturnValue",
SqlDbType.Int);

vSqlCommand = GetSqlCommand("MyStoredProcedure",
aryParams);
vSqlCommand.Connection.Open();
vDataReader = vSqlCommand.ExecuteReader();

while (vDataReader.Read())
{
// code using parameters from the data set
residing in vDataReader (populating an object)
}

if
(vSqlCommand.Parameters["@ErrorMessage"].Value.ToString()!=""

||(int)vSqlCommand.Parameters["@ReturnValue"].Value == -1 )
{
throw new
CustomException(vSqlCommand.Parameters["@ErrorMessage"].Value.ToString(),
null);
}
}

--
- Jim Owen
Nov 17 '05 #1
2 2647
This would work fine if you use a DataSet instead of the datareader. That is
if its not a big perf implication as Datasets are more heavy and slow than
the data reader.

"Jim Owen" <jk********@comcast.net> wrote in message
news:eD****************@tk2msftngp13.phx.gbl...
The following code does not operate properly, and neither I nor the three
.Net experts can figure out why. It's a seeming mystery. The code is simple. A SqlCommand is executed against a stored procedure, then a DataReader is
looped through. After the DataReader is exhausted, I attempt to read some
output parameters from the SqlCommand. However, both the output parameters
return <undefined value>.

I understand that one cannot examine the contents of output parameters from a SqlCommand until after the DataReader is exhausted, but after that, it
should be fine. The Stored Procedure, when run outside the application (from within Sql Server), returns the correct values, so the fault lies somewhere here.

(The local methods GetInputParam and GetOutputParam simply build
SqlParameter objects by setting the Direction, Name, Size if necessary, and data type. They are used all throughout the application and do not pose
problems, unless perhaps some other property needs to be filled.
GetSqlCommand is another local method which creates a SqlCommand object and assigns the SqlParameter array to it. Same story.)
SqlDataReader vDataReader;
SqlCommand vSqlCommand = null;

SqlParameter[] aryParams = new SqlParameter[3];
aryParams[0] = GetInputParam("@IncludeInactive",
SqlDbType.Bit);
aryParams[1] = GetOutputParam
("@ErrorMessage",SqlDbType.NVarChar, 512);
aryParams[2] = GetOutputParam ("@ReturnValue",
SqlDbType.Int);

vSqlCommand = GetSqlCommand("MyStoredProcedure",
aryParams);
vSqlCommand.Connection.Open();
vDataReader = vSqlCommand.ExecuteReader();

while (vDataReader.Read())
{
// code using parameters from the data set
residing in vDataReader (populating an object)
}

if
(vSqlCommand.Parameters["@ErrorMessage"].Value.ToString()!=""

||(int)vSqlCommand.Parameters["@ReturnValue"].Value == -1 )
{
throw new
CustomException(vSqlCommand.Parameters["@ErrorMessage"].Value.ToString(),
null);
}
}

--
- Jim Owen

Nov 17 '05 #2
This would work fine if you use a DataSet instead of the datareader. That is
if its not a big perf implication as Datasets are more heavy and slow than
the data reader.

"Jim Owen" <jk********@comcast.net> wrote in message
news:eD****************@tk2msftngp13.phx.gbl...
The following code does not operate properly, and neither I nor the three
.Net experts can figure out why. It's a seeming mystery. The code is simple. A SqlCommand is executed against a stored procedure, then a DataReader is
looped through. After the DataReader is exhausted, I attempt to read some
output parameters from the SqlCommand. However, both the output parameters
return <undefined value>.

I understand that one cannot examine the contents of output parameters from a SqlCommand until after the DataReader is exhausted, but after that, it
should be fine. The Stored Procedure, when run outside the application (from within Sql Server), returns the correct values, so the fault lies somewhere here.

(The local methods GetInputParam and GetOutputParam simply build
SqlParameter objects by setting the Direction, Name, Size if necessary, and data type. They are used all throughout the application and do not pose
problems, unless perhaps some other property needs to be filled.
GetSqlCommand is another local method which creates a SqlCommand object and assigns the SqlParameter array to it. Same story.)
SqlDataReader vDataReader;
SqlCommand vSqlCommand = null;

SqlParameter[] aryParams = new SqlParameter[3];
aryParams[0] = GetInputParam("@IncludeInactive",
SqlDbType.Bit);
aryParams[1] = GetOutputParam
("@ErrorMessage",SqlDbType.NVarChar, 512);
aryParams[2] = GetOutputParam ("@ReturnValue",
SqlDbType.Int);

vSqlCommand = GetSqlCommand("MyStoredProcedure",
aryParams);
vSqlCommand.Connection.Open();
vDataReader = vSqlCommand.ExecuteReader();

while (vDataReader.Read())
{
// code using parameters from the data set
residing in vDataReader (populating an object)
}

if
(vSqlCommand.Parameters["@ErrorMessage"].Value.ToString()!=""

||(int)vSqlCommand.Parameters["@ReturnValue"].Value == -1 )
{
throw new
CustomException(vSqlCommand.Parameters["@ErrorMessage"].Value.ToString(),
null);
}
}

--
- Jim Owen

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bari Allen | last post by:
I have a Stored procedure in SQL, that works, when tested in SQL, with one input & several output parameters, as follows: CREATE PROCEDURE myProcedure @MyID int , @First varchar(80) OUTPUT ,...
11
by: Andrew Thompson | last post by:
I have written a few scripts to parse the URL arguments and either list them or allow access to the value of any parameter by name. <http://www.physci.org/test/003url/index.html>...
5
by: vivienne.netherwood | last post by:
I am developing an Access Project front end with a SQL server database. I have written a stored procedure that returns a record set and also a value via an output parameter. The procedure is as...
12
by: unique | last post by:
I have written some programs in c lang yet but today I get confused with output i get. I have function educate(..) which i call in main program this way: J = educate(no_layers, no_neurons,...
0
by: Jim Owen | last post by:
The following code does not operate properly, and neither I nor the three ..Net experts can figure out why. It's a seeming mystery. The code is simple. A SqlCommand is executed against a stored...
2
by: Bari Allen | last post by:
ASP Classic question: I have a Stored procedure in SQL, that works, when tested in SQL, with one input & several output parameters, as follows: CREATE PROCEDURE myProcedure @MyID int , @First...
1
by: Garth Wells | last post by:
Using an example in the Jan 2006 release of the Enterprise Library, I came up with the code shown below to create a DAL method for returning several columns of a single row. I place the output...
7
by: Aaron Gray | last post by:
I put together the following code to get the href's parameters :- function GetParameters() { var arg = new Object(); var href = document.location.href; if ( href.indexOf( "?") != -1) { var...
2
by: gabosom | last post by:
Hi! I've been breaking my head trying to get the output variables from my Stored Procedure. This is my SP code CREATE PROCEDURE GetKitchenOrderDetail( @idService int, --outPut Variables ...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.