0

I am trying to access excel data from a file using windows form to input latitude and longitude information. However, When I debug it, it throws "input string not in correct format" error. I am using the following code:

namespace Excel_Access
{
    public partial class Form1 : Form

    {
        string myDouble2;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)

        {
            string cellValue = "";
            string cellValue1 = "";
            string cellValue2 = "";

            OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source =G:\Manish Dixit\TAMU\BS2015\SolarInsolation\Excel Sheets\Clr_Index.xlsm; Extended Properties= Excel 12.0");
            OleDbDataAdapter da = new OleDbDataAdapter("select*from[Sheet2$]", con);
            DataTable dt = new DataTable();   
            da.Fill(dt);
            int i = 1;
            do
            {
            cellValue = dt.Rows[i][0].ToString();

                if(double.Parse(cellValue)==double.Parse(textBox1.Text))
                    cellValue1 = dt.Rows[i][1].ToString();
                    if (double.Parse(cellValue1) == double.Parse(textBox2.Text))
                    {
                        cellValue2 = dt.Rows[i][5].ToString();
                        MessageBox.Show(cellValue2);
                    }

                    i++;

            }

            while (i < 441);


            //myDouble2 = ExcelDB(10, "L2");
            //MessageBox.Show("The value is " +myDouble2);
        }


    }
}

Could you please suggest why this is happening? The input values of latitude and longitude are in decimal.Please see the attached image of the data I am trying to access. See data image in Excel file here

user1671860
  • 75
  • 1
  • 2
  • 6
  • What line gives the error? We basically have to guess right now what throws the error. – Bas Apr 30 '15 at 14:24
  • 3
    Is `cellValue` ever null? Did you try setting a breakpoint and seeing what the values are at the line it throws the error? I bet you'll figure it out if you do... – Ron Beyer Apr 30 '15 at 14:26
  • Thanks a lot for your responses! The error is in the second IF statement: if (double.Parse(cellValue1) == double.Parse(textBox2.Text)) – user1671860 Apr 30 '15 at 14:36

1 Answers1

1

You have no error handling on your double.parse. I would look at the values you are trying to double.parse in your debugger and add some error handling or use double.TryParse. see Input string was not in correct format

double.Parse(textBox2.Text)

If this is not a valid double an exception 'Input string was not in a correct format' will be thrown.

Community
  • 1
  • 1
kmcnamee
  • 5,097
  • 2
  • 25
  • 36
  • Thanks Kmcnamee and all other who responded! I found that there was something wrong with the IF statement that was causing a null value to exist. I finally caught it using the debugger and resolved it. Thanks a lot once again! – user1671860 May 01 '15 at 12:56