views:

135

answers:

4

Hi,

I'm getting "InvalidCastException" (occurred in System.Data.Linq.dll) in my function:

public User GetUserByKey(Guid key)
{
            return usersTable.FirstOrDefault(m => m.UserKey == key);
}

which is called here:

MembershipUser mu = Membership.CreateUser(user.UserName, user.Password, user.Email, null, null, true, Guid.NewGuid(), out status);
User new_user = _UsersRepository.GetUserByKey((Guid)mu.ProviderUserKey);

mu.ProviderUserKey is the Guid object encapsulated in general object type so everything should be fine :/

Thanks for your help!

A: 

An invalid cast exception is almost always due to an incorrect mapping in the destination class (User here).

Check the stack trace to see what the destination type is, which should help you narrow down which properties to look at. Most likely something changed in the database schema and the Linq entity wasn't updated.

Edit - the error could also be happening when casting ProviderUserKey to a Guid - you say it's a Guid boxed as an object but that might not be the case (it might be null, for example). Have you tried stepping in with a debugger and seeing what the actual runtime type is?

Aaronaught
I'm mapping it as follows:[Column]public Guid UserKey { get; set; } and it's nvarchar(100) in database - is it correct !?
Kotu
Looks like I didn't read the comment properly the first time around. The answer is **no**, `nvarchar(len)` maps to `System.String` which **cannot** be cast to a `System.Guid`!
Aaronaught
A: 

I might be off base here but you are creating a MembershipUser and from your method you are returning User.

Is this by design because if the two don't match then you'll get a cast error.

Try returning a MembershipUser and see if that helps.

griegs
Yes, I'm creating user which is automatically being saved in Membership's CreateUser function. Later than I wish to assign values to some custom properties - that's why I'm trying to get it by guid. I know that it should be done probably by using profiles but don't have a time for it and it's not the point of my project.
Kotu
Probably not the issue since he's passing in the `ProviderUserKey` property of the MembershipUser object. That's where the mismatch is.
Ahmad Mageed
So, can I use ProviderUserKey as Guid object !? If now what should be pass to CreateUser instead of !?
Kotu
@Kotu, *what is the runtime type?* Don't ask us, step through the program in a debugger and find out for certain.
Aaronaught
A: 

Hey,

Quick and easy way to tell to figure it out; mouse over the field and ensure it's a guid. For safekeeping, you may want to consider using Guid.TryParse to safely pass the value if its a GUID, to prevent that error from happening. In the underlying database, it does use a string value, and I don't know if there is any in-built conversion, or if you need to convert using Guid.Parse or Guid.TryParse.

HTH.

Brian
+1  A: 

Since you mentioned it's a nvarchar(100) in your comment earlier try this:

Guid key = new Guid(mu.ProviderUserKey.ToString()); // object to string
User new_user = _UsersRepository.GetUserByKey(key);

Also, SQL Server has a uniqueidentifier data type to represent a GUID which you may consider using.

Ahmad Mageed
And that is a solution for my problem! When a set "uniqueidentifier" instead of "nvarchar(100)" everything works as it should. Thanks a lot Ahmad!
Kotu