0

I have been having difficulty with this code. Right here below, I have a load button on my windows form application. I am trying to configure this button to load excel data upon and onto a datagridview control which I have placed upon the form. My code, keeps on crashing because it's saying it's having an exception handling. I am willing to learn and fix this code. SO, please help me when you get the chance. I hope you have a good day!

Here is my code below to illustrate what my issue is. I am getting the exception handler at the link.Open, so this is where my error is occurring. Below are multiple screen shots which display my error message.

  private void LoadButton_Click(object sender, EventArgs e)
        {
            // This code is getting excel file to load upon the dataViewGrid table

            using (OpenFileDialog dialog = new OpenFileDialog() { Filter = "Excel Workbook | *.xlsx", ValidateNames = true })
            {
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    string name = "Items";
                    string path = dialog.FileName;
                    string DataT = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = " + path + "; Extended Properties = 'Excel 8.0;HDR=Yes;IMEX=1;";

                    OleDbConnection link = new OleDbConnection(DataT);
                    OleDbCommand cmd = new OleDbCommand("select * from [ " + name + "$]", link);
                    link.Open();


                    OleDbDataAdapter dir = new OleDbDataAdapter(cmd);
                    DataTable td = new DataTable();
                    dir.Fill(td);

                    td.Load(cmd.ExecuteReader());
                    dataGridView1.DataSource = td;
                    /*
                    DataTable td = new DataTable();
                    td.Load(cmd.ExecuteReader());
                    dataGridView1.DataSource = td;
                    */


                }

            }
        }
    }
}

Image of Exception Error in Program

dbc
  • 104,963
  • 20
  • 228
  • 340
  • Post the error you get. – dglozano Dec 08 '18 at 23:05
  • here is my errror and are the screenshots with the error message. – Mike Jones Dec 09 '18 at 00:35
  • Might you please [edit] your question to include the full `ToString()` output of your exception as **text** rather than as a screenshot? It's policy here not to to use images for textual data, see [*Discourage screenshots of code and/or errors*](https://meta.stackoverflow.com/a/307500) and [*Why not upload images of code on SO when asking a question*](https://meta.stackoverflow.com/a/285557) for why. – dbc Dec 09 '18 at 01:22
  • Also, what is the value of `path`? Does an Excel document actually exist at that location? – dbc Dec 09 '18 at 01:23
  • The value of path is = dialog.FileName; The Excel File document is named Game_Inventory – Mike Jones Dec 09 '18 at 02:03
  • Probably that's not the value of `path`, that's where it was defined. The value would be the string inserted into `DataT`, which is likely not `Provider = Microsoft.Jet.OLEDB.4.0; Data Source = dialog.FileName; Extended Properties = 'Excel 8.0;HDR=Yes;IMEX=1;`? – dbc Dec 09 '18 at 02:06
  • Incidentally, [Difference between Microsoft.Jet.OleDb and Microsoft.Ace.OleDb](https://stackoverflow.com/q/14401729) suggests using `Provider=Microsoft.ACE.OLEDB.12.0`, i.e. `string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0; Data Source={0}; Extended Properties=""Excel 8.0;HDR=YES;IMEX=1""", path)`. You might try that. – dbc Dec 09 '18 at 02:07
  • Dbc- i have tried to use the method above and it's still giving me an error message. I have the most current version of Excel, is this seem to be the case? – Mike Jones Dec 09 '18 at 18:39

0 Answers0