please send the suggestion on following code ..
Error :-
The Microsoft Office Access database engine could not find the object 'C:\Sample.xlsx'. Make sure the object exists and that you spell its name and the path name correctly.
private void btn_Browse_Click(object sender, EventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Title = "Select file";
fdlg.InitialDirectory = @"c:\";
fdlg.FileName = txt_path.Text;
fdlg.Filter = "Excel Sheet(*.xlsx)|*.xlsx|All Files(*.*)|*.*";
fdlg.FilterIndex = 1;
fdlg.RestoreDirectory = true;
if (fdlg.ShowDialog() == DialogResult.OK)
{
txt_path.Text = fdlg.FileName;
strExcelPathName = txt_path.Text;
}
}
private void btn_Show_Click(object sender, EventArgs e)
{
if (System.IO.File.Exists(strExcelPathName))
{
try
{
string connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + strExcelPathName + "';Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\";", "", "", 0);
string query = String.Format("select * from [" + txt_path.Text + "]");
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connectionString);
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet);
DataTable dtView = dataSet.Tables[0];
if (dtView.Rows.Count > 0)
{
dataGridView1.Rows.Clear();
dataGridView1.Rows.Add(dtView.Rows.Count);
//int i = 0;
//foreach (DataRow drow in dtView.Rows)
//{
// //dgrdReciver.Rows[i].Cells["SerialNo"].Value = j;
// dataGridView1.Rows[i].Cells["Addressid"].Value = drow["ADRSID"];
// dataGridView1.Rows[i].Cells["ReciverName"].Value = drow["NAME"];
// dataGridView1.Rows[i].Cells["companyName"].Value = drow["COMPANY"];
// dataGridView1.Rows[i].Cells["RecvAddress"].Value = drow["ADDRESS"];
// i++;
//}
}
}
catch (OleDbException ex)
{
MessageBox.Show(ex.ToString());
}
}
else
{
MessageBox.Show("No File is Selected");
}
}