1

am getting the following error while trying to import data from excel to the database.

The Microsoft Office Access database engine could not find the object 'C:\Users\DAKTARI\Desktop\smarttable.xls'

this is my code behind that am using.

 public partial class Smarttable : System.Web.UI.Page
{
    OleDbConnection Econ;
    SqlConnection con;

    string constr, Query, sqlconn;
    protected void Page_Load(object sender, EventArgs e)
    {

    }


    private void ExcelConn(string FilePath)
    {

        constr = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\DAKTARI\Desktop\smarttable.xls;Extended Properties=""Excel 12.0 Xml;HDR=YES;""");
        Econ = new OleDbConnection(constr);

    }
    private void connection()
    {
        sqlconn = ConfigurationManager.ConnectionStrings["SqlCom"].ConnectionString;
        con = new SqlConnection(sqlconn);

    }


    private void InsertExcelRecords(string FilePath)
    {
        ExcelConn(FilePath);

        Query = string.Format("Select [InvoiceNumber],[AmountPaid],[Remarks] FROM [C:\\Users\\DAKTARI\\Desktop\\smarttable.xls]", "Orders$");
        OleDbCommand Ecom = new OleDbCommand(Query, Econ);
        Econ.Open();

        DataSet ds = new DataSet();
        OleDbDataAdapter oda = new OleDbDataAdapter(Query, Econ);
        Econ.Close();
        oda.Fill(ds);
        DataTable Exceldt = ds.Tables[0];
        connection();
        //creating object of SqlBulkCopy    
        SqlBulkCopy objbulk = new SqlBulkCopy(con);
        //assigning Destination table name    
        objbulk.DestinationTableName = "smarttable";
        //Mapping Table column    
        objbulk.ColumnMappings.Add("InvoiceNumber", "InvoiceNumber");
        objbulk.ColumnMappings.Add("AmountPaid", "AmountPaid");
        objbulk.ColumnMappings.Add("Remarks", "Remarks");
        //inserting Datatable Records to DataBase    
        con.Open();
        objbulk.WriteToServer(Exceldt);
        con.Close();

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string CurrentFilePath = Path.GetFullPath(FileUpload1.PostedFile.FileName);
        InsertExcelRecords(CurrentFilePath);
    }
}
3
  • 4
    well the error seems pretty specific..... Commented Aug 25, 2017 at 7:22
  • Is this a timing error i.e. is the file upload still in progress as you attempt to open it? Commented Aug 25, 2017 at 7:47
  • no it is not in progress..i select the file but upon the upload i get the error. Commented Aug 25, 2017 at 8:35

2 Answers 2

1

Your Excel file format uses XLS which means for Office 2003 or earlier, but you're using ACE OLEDB provider which used for Office 2007 or later:

constr = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\DAKTARI\Desktop\smarttable.xls;Extended Properties=""Excel 12.0 Xml;HDR=YES;"");

The correct usage is using Jet 4.0 provider like this:

constr = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=YES;'", FilePath);

Also you have second issue which a wrong query string is used to read the data inside worksheet:

Query = string.Format("Select [InvoiceNumber],[AmountPaid],[Remarks] FROM [C:\\Users\\DAKTARI\\Desktop\\smarttable.xls]", "Orders$");

This should be changed to proper form below:

Query = "SELECT [InvoiceNumber],[AmountPaid],[Remarks] FROM [Orders$]";
Sign up to request clarification or add additional context in comments.

13 Comments

thanks alot. am using excel 2016. i have corrected the ACE OLEDB to using jet 4.o but am now getting this error; could not find installable ISAM.
kindly assist me..its giving me a hell of time
Use single quotes for Extended Properties part instead of double ones, see the edited one (from stackoverflow.com/questions/512143/…).
i have done that and now am getting this error. Format of the initialization string does not conform to specification starting at index 89.
kindly help me out am totally confused on this one
|
0

Change this line

Query = string.Format("Select [InvoiceNumber],[AmountPaid],[Remarks] FROM [C:\\Users\\DAKTARI\\Desktop\\smarttable.xls]", "Orders$");

To

Query = "Select [InvoiceNumber],[AmountPaid],[Remarks] FROM [Orders$]";

The FROM needs to be followed by the Sheet Name which is treated like a table

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.