john kc
john kc

Reputation: 45

importing excel data to database using asp.net and c #

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);
    }
}

Upvotes: 1

Views: 151

Answers (2)

Tetsuya Yamamoto
Tetsuya Yamamoto

Reputation: 24957

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$]";

Upvotes: 1

Dumisani
Dumisani

Reputation: 3048

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

Upvotes: 0

Related Questions