Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hey guys,
sorry to post an error message, but I am really having trouble here. I have a c# winforms project that gets this error message:

************** Exception Text **************
    System.FormatException: Input string was not in a correct format.
       at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
       at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
       at SlashA006.Form1.IdCheck() in C:\Users\John\Desktop\John\SCAR\OperationMonkeyRaider-v2\visual studio\real\SlashA006 f\SlashA006\SlashA006\Form1.cs:line 180
       at SlashA006.Form1.Form1_Load(Object sender, EventArgs e) in C:\Users\John\Desktop\John\SCAR\OperationMonkeyRaider-v2\visual studio\real\SlashA006 f\SlashA006\SlashA006\Form1.cs:line 64
       at System.Windows.Forms.Form.OnLoad(EventArgs e)
       at System.Windows.Forms.Form.OnCreateControl()
       at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
       at System.Windows.Forms.Control.CreateControl()
       at System.Windows.Forms.Control.WmShowWindow(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.Form.WmShowWindow(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


So I did some research, I guess this error means some variable types could be mixed around, but I have no idea what to do and I can't find anything relavent online. My code for lines 64-180 is:

line 64:
C#
IdCheck();

line 180:
C#
public void IdCheck()
       {
           string currentDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)+ @"\SlashA006";
           string path = currentDirectory + @"\saveData.txt";
           if (!File.Exists(path))
           {
              var saveDataFile = File.Create(path);
               saveDataFile.Close();
               int id = IDsetup();
               TextWriter tw = new StreamWriter(path);
               tw.WriteLine(id.ToString());
               tw.Close();
               computerID = id;
               this.Visible = true;
               System.Windows.Forms.MessageBox.Show("ID is: " + id);
               func_wait_3("500");
               this.Visible = false;
           }
           if (File.Exists(path))
           {
               bool success = int.TryParse(File.ReadAllText(path), out int idRead);
               //  printLine(idRead.ToString()); //LINE 180 ABOVE THIS ONE
               computerID = idRead;
           }
       }

Once again, sorry for a newb question, but I'd really appreciate any help. Thanks!

What I have tried:

As I said above, I tried looking online for examples of this problem but to no avail. I changed line 180 from parse to tryparse, but that didn't work either.
Posted
Updated 13-Mar-18 13:38pm
v2

1 solution

Basically, it means that whatever text you're trying to convert to a number type, or date type, is not a string of characters that can be converted to that type.

If you enter "AXG012" and then type to convert that to an integer, you'll get the error you're talking about.

In your example, it would appear that your reading an ENTIRE FILE and trying to convert that to an integer. Carriage Return and Line Feed characters don't convert to integers, so I'm not surprised you're getting this error.

What you're doing is very odd. You normally don't do any kind of file operation in the parameters to TryParse.
 
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