470,631 Members | 1,621 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,631 developers. It's quick & easy.

Retrieving SqlCommand parameter values

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!!!
Nov 16 '05 #1
2 5034
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dmitri Khanine" <kh*****@hotmail.com> wrote in message
news:d4**************************@posting.google.c om...
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!!!

Nov 16 '05 #2
Dmitri Khanine <kh*****@hotmail.com> wrote:
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;
...
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Justin | last post: by
9 posts views Thread by fniles | last post: by
5 posts views Thread by Sanjay Pais | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.