views:

103

answers:

1

The data layer of my ASP.Net app calls a stored proc to get a small (one record) amount of information about a visitor upon login. I pass in their phone number and the sproc, using a simple SELECT, passes back 5 fields, the first of which is the primary key, a BIGINT. My data layer gets the DataRow and tries to create a data object with it. In the data object, the property that represents the primary key is an Int64. That looks like this:

 sub = new PersistentSubscriber((String) dr["UserFirstName"],
        (String) dr["UserLastName"],
        phoneNumber, (Int64) dr["UserId"], (Byte) dr["SubscriberStatus"]);

What I'm finding is that everything works great when I have a big primary key value, such as 88698. However, when I get a small one, like 999, I get a "Specified Cast is Invalid" error when trying to set up that primary key property. When I try playing with it in the Immediate window, I get this:

?(Int64)dr["UserId"]
Cannot unbox 'dr["UserId"]' as a 'long'
?(int)dr["UserId"]
999
?(Int32)dr["UserId"]
999

Without resorting to a typed dataset, what am I doing wrong, here?

+1  A: 

Try giving Int64.TryParse a shot.

long userId;
if(Int64.TryParse(dr["UserId"], out userId))
{
    // successful conversion
}
theprise