title: Message=Input string was not in a correct format. in c#
body: <p>The following exception is being thrown in the first while loop that I convert to double:</p>
  
  <pre><code>System.FormatException
    HResult=0x80131537
    Message=Input string was not in a correct format.
    Source=mscorlib
    StackTrace:
     at System.Number.ParseDouble(String value, NumberStyles options, NumberFormatInfo numfmt)
     at System.Convert.ToDouble(String value)
     at WindowsFormsApp6.Form1.button1_Click(Object sender, EventArgs e) in C:\Users\King\source\repos\WindowsFormsApp6\WindowsFormsApp6\Form1.cs:line 52
     at System.Windows.Forms.Control.OnClick(EventArgs e)
     at System.Windows.Forms.Button.OnClick(EventArgs e)
     at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
     at System.Windows.Forms.Control.WmMouseUp(Message&amp; m, MouseButtons button, Int32 clicks)
     at System.Windows.Forms.Control.WndProc(Message&amp; m)
     at System.Windows.Forms.ButtonBase.WndProc(Message&amp; m)
     at System.Windows.Forms.Button.WndProc(Message&amp; m)
     at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
     at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
     at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
     at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp; msg)
     at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
     at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
     at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
     at System.Windows.Forms.Application.Run(Form mainForm)
     at WindowsFormsApp6.Program.Main() in C:\Users\King\source\repos\WindowsFormsApp6\WindowsFormsApp6\Program.cs:line 19
  </code></pre>
  
  <p>I'm not sure what I'm writing wrong or how to fix this issue. I'm reading from a csv file.  I have tested to make sure the data is being passed from variable to variable, and it is.  the data is being read by my streamreader and stored in single dimensional arrays which are being saved to a variable that I am using to compare the data points to determine all the peaks and the valleys in the dataset</p>
  
  <p>This is my program:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows.Forms;
  using System.IO;
  
  namespace WindowsFormsApp6
  {
      public partial class Form1 : Form
      {
          double firstY = 0.0;
  
          string testX;
          string testY;
  
          string[] xpoint = new string[5000];
          string[] ypoint = new string[5000];
  
          private void button2_Click(object sender, EventArgs e)
          {
              Application.Exit();
          }
  
          public Form1()
          {
              InitializeComponent();
          }
  
          private void button1_Click(object sender, EventArgs e)
          {
              using (var reader = new StreamReader(@"D:\data.csv"))
              {
                  while (!reader.EndOfStream)
                  {
                      string line = reader.ReadLine();
                      var values = line.Split(',');
  
                      testX = values[0];
                      testY = values[1];
  
                      if (firstY == 0.0)
                      {
                          firstY = Convert.ToDouble(testY);
                          Convert.ToString(testY);
                      }
  
                      while (Convert.ToDouble(testY) &gt;= firstY)//where error is
                      {
                          firstY = Convert.ToDouble(testY);
  
                          if (firstY == Convert.ToDouble(testY))
                          {
                              break;
                          }
  
                          if (Convert.ToDouble(testY) &lt; firstY)
                          {
                              listBox1.Items.Add(Convert.ToDouble(testX) + "," + firstY);
  
                              break;
                          }
  
                          break;
                      }
  
                      while (Convert.ToDouble(testY) &lt; firstY)
                      {
                          firstY = Convert.ToDouble(testY);
  
                          if(firstY == Convert.ToDouble(testY))
                          {
                              break;
                          }
  
                          if (Convert.ToDouble(testY) &gt; firstY)
                          {
  
                              listBox2.Items.Add(Convert.ToDouble(testX) + "," + firstY);
  
                              break;
                          }
  
                          break;
                      }
  
                      Convert.ToString(testX);
                      Convert.ToString(testY);
                  }
              }
          }
      }
  }
  </code></pre>