473,382 Members | 1,736 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,382 software developers and data experts.

Can't get my output parameter since it's not an object

Hi,

On the line where I'm assigning RecordCount to be the value of my output
parameter, I'm getting the generic "Object reference not set to an instance
of an object" error. I've isolated it down to this line, as the line of
code commented out just beneath it runs fine. Can anyone see why my
parameter isn't an object? When I grab the command from SQL profiler and
run it in Query Analyzer, I get my output parameter returned with a non-null
value.

Thank you

using (SqlConnection objConn = new SqlConnection(clsData.GetConnStr()))
{
SqlCommand objCmd = new SqlCommand("search", objConn);
objCmd .CommandType = CommandType.StoredProcedure;
//some input parameters defined here

SqlParameter reccount= objCmd.Parameters.Add("@reccount",
SqlDbType.Int);
reccount.Direction = ParameterDirection.Output;

objConn.Open();
ret = oCmd.ExecuteReader(CommandBehavior.CloseConnection );

RecordCount = (int)reccount.Value;
//RecordCount = 5; //no error with that
//etc.
}
Nov 19 '05 #1
8 1986
Patreek:
normally null reference error are pretty easy to spot, but this one has me a
little baffled (though I'm still sure it's something basic).

Normally it would mean that if you do something like:

SqlCommand command;
command.CommandType = CommandType.StoredProcedure;

you would get an error because you are never creating an instance (via new
in this case) of SqlCommand such as SqlCommand command = new SqlCommand();

However, it seems unlikely that reccount is null on the line that you
suspect, because a couple lines up from it, you are successfully accessing
it via reccount.Direction = ParameterDirection.Output;
If you've typed thsis out exactly, I'd point out that at one place you use
objCmd and at the other you've used oCmd

I can tell you that other than this, I've taken your code and made it work
perfectly. Perhaps you could debug and step through your code to double
check the exact location of the error.

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/

"Patreek" <do**@spam.me> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Hi,

On the line where I'm assigning RecordCount to be the value of my output
parameter, I'm getting the generic "Object reference not set to an
instance of an object" error. I've isolated it down to this line, as the
line of code commented out just beneath it runs fine. Can anyone see why
my parameter isn't an object? When I grab the command from SQL profiler
and run it in Query Analyzer, I get my output parameter returned with a
non-null value.

Thank you

using (SqlConnection objConn = new SqlConnection(clsData.GetConnStr()))
{
SqlCommand objCmd = new SqlCommand("search", objConn);
objCmd .CommandType = CommandType.StoredProcedure;
//some input parameters defined here

SqlParameter reccount= objCmd.Parameters.Add("@reccount",
SqlDbType.Int);
reccount.Direction = ParameterDirection.Output;

objConn.Open();
ret = oCmd.ExecuteReader(CommandBehavior.CloseConnection );

RecordCount = (int)reccount.Value;
//RecordCount = 5; //no error with that
//etc.
}

Nov 19 '05 #2
Hi Patreek,

Since your oCmd to execute a datareasder, only after the datareader closing,
you can get output paramter value. Try

ret.Close();
RecordCount = (int)reccount.Value;
HTH

Elton Wang
"Patreek" wrote:
Hi,

On the line where I'm assigning RecordCount to be the value of my output
parameter, I'm getting the generic "Object reference not set to an instance
of an object" error. I've isolated it down to this line, as the line of
code commented out just beneath it runs fine. Can anyone see why my
parameter isn't an object? When I grab the command from SQL profiler and
run it in Query Analyzer, I get my output parameter returned with a non-null
value.

Thank you

using (SqlConnection objConn = new SqlConnection(clsData.GetConnStr()))
{
SqlCommand objCmd = new SqlCommand("search", objConn);
objCmd .CommandType = CommandType.StoredProcedure;
//some input parameters defined here

SqlParameter reccount= objCmd.Parameters.Add("@reccount",
SqlDbType.Int);
reccount.Direction = ParameterDirection.Output;

objConn.Open();
ret = oCmd.ExecuteReader(CommandBehavior.CloseConnection );

RecordCount = (int)reccount.Value;
//RecordCount = 5; //no error with that
//etc.
}

