Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在asp.net vs2015中将Context.User.Identity.GetUserId()转换为带c#的int_C#_Asp.net_.net - Fatal编程技术网

如何在asp.net vs2015中将Context.User.Identity.GetUserId()转换为带c#的int

如何在asp.net vs2015中将Context.User.Identity.GetUserId()转换为带c#的int,c#,asp.net,.net,C#,Asp.net,.net,如何将Context.User.Identity.GetUserId()转换为int 代码: 上述代码给出了错误声明: 将字符串转换为DateTime时,在将每个变量放入DateTime对象之前,先解析字符串以获取日期 我想你想要的是: NewInCart.ClientId = Int32.Parse(clientId); 暂时分类 我决定将dbo中ClientID的数据类型更改为nvarchar(50)这样我可以像这样保存: string clientId = Context.User.Id

如何将Context.User.Identity.GetUserId()转换为int

代码:

上述代码给出了错误声明:

将字符串转换为DateTime时,在将每个变量放入DateTime对象之前,先解析字符串以获取日期


我想你想要的是:

NewInCart.ClientId = Int32.Parse(clientId);
暂时分类

我决定将dbo中ClientID的数据类型更改为
nvarchar(50)这样我可以像这样保存:

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

其中,
cart.ClientId
现在需要一个字符串。这解决了我的问题,我不再需要将
Context.User.Identity.GetUserId()
转换为int.

它告诉你该怎么做你熟悉
DateTime.Parse | DateTime.TryParse | DateTime.ParseExact
函数还有你的标题和你的问题是两件不同的事情。你知道如何使用调试器吗。。?当您使用调试器逐步执行代码时,
cliendId
的值是多少?您确定您的newInvot.clientid不是日期吗?我认为Identity上的.clientid已经是一个int。我确信Context.User.Identity.GetUserId()返回的字符串不是日期。当我将“-1”分配给clientId时,代码就起作用了;但是,当我分配Context.User.Identity.GetUserId()时,它会抛出一个错误,告诉我格式不正确(就像我试图格式化一个日期,而不是Context.User.Identity.GetUserId()生成的文本:xx-45-rty-566-g45等)。问题是,如何将Context.User.Identity.GetUserId()格式正确设置为int?获取调用GetUserId返回的字符串,然后让我们使用它…我尝试了Int32.Parse(clientId);和int.Parse(clientId);所有的错误都是一样的。我是否缺少using声明?@method man,编译器认为Context.User.Identity.GetUserId()是一个日期。基本上,将格式设置为int的常规方法不起作用。int clientId=int.Parse(Context.User.Identity.GetUserId());仍然给出了相同的错误。同时Context.User.Identity.GetUserId()本身返回一个字符串,但编译器拒绝将其格式化为int。
string clientId = Context.User.Identity.GetUserId() ;
.......
cart.ClientId = clientId;