tags:

views:

167

answers:

6

Hi, might be a kiddy, biddy question but

I was trying to pass an "output" parameter to a Stored Procedure using C Sharp and was getting an error until I passed the "output" variable using following syntax:

int? ROWID = 0;
ADAPTER.INSERT(val1, val2, ref ROWID);

Though problem is solved, I just can't understand why, when I try to hold the value in a normal "int" returned by the stored proc it gives a conversion error

int result = ROWID; // not correct

So, what I have to do is:

int result = (int)ROWID; // correct

What exactly does "int?" mean?

+9  A: 

int? is a nullable int. Read Using Nullable Types (C# Programming Guide).

By the way, if you don't read the whole guide, at least check out the HasValue and Value properties on your ROWID variable.

jball
+1 for an accurate (at least right now) link.
No Refunds No Returns
+1  A: 

int? is a nullable integer. See the MSDN page on Nullable Types for details.

Phil Ross
+5  A: 

int? is a nullable int (system.nullable), it can accept the value null.

Best way to convert is to null coallesce:

int result = rowid ?? 0;
Paul Creasey
"Best"? Not always. Sometimes it is safe to do this, but other times it is better to handle NULL and 0 differently.
Mark Byers
I'd argue that it is the best, but often collescing to a value such as -1 or int.max is better, but it's still the same solution.
Paul Creasey
+1  A: 

It's a nullable type.

Read more about them here:

Nullable Types

Banang
+6  A: 

int? is the same as Nullable<int>, and means that the value can either be an integer, or the value null.

It's probably because your database column is set to NULL, instead of NOT NULL. If there can actually be null values in your database, your code will fail with an exception on the cast. If there can't be nulls in the database then you should change the schema definition for that column not NOT NULL so that it maps to int instead of int?.

Mark Byers
Thanks, Mark. The value I am trying to return is "ID" if the row, auto generated, primary key, selected after the new row is inserted using SCOPE_IDENTITY(). Column is not set to accept null as you can understand, being primary key. but result may be null if operation is not successfully ??
Asad Butt
Primary keys should not usually be nullable. Typically if an insertion fails you will get an exception, not a null, although this does of course depend on what API you are using.
Mark Byers
It is not nullable, But returning result from stored proc could be.
Asad Butt
Then either that's an error in the stored procedure and you should fix it, or probably the stored precodure returns null in order to tell you that an error occurred, and you should first check for null and handle that error before you try to make the cast. If you don't do this, you will get an exception when you try to cast to int.
Mark Byers
Thanks mate, do appreciate
Asad Butt
A minor correction to my previous post - I hope the meaning was clear before, but just to clarify: If you don't do this, *and the stored procedure returns null*, you will get an exception when you try to cast to int.
Mark Byers
A: 

I would suggest this:

        int? ROWID = 0;
        ........
        int result = ROWID.HasValue ? ROWID.Value : 0;
Amby