473,769 Members | 6,404 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(c lsData.GetConnS tr()))
{
SqlCommand objCmd = new SqlCommand("sea rch", objConn);
objCmd .CommandType = CommandType.Sto redProcedure;
//some input parameters defined here

SqlParameter reccount= objCmd.Paramete rs.Add("@reccou nt",
SqlDbType.Int);
reccount.Direct ion = ParameterDirect ion.Output;

objConn.Open();
ret = oCmd.ExecuteRea der(CommandBeha vior.CloseConne ction);

RecordCount = (int)reccount.V alue;
//RecordCount = 5; //no error with that
//etc.
}
Nov 19 '05 #1
8 2012
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.Command Type = CommandType.Sto redProcedure;

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.Direct ion = ParameterDirect ion.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******** ********@TK2MSF TNGP14.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(c lsData.GetConnS tr()))
{
SqlCommand objCmd = new SqlCommand("sea rch", objConn);
objCmd .CommandType = CommandType.Sto redProcedure;
//some input parameters defined here

SqlParameter reccount= objCmd.Paramete rs.Add("@reccou nt",
SqlDbType.Int);
reccount.Direct ion = ParameterDirect ion.Output;

objConn.Open();
ret = oCmd.ExecuteRea der(CommandBeha vior.CloseConne ction);

RecordCount = (int)reccount.V alue;
//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.V alue;
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(c lsData.GetConnS tr()))
{
SqlCommand objCmd = new SqlCommand("sea rch", objConn);
objCmd .CommandType = CommandType.Sto redProcedure;
//some input parameters defined here

SqlParameter reccount= objCmd.Paramete rs.Add("@reccou nt",
SqlDbType.Int);
reccount.Direct ion = ParameterDirect ion.Output;

objConn.Open();
ret = oCmd.ExecuteRea der(CommandBeha vior.CloseConne ction);

RecordCount = (int)reccount.V alue;
//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(c lsData.GetConnS tr()))
{
SqlCommand objCmd = new SqlCommand("sea rch", objConn);
objCmd .CommandType = CommandType.Sto redProcedure;
//some input parameters defined here

SqlParameter reccount= objCmd.Paramete rs.Add("@reccou nt",
SqlDbType.Int);
reccount.Direct ion = ParameterDirect ion.Output;

objConn.Open();
ret = oCmd.ExecuteRea der(CommandBeha vior.CloseConne ction);

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

RecordCount = (int)reccount.V alue;
//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******** ********@TK2MSF TNGP14.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(c lsData.GetConnS tr()))
{
SqlCommand objCmd = new SqlCommand("sea rch", objConn);
objCmd .CommandType = CommandType.Sto redProcedure;
//some input parameters defined here

SqlParameter reccount= objCmd.Paramete rs.Add("@reccou nt",
SqlDbType.Int);
reccount.Direct ion = ParameterDirect ion.Output;

objConn.Open();
ret = oCmd.ExecuteRea der(CommandBeha vior.CloseConne ction);

RecordCount = (int)reccount.V alue;
//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****@discuss ions.microsoft. com> wrote in message
news:AC******** *************** ***********@mic rosoft.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.V alue;
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(c lsData.GetConnS tr()))
{
SqlCommand objCmd = new SqlCommand("sea rch", objConn);
objCmd .CommandType = CommandType.Sto redProcedure;
//some input parameters defined here

SqlParameter reccount= objCmd.Paramete rs.Add("@reccou nt",
SqlDbType.Int);
reccount.Direct ion = ParameterDirect ion.Output;

objConn.Open();
ret = oCmd.ExecuteRea der(CommandBeha vior.CloseConne ction);

RecordCount = (int)reccount.V alue;
//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(objec t sender, System.EventArg s e)
{
SqlDataReader ret;
SqlConnection objConn = new SqlConnection(c lsData.GetConnS tr());
SqlCommand objCmd = new SqlCommand("Tes tProc", objConn);
objCmd.CommandT ype = CommandType.Sto redProcedure;
SqlParameter testParam = objCmd.Paramete rs.Add("@TestOu tput",
SqlDbType.VarCh ar, 50);
testParam.Direc tion = ParameterDirect ion.Output;
objConn.Open();
ret = objCmd.ExecuteR eader(CommandBe havior.CloseCon nection);

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******** ******@TK2MSFTN GP14.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.Command Type = CommandType.Sto redProcedure;

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.Direct ion = ParameterDirect ion.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******** ********@TK2MSF TNGP14.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(c lsData.GetConnS tr()))
{
SqlCommand objCmd = new SqlCommand("sea rch", objConn);
objCmd .CommandType = CommandType.Sto redProcedure;
//some input parameters defined here

SqlParameter reccount= objCmd.Paramete rs.Add("@reccou nt",
SqlDbType.Int);
reccount.Direct ion = ParameterDirect ion.Output;

objConn.Open();
ret = oCmd.ExecuteRea der(CommandBeha vior.CloseConne ction);

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


Nov 19 '05 #6

"Bruce Barker" <br************ ******@safeco.c om> wrote in message
news:%2******** ********@TK2MSF TNGP12.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****@discuss ions.microsoft. com> wrote in message
news:AC******** *************** ***********@mic rosoft.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.V alue;
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(c lsData.GetConnS tr()))
{
SqlCommand objCmd = new SqlCommand("sea rch", objConn);
objCmd .CommandType = CommandType.Sto redProcedure;
//some input parameters defined here

SqlParameter reccount= objCmd.Paramete rs.Add("@reccou nt",
SqlDbType.Int);
reccount.Direct ion = ParameterDirect ion.Output;

objConn.Open();
ret = oCmd.ExecuteRea der(CommandBeha vior.CloseConne ction);

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


Nov 19 '05 #8

"Elton W" <El****@discuss ions.microsoft. com> wrote in message
news:FC******** *************** ***********@mic rosoft.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
2190
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 null value is returned I don't seem to be able to detect it. Any help would be greatly appreciated. C# code is as follows: SqlCommand cmd = new SqlCommand("sp_GetApplicationID", conn);
6
2372
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 group. i want to use a array object in the class but i don't know how..i am just learning the c++. So i dont know how to use class. in fact, i have writen like the following: class employee { public: employee();
1
1349
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", OracleType.Number)).Direction = ParameterDirection.Output; then i just try this: string strPageAccess = cmd.Parameters.Value.ToString();
6
4900
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 the html page controls the form fields that are required. It doesn't function like it's supposed to and I can leave all the fields blank and it still submits the form. Also I can't get it to transfer the file in the upload section. The file name...
8
3245
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 returned to the ASP from a stored procedure. The error I am getting is: Item can not be found in the collection corresponding to the requested name or ordinal. Here is my Stored Procedure code.
1
12323
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 poor soul. Situation: I want to do a lookup using a stored procedure for each value in a Row within a GridView. I use a lookup function in my code behind, evaluating the necessary bound fields. The problem is the SqlDataSource representing...
10
4042
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 procedure or ad hoc SQL and using ExecuteScalar()? Thanks.
1
3690
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. .net/C# code is public void getTSmid() { int tsid=0; tsid = System.Convert.ToInt32(SqlHelper.ExecuteReader(SqlHelper.tConnectionString, CommandType.StoredProcedure, "GetTSMID_SP",
2
3379
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 @idUser int OUTPUT,
0
9587
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9993
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8870
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7406
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3958
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.