Nov 19 '05 #3
sql returns output parameters and the return value after all result sets
have been sent back to the client. as you are using a reader, you need to
read all rows and results sets before you can access them.

try the following:

using (SqlConnection objConn = new SqlConnection(clsData.GetConnStr()))
{
SqlCommand objCmd = new SqlCommand("search", objConn);
objCmd .CommandType = CommandType.StoredProcedure;
//some input parameters defined here

SqlParameter reccount= objCmd.Parameters.Add("@reccount",
SqlDbType.Int);
reccount.Direction = ParameterDirection.Output;

objConn.Open();
ret = oCmd.ExecuteReader(CommandBehavior.CloseConnection );

do
{
while (ret.Read())
;
} while (ret.NextResult())

RecordCount = (int)reccount.Value;
//RecordCount = 5; //no error with that
//etc.
}

i realize you expected to get the count before processing the rows, but if
you think about it, it makes sense. sqlserver does not know the count until
its sent all the rows back to the client. if you really need the count
first, then you need two result sets in your search proc, the first one
should return the count, the second one the search rows.

-- bruce (sqlwork.com)

"Patreek" <do**@spam.me> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Hi,

On the line where I'm assigning RecordCount to be the value of my output
parameter, I'm getting the generic "Object reference not set to an
instance of an object" error. I've isolated it down to this line, as the
line of code commented out just beneath it runs fine. Can anyone see why
my parameter isn't an object? When I grab the command from SQL profiler
and run it in Query Analyzer, I get my output parameter returned with a
non-null value.

Thank you

using (SqlConnection objConn = new SqlConnection(clsData.GetConnStr()))
{
SqlCommand objCmd = new SqlCommand("search", objConn);
objCmd .CommandType = CommandType.StoredProcedure;
//some input parameters defined here

SqlParameter reccount= objCmd.Parameters.Add("@reccount",
SqlDbType.Int);
reccount.Direction = ParameterDirection.Output;

objConn.Open();
ret = oCmd.ExecuteReader(CommandBehavior.CloseConnection );

RecordCount = (int)reccount.Value;
//RecordCount = 5; //no error with that
//etc.
}

Nov 19 '05 #4
I don't really understand why, but that worked. Thank you Elton. Of
course, this creates a new problem as I'm trying to return a SqlDataReader
from my function, but if I have to close it to get the output parameter
value, that renders the datareader pretty useless.

Is the purpose of .Net to make it impossible to actually separate out your
data class? It sure seems that way.

Thanks,

Patreek

"Elton W" <El****@discussions.microsoft.com> wrote in message
news:AC**********************************@microsof t.com...
Hi Patreek,

Since your oCmd to execute a datareasder, only after the datareader
closing,
you can get output paramter value. Try

ret.Close();
RecordCount = (int)reccount.Value;
HTH

Elton Wang
"Patreek" wrote:
Hi,

On the line where I'm assigning RecordCount to be the value of my output
parameter, I'm getting the generic "Object reference not set to an
instance
of an object" error. I've isolated it down to this line, as the line of
code commented out just beneath it runs fine. Can anyone see why my
parameter isn't an object? When I grab the command from SQL profiler and
run it in Query Analyzer, I get my output parameter returned with a
non-null
value.

Thank you

using (SqlConnection objConn = new
SqlConnection(clsData.GetConnStr()))
{
SqlCommand objCmd = new SqlCommand("search", objConn);
objCmd .CommandType = CommandType.StoredProcedure;
//some input parameters defined here

SqlParameter reccount= objCmd.Parameters.Add("@reccount",
SqlDbType.Int);
reccount.Direction = ParameterDirection.Output;

objConn.Open();
ret = oCmd.ExecuteReader(CommandBehavior.CloseConnection );

RecordCount = (int)reccount.Value;
//RecordCount = 5; //no error with that
//etc.
}

Nov 19 '05 #5
Hi Karl,

I tried to trim up the code before posting it. Here is some test code that
I'm using that does work, however.

