Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
An exception of type System.FormatException& occurred in mscorlib.dll but was not handled in user code

 ChangePasswordModel changemodel = new ChangePasswordModel()
changemodel.AdminId = Convert.ToInt32(User.Identity.Name);
Posted
Comments
phil.o 8-Dec-15 0:58am    
Convert.ToInt32(User.Identity.Name);

What exactly do you expect from this statement?

First of all, don't use Convert, uses int.Parse (which throws exception) or, better, int.TryParse. Not all strings someone enters can be parsed as integer; I have no idea why did you decide that User.Identity.Name should. Anyway, with the methods I mentioned you can check it up and handle the cases when the string cannot be parsed.

—SA
 
Share this answer
 
Unless User.Identity.Name always contains only the digits 0 to 9, and has a total value that will fit in an Int32, then Convert.ToInt32 will always fail.

Given that it's called Name the chances are that it isn't a number. I'd start by looking at what it should contain, and check if what you actually meant was something like:
C#
changemodel.AdminId = Convert.ToInt32(User.Identity.Id);
Or more likely:
C#
changemodel.AdminId = User.Identity.Id;

But if you do have to convert it then use TryParse:
C#
if (!int.TryParse(User.Identity.Id, out changemodel.AdminId))
   {
   // Report or log problem
   ...
   }
else
   {
   // Work with value
   ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900