Retrieving SqlCommand parameter values

  • Thread starter Thread starter Dmitri Khanine
  • Start date Start date
D

Dmitri Khanine

Hi All

Trying to retrieve the value of the Sqlcommand' parameter as integer.
Here is how I declare it:


SqlParameter paramId = command.Parameters.Add("@id", SqlDbType.Int);
paramId.Direction = ParameterDirection.InputOutput;
paramId.Value = id;

......

The only method of the SqlParameter available is ToString() so I have
to retrieve it in the following ugly way:

id = Int64.Parse( "0" + paramId.Value.ToString() );

Anyone knows of a prettier way to do it?


Thanks very much!!!
 
Dmitri,

The Value property is of type object. However, that doesn't mean that
it is an object that is stored. Since every type derives from object, it is
the only type the property can expose which will cover every possible value
returned.

In order to access your property, cast the result of the Value property
to int and then you should be able to access it normally.

Hope this helps.
 
Dmitri Khanine said:
Trying to retrieve the value of the Sqlcommand' parameter as integer.
Here is how I declare it:

SqlParameter paramId = command.Parameters.Add("@id", SqlDbType.Int);
paramId.Direction = ParameterDirection.InputOutput;
paramId.Value = id;

.....

The only method of the SqlParameter available is ToString() so I have
to retrieve it in the following ugly way:

id = Int64.Parse( "0" + paramId.Value.ToString() );

Anyone knows of a prettier way to do it?

First check whether or not it's null (DBNull.Value) and otherwise, cast
it to int:

if (paramId.Value != DBNull.Value)
{
int id = (int) paramId.Value;
...
}

Alternatively:

if (paramId.Value is int)
{
int id = (int) paramId.Value;
...
}
 
Back
Top