private void Page_Load(object sender, System.EventArgs e)
{
SqlDataReader ret;
SqlConnection objConn = new SqlConnection(clsData.GetConnStr());
SqlCommand objCmd = new SqlCommand("TestProc", objConn);
objCmd.CommandType = CommandType.StoredProcedure;
SqlParameter testParam = objCmd.Parameters.Add("@TestOutput",
SqlDbType.VarChar, 50);
testParam.Direction = ParameterDirection.Output;
objConn.Open();
ret = objCmd.ExecuteReader(CommandBehavior.CloseConnecti on);

ret.Close();

Response.Write(testParam.Value.ToString());
objConn.Close();
}
And the proc:

CREATE PROC TestProc (@TestOutput varchar(50) output) AS
SELECT 'whatever'
select @TestOutput = 'the output value'
GO

The problem with this is that I have to CLOSE the datareader to get the
output parameter. That's not helpful since I was trying to return the
datareader from my function so that I could bind it to a dropdown in a box.
Why am I having such a hard time trying to do this "create a separate data
layer" thing? If I just stuck the database code in my codebehind files
instead of trying this data class stuff, all this would work fine. But I'm
trying to do things the "right" way. Trying.

Thanks

"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:O4**************@TK2MSFTNGP14.phx.gbl...
Patreek:
normally null reference error are pretty easy to spot, but this one has me
a little baffled (though I'm still sure it's something basic).

Normally it would mean that if you do something like:

SqlCommand command;
command.CommandType = CommandType.StoredProcedure;

you would get an error because you are never creating an instance (via new
in this case) of SqlCommand such as SqlCommand command = new SqlCommand();

However, it seems unlikely that reccount is null on the line that you
suspect, because a couple lines up from it, you are successfully accessing
it via reccount.Direction = ParameterDirection.Output;
If you've typed thsis out exactly, I'd point out that at one place you use
objCmd and at the other you've used oCmd

I can tell you that other than this, I've taken your code and made it work
perfectly. Perhaps you could debug and step through your code to double
check the exact location of the error.

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/

"Patreek" <do**@spam.me> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Hi,

On the line where I'm assigning RecordCount to be the value of my output
parameter, I'm getting the generic "Object reference not set to an
instance of an object" error. I've isolated it down to this line, as the
line of code commented out just beneath it runs fine. Can anyone see why
my parameter isn't an object? When I grab the command from SQL profiler
and run it in Query Analyzer, I get my output parameter returned with a
non-null value.

Thank you

using (SqlConnection objConn = new SqlConnection(clsData.GetConnStr()))
{
SqlCommand objCmd = new SqlCommand("search", objConn);
objCmd .CommandType = CommandType.StoredProcedure;
//some input parameters defined here

SqlParameter reccount= objCmd.Parameters.Add("@reccount",
SqlDbType.Int);
reccount.Direction = ParameterDirection.Output;

objConn.Open();
ret = oCmd.ExecuteReader(CommandBehavior.CloseConnection );

RecordCount = (int)reccount.Value;
//RecordCount = 5; //no error with that
//etc.
}


Nov 19 '05 #6

"Bruce Barker" <br******************@safeco.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
sql returns output parameters and the return value after all result sets i realize you expected to get the count before processing the rows, but if
you think about it, it makes sense. sqlserver does not know the count
until its sent all the rows back to the client. if you really need the
count first, then you need two result sets in your search proc, the first
one should return the count, the second one the search rows.

Hi Bruce,

If I were using a recordcount property of the datareader object (I'm
assuming there is one), I'd expect not to be able to get the recordcount
until after the rows are read, perhaps. But that's not really what I'm
doing. I'm just trying to select an output parameter value. That value
just so happens to be called "reccount" in this case, but it could be
something totally unrelated to the data. Example:

CREATE PROC TestProc (@TestOutput varchar(50) output) AS
SELECT 'whatever'
select @TestOutput = 'the output value'
GO

I can't get the value of the output parameter there until after I close the
reader. I don't really understand why that is.

--
Pat
Nov 19 '05 #7
Hi Patreek,

While the SqlDataReader is in use, the associated SqlConnection is busy
serving the SqlDataReader, and no other operations can be performed on the
SqlConnection other than closing it. This is the case until the Close method
of the SqlDataReader is called. For example, you cannot retrieve output
parameters until after you call Close.

