How to convert/cast sqldbtype.int

G

Guest

Hi all,
When I call a stored procedure, one of the output parameter returns a
sqldbtype.integer but when assigned to a C# int field/property I get this
error:

int myfield;
// say, parms[0] is the sql integer data
myfield = parms[0]; // creates the error.
Error 1 Cannot implicitly convert type 'System.Data.SqlClient.SqlParameter'
to 'int'

also, parms[0] is a nullable column in the database.
 
J

John B

Amil said:
Hi all,
When I call a stored procedure, one of the output parameter returns a
sqldbtype.integer but when assigned to a C# int field/property I get this
error:

int myfield;
// say, parms[0] is the sql integer data
myfield = parms[0]; // creates the error.
Error 1 Cannot implicitly convert type 'System.Data.SqlClient.SqlParameter'
to 'int'

also, parms[0] is a nullable column in the database.

Im not sure if you have to use the .Value property of the parameter but
I think you do, so:

if(parms[0].Value != dbnull) //Whatever dbnull is called
{
myfield = (int)parms[0].Value;
}

JB
 
G

Guest

cool. i think adding the .Value made it work; thank for the null checking too.

John B said:
Amil said:
Hi all,
When I call a stored procedure, one of the output parameter returns a
sqldbtype.integer but when assigned to a C# int field/property I get this
error:

int myfield;
// say, parms[0] is the sql integer data
myfield = parms[0]; // creates the error.
Error 1 Cannot implicitly convert type 'System.Data.SqlClient.SqlParameter'
to 'int'

also, parms[0] is a nullable column in the database.

Im not sure if you have to use the .Value property of the parameter but
I think you do, so:

if(parms[0].Value != dbnull) //Whatever dbnull is called
{
myfield = (int)parms[0].Value;
}

JB
 
I

Ignacio Machin \( .NET/ C# MVP \)

hi,

parms[0] is an instance of SqlParameter , of course you cannot cast it to
an int.
do this:
int i = Convert.ToInt32( parms[0].Value );
or
int i = (int) parms[0].Value ;


cheers,
 

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