title: I cannot figure out how to fix an unhandled Format Excpetion
body: <p>I am trying to write a C# program using Visual Studio that reads data from a file and allow me perform various sorts on the data. I am a beginner so it took much research to figure out how to write this code: Now I get a:</p>
  
  <blockquote>
    <p>Format Exception was unhandled.  Input string was not in a correct
    format</p>
  </blockquote>
  
  <p>I am not sure where I went wrong. This is happening for the following line of code:</p>
  
  <p><code>Candidate newrec = new Candidate(str[0], str[1], str [2], str[3], str[4], str[5], str[6], Convert.ToInt32(str[7]), str[8], str[9], str[10], str[11]);</code></p>
  
  <p>The entire code is as follows:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.IO;
  using System.Collections;
  
  
  
  namespace Unit4IP
  {
     //used to sort in asceding and descending order
      public struct Candidate:IComparable 
      {
  
         public char[] _FirstName;
          public char[] _LastName;
          public char[] _Company;
          public char[] _Address;
          public char[] _City;
          public char[] _Country;
          public char[] _State;
          public char[] _Phone;
          public char[] _Fax;
          public char[] _Email;
          public char[] _Web;
          public int _zip;
  
          //for comparing objects
          public int CompareTo(object obj)
          {
              Candidate Candidate2 = (Candidate)obj;
              return _LastName.ToString().CompareTo(Candidate2._LastName.ToString());
          }
          //implements sorting based on assignments such as zip, lastname, etc. 
          public int CompareTo(Candidate Candidate2,
              CandidateComparer.ComparisonType comptype)
          {
              if(comptype==CandidateComparer.ComparisonType.Lastname)
              {
                      String _LName = new String(_LastName);
                      String LName = new String(Candidate2._LastName);
                      return _LName.CompareTo(LName);// Convert Character Array to String because CompareTo Works efficiently with Strings
              }
              else
              {
                      return Candidate2._zip.CompareTo(_zip); // compareto values that are interchanged in descending order
              }
          }
                 //Constructor of Candidate Structure
           public Candidate(string FirstName, string LastName, string Company, string Address, string City, string Country, string State, int zip, string Phone, string Fax, string Email, string Web)
          {
              _FirstName = new char[12];
              _LastName = new char[16];
              _Company = new char[32];
              _Address = new char[32];
              _City = new char[24];
              _Country = new char[24];
              _State = new char[2];
              _Phone = new char[12];
              _Fax = new char[12];
              _Email = new char[32];
              _Web = new char[42];
              _FirstName = FirstName.ToCharArray();
              _LastName = LastName.ToCharArray();
              _Company = Company.ToCharArray();
              _Address = Address.ToCharArray();
              _City = City.ToCharArray();
              _Country = Country.ToCharArray();
              _State = State.ToCharArray();
              _zip = zip;
              _Phone = Phone.ToCharArray();
              _Fax = Fax.ToCharArray();
              _Email = Email.ToCharArray();
              _Web = Web.ToCharArray();
          }
  
           //Implement IComparer Interface as nested structure
           public struct CandidateComparer : IComparer
           {
               public enum ComparisonType
               { Lastname = 1, zip = 2 }
               private ComparisonType _comparisonType;
               public ComparisonType comptype
               {
                   get { return _comparisonType; }
                   set { _comparisonType = value; }
               }
               public int Compare(object x, object y)
               {
                   Candidate Candidate1 = (Candidate)x;
                   Candidate Candidate2 = (Candidate)y;
  
                   return Candidate1.CompareTo(Candidate2, _comparisonType);
               }
           }
      }
  class Program
      {
          static void Main(string[] args)
          {
              ArrayList ArrayTest = new ArrayList();
              //Loading of File 'ITCO321_U4_sample_data.csv' into ArraList. File only holds values, no heading i.e. remove headings
              StreamReader stream1 = File.OpenText("c:\\Users\\Cdhss\\Documents\\ITCO321_U4IP_sample_data-2.csv");
              string recdata = null;
              while ((recdata = stream1.ReadLine()) != null)
              {
                  string[] str = recdata.Split(',');
                  Candidate newrec = new Candidate(str[0], str[1], str [2], str[3], str[4], str[5], str[6], Convert.ToInt32(str[7]), str[8], str[9], str[10], str[11]);
                  ArrayTest.Add(newrec);//add struct object into ArrayList
              }
                          //Traversing of Records
              Console.WriteLine("Traversing Records");
              foreach (Candidate Candidate1 in ArrayTest)
              {
                  string fname = new String(Candidate1._FirstName);
                  string lname=new String(Candidate1._LastName);
                  string company = new String(Candidate1._Company);
                  string address=new String(Candidate1._Address);
                  string city=new String(Candidate1._City);
                  string country = new String(Candidate1._Country);
                  string phone = new String(Candidate1._Phone);
                  string fax = new String(Candidate1._Fax);
                  string email=new String(Candidate1._Email);
                  string web = new String(Candidate1._Web);
                  Console.WriteLine( fname + "," + lname + "," + company + "," + address  + "," + city + "," + country + "," + Candidate1._zip + "," + phone + "," + fax + "," + email + "," + web);
              }
              Candidate.CandidateComparer comparer = new Candidate.CandidateComparer();
              //Sort by Lastname in ascending order
              comparer.comptype = Candidate.CandidateComparer.ComparisonType.Lastname;
              ArrayTest.Sort(comparer);
              Console.WriteLine("Sorting of Elements by LastName");
              foreach (Candidate Candidate1 in ArrayTest)
              {
                  string fname = new String(Candidate1._FirstName);
                  string lname = new String(Candidate1._LastName);
                  string company = new String(Candidate1._Company);
                  Console.WriteLine("\t" + fname + "," + lname + "," + company);
              }
              // Data sorted in desending order of ZIP field
              comparer.comptype = Candidate.CandidateComparer.ComparisonType.zip;
              ArrayTest.Sort(comparer);
              Console.WriteLine("Sorting of Elements by Zip");
              foreach (Candidate Candidate1 in ArrayTest)
              {
                  string fname = new String(Candidate1._FirstName);
                  string lname = new String(Candidate1._LastName);
                  string company = new String(Candidate1._Company);
                  Console.WriteLine("\t" + fname + "," + lname + "," + company + "," + Candidate1._zip);
              }
  
              //Display Records of 'NY' State
              Console.WriteLine("Display Records of NY State");
              foreach (Candidate Candidate1 in ArrayTest)
              {
                  string fname = new String(Candidate1._FirstName);
                  string lname = new String(Candidate1._LastName);
                  string company = new String(Candidate1._Company);
                  string address = new String(Candidate1._Address);
                  string city = new String(Candidate1._City);
                  string country = new String(Candidate1._Country);
                  string phone = new String(Candidate1._Phone);
                  string fax = new String(Candidate1._Fax);
                  string email = new String(Candidate1._Email);
                  string web = new String(Candidate1._Web);
                  if (new String(Candidate1._State).Contains("NY"))
                      Console.WriteLine(fname + "," + lname + "," + company + "," + address + "," + city + "," + country + "," + Candidate1._zip + "," + phone + "," + fax + "," + email + "," + web);
              }
              Console.Read();
          }
      }
  }
  </code></pre>