DataReader

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!!!!!!!!!
 
M

Miha Markic

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.
 
B

bruce barker

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;
 
W

William \(Bill\) Vaughn

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.
__________________________________
 
J

Jim Heavey

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.
 
K

Kathleen Dollard

Jim,

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

Kathleen
 
K

Kathleen Dollard

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top