开发者

Convert Session to Guid

开发者 https://www.devze.com 2023-03-16 04:11 出处:网络
I am building a User Management section to a website CMS. The user has a list of users and then clicks the edit button, the system then stored the UserId in a session and goes to the editUser.aspx pa

I am building a User Management section to a website CMS.

The user has a list of users and then clicks the edit button, the system then stored the UserId in a session and goes to the editUser.aspx page which will show the users details.

To get the users details I need to开发者_JAVA技巧 convert the UserId session to a Guid so i can get user details.

I keep getting error message:

System.InvalidCastException: Specified cast is not valid.

Dim selectedUserId As Guid = CType(Session("strUserId"), Guid)
Dim mu As MembershipUser = Membership.GetUser(selectedUserId)

Does anyone know if this is possible?

Thanks in advance.


Use Guid.Parse() instead. You are storing a string, so must convert from a string back to a Guid.


Is the strUserId a GUID value or a username?

If it is a GUID value, you cannot cast it but you can create a new GUID object using new GUID(strUserId).

If it is a username, your last line mu = Membership.GetUser(strUserId) should work without the cast.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号