简体   繁体   中英

Input string was not in a correct format. in C# ASP.NET

I see everyday in my Elmah this error. How can fix this?

Input string was not in a correct format. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

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

Source Error:

Line 40:     {
Line 41:         String Pass = password.Text;
Line 42:         Int64 UserName = Convert.ToInt64(username.Text);
Line 43: 
Line 44:         if ( Convert.ToInt32(Session["FaildLoginTime"]) == 5)

Source File: c:\\Inetpub\\vhosts\\domin.com\\my.domin.com\\WebMaster\\LoginMaster.master.cs Line: 42

Stack Trace:

[FormatException: Input string was not in a correct format.]

   System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +11793873

   System.Number.ParseInt64(String value, NumberStyles options, NumberFormatInfo numfmt) +133

   System.Convert.ToInt64(String value) +51

   WebMaster_LoginMaster.btnLogin_Click(Object sender, EventArgs e) in c:\Inetpub\vhosts\domin.com\my.domin.com\WebMaster\LoginMaster.master.cs:42

   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9696694

   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +204

   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12

   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15

   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35

   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1639

At line 42: Int64 UserName = Convert.ToInt64(username.Text); you should make sure that username.Text returns a valid integer.

Try using Int64.TryParse instead.

Int64.TryParse Method

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