Saturday, March 31, 2012

System.FormatException: Input string was not in a correct format

I am getting above error

// Validate input
if ((userId.Trim() == string.Empty) || (password.Trim() == string.Empty))
return null;

// Get an instance of the account DAC using the DACFactory
IUserAccount dac = CData.DACFactory.FDACUserAccount.Create();

//On this line i am getting error.userId and Password are strings
UserAccountInfo userAccount = dac.Login(userId , password);
return userAccount;

Please sort out my problemI have made three assemblies. 1 is calling 2nd and 2nd is calling 3rd.
now when i call methods of 3rd from 1st through 2nd one i get "System.FormatException: Input string was not in a correct format" on every string manipulation. Even if it is throwing exceptions in string formats it gives that exception
When you use the Trim method, it expects the variable to be a string. When your variable is null, the Trim method would throw that FormatException. So you should rewrite your code to test your variable again null instead.
Ex:
<code>
if( userID == null )
{
//do something here
}

0 comments:

Post a Comment