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;
*/
}
}
}
}
}