简体   繁体   中英

How do i convert Context.User.Identity.GetUserId() to int with c# in asp.net vs2015

How do I convert Context.User.Identity.GetUserId() to an int.

code:

string clientId = Context.User.Identity.GetUserId();
if (clientId != null) {
    cart NewInCart = new cart();
    NewInCart.ClientId = Convert.ToInt32(clientId);
}

Above code gives error stating:

when converting a string to DateTime, parse the string to take the date before putting each variable into the DateTime object.

我认为您想要的是:

NewInCart.ClientId = Int32.Parse(clientId);

Sorted for now.

I have decided to change the data type of ClientID in the dbo to nvarchar(50); that way I save like this:

string clientId = Context.User.Identity.GetUserId() ;
.......
cart.ClientId = clientId;

where cart.ClientId is now expecting a string. This sorted my problem and I no longer need to convert Context.User.Identity.GetUserId() to int.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM