Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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.


C#
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");
}

}
Posted
Comments
Thanks7872 25-Aug-13 8:03am    
Its pretty clear from message. Make sure the object(you are trying to access) exists and that you spell its name and the path name correctly.
Anh Nguyen Trong 25-Aug-13 22:59pm    
do you have installed this?
The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine. ?
download patch from

http://www.microsoft.com/en-us/download/details.aspx?id=13255
syed shanu 25-Aug-13 23:17pm    
Make sure you have full permission for your excel file.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900