DataReader

  • Thread starter Thread starter Jim Heavey
  • Start date Start date
J

Jim Heavey

Hello, I am transitioning from VB to C# and I can't tell if my problem is
to do with the format of my command or something else.

I am getting an error of "Object reference not set to an instance of the
object" when it hits this line of code..

int rtnValue = (int) cmd.Parameters["@RETURN_VALUE"].Value;
I set my Parameter up as follows:

cmd.Parameters.Add("@RETURN_VALUE",SqlDbType.Int).Direction =
ParameterDirection.ReturnValue ;


I used a "foreach" to loop through each of my parms, with the @RETURN_VALUE
being the last one. Each time this loop generates the same error when it
gets to the "@RETURN_VALUE". I do not see anything wrong with how I have
set it up. Does the procedure that I am executing "HAVE TO" use the
"return statement in order to properly populate the parm?

Any ideas?

Thanks inadvance for your assistance!!!!!!!!!
 
Hi Jim,


Jim Heavey said:
Hello, I am transitioning from VB to C# and I can't tell if my problem is
to do with the format of my command or something else.

I am getting an error of "Object reference not set to an instance of the
object" when it hits this line of code..

int rtnValue = (int) cmd.Parameters["@RETURN_VALUE"].Value;


Your return value is null that's why you are geting the error

I set my Parameter up as follows:

cmd.Parameters.Add("@RETURN_VALUE",SqlDbType.Int).Direction =
ParameterDirection.ReturnValue ;


I used a "foreach" to loop through each of my parms, with the @RETURN_VALUE
being the last one. Each time this loop generates the same error when it
gets to the "@RETURN_VALUE". I do not see anything wrong with how I have
set it up. Does the procedure that I am executing "HAVE TO" use the
"return statement in order to properly populate the parm?

What sql code are you invoking?
The return value will work only for stored procedure actually returning
something.
 
you can fetch the return value until you have processed all rows and result
sets, or you close the datareader. this is because sqlserver does not send
back the return value until after its sent all result sets.

do
{
while (dr.Read())
;
} rs.NextResult();

int rtnValue = (int) cmd.Parameters["@RETURN_VALUE"].Value;
 
And the reason you're "transitioning to C#" is?

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
MVP, hRD
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
And the reason you're "transitioning to C#" is?

I want to say I have the ability to code in either VB or C#. Seems to be a
definite bias in salaries toward the C# folks.
 
Jim,

It doesn't take much thought to see the falacies in at least the VSM
statistics.

Kathleen
 
Jim,

I am guessing the underlying problem is that C# does not initialize
variables where VB.NET does. Even if that's not your immediate problem, its
one of a handful of pretty serious gotchas. The framerwork usage is the
same, but each language interacts with you, the developer quite diferently.

Kathleen
 
Back
Top