In order to get count before getting actual records, you can either execute
query Select Count(*) From table_name, first then run actual query to a
datareader, or you can use dataadapter to fill query result to a datatable.
So datatable.Rows.Count gives you the count of records.

HTH

Elton

"Patreek" wrote:
I don't really understand why, but that worked. Thank you Elton. Of
course, this creates a new problem as I'm trying to return a SqlDataReader
from my function, but if I have to close it to get the output parameter
value, that renders the datareader pretty useless.

Is the purpose of .Net to make it impossible to actually separate out your
data class? It sure seems that way.

Thanks,

Patreek

"Elton W" <El****@discussions.microsoft.com> wrote in message
news:AC**********************************@microsof t.com...
Hi Patreek,

Since your oCmd to execute a datareasder, only after the datareader
closing,
you can get output paramter value. Try

ret.Close();
RecordCount = (int)reccount.Value;
HTH

Elton Wang
"Patreek" wrote:
Hi,

On the line where I'm assigning RecordCount to be the value of my output
parameter, I'm getting the generic "Object reference not set to an
instance
of an object" error. I've isolated it down to this line, as the line of
code commented out just beneath it runs fine. Can anyone see why my
parameter isn't an object? When I grab the command from SQL profiler and
run it in Query Analyzer, I get my output parameter returned with a
non-null
value.

Thank you

using (SqlConnection objConn = new
SqlConnection(clsData.GetConnStr()))
{
SqlCommand objCmd = new SqlCommand("search", objConn);
objCmd .CommandType = CommandType.StoredProcedure;
//some input parameters defined here

SqlParameter reccount= objCmd.Parameters.Add("@reccount",
SqlDbType.Int);
reccount.Direction = ParameterDirection.Output;

objConn.Open();
ret = oCmd.ExecuteReader(CommandBehavior.CloseConnection );

RecordCount = (int)reccount.Value;
//RecordCount = 5; //no error with that
//etc.
}


Nov 19 '05 #8

"Elton W" <El****@discussions.microsoft.com> wrote in message
news:FC**********************************@microsof t.com...
Hi Patreek,

While the SqlDataReader is in use, the associated SqlConnection is busy
serving the SqlDataReader, and no other operations can be performed on the
SqlConnection other than closing it. This is the case until the Close
method
of the SqlDataReader is called. For example, you cannot retrieve output
parameters until after you call Close.
That explains it. Thanks! I don't like that I can't get to the output
parameters, but it is what it is.

In order to get count before getting actual records, you can either
execute
query Select Count(*) From table_name, first then run actual query to a
datareader, or you can use dataadapter to fill query result to a
datatable.
So datatable.Rows.Count gives you the count of records.


Getting the count isn't the issue, but thank you anyway.
Nov 19 '05 #9

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

Similar topics

4
by: Steven | last post by:
I'm calling a stored procedure which has an output parameter of type int. Once the stored procedure is executed, I want to check the value of the parameter in case it is null. However, when the a...
6
by: surrealtrauma | last post by:
i have a trouble about that: i want to ask user to enter the employee data (employee no., name, worked hour, etc.), but i dont know how to sort the data related to a particular employee as a...
1
by: chris | last post by:
Hi there, I try to write out an oracle parameter from a stored procedure, but only receive the name of the parameter. Here is what I do: cmd.Parameters.Add(new OracleParameter("confidential",...
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
8
by: jbonifacejr | last post by:
Hi. I'm sorry to bother all of you, but I have spent two days looking at code samples all over the internet, and I can not get a single one of them to work for me. I am simply trying to get a value...
1
by: John Bailo | last post by:
This is a my solution to getting an Output parameter from a SqlDataSource. I have seen a few scant articles but none of them take it all the way to a solution. Hopefully this will help some...
10
by: Mike | last post by:
Sql Server, Scope_Identity(), Ado.NET: Which is better? Using an output parameter to return Scope_Identity through ExecuteNonQuery(), or adding Select Scope_Identity() to the end of the...
1
by: rogerford | last post by:
Hi, I am trying to retrieve a value from database, based on that value I want to insert records into DB.Let’s say I am retrieving tsmid which serves as the output parameter in the stored procedure....
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 ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.