Click here to Skip to main content
14,870,621 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Error::
Fatal error encountered during command execution.

C#
Con.Open();
        string path = fileuploadExcel.PostedFile.FileName;
       // fileuploadExcel.SaveAs(Server.MapPath("orders.xlsx"));
        
        string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("orders.xlsx") + ";Extended Properties=Excel 12.0;Persist Security Info=False";
        
        OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
        
        DataTable dt = new DataTable();
        excelConnection.Open(); 
        try
        {           

             dt.Columns.AddRange(new DataColumn[11] { new DataColumn("Date", typeof(DateTime)),
                new DataColumn("Ref_No", typeof(string)),
                new DataColumn("Partys_Name",typeof(string)),
               new DataColumn("Pending", typeof(float)),
        new DataColumn("<30 days ", typeof(float)),
        new DataColumn("30to60days", typeof(float)),
        new DataColumn("60 to 90 days", typeof(float)),
        new DataColumn("90 to 120 days", typeof(float)),
        new DataColumn("(> 120 days )", typeof(float)),
        new DataColumn("Post-Dated", typeof(string)),
        new DataColumn("Final", typeof(float))});
             OleDbCommand cmd = new OleDbCommand("Select * from [Sheet1$]", excelConnection);
             OleDbDataReader dReader;
             dReader = cmd.ExecuteReader();

            // create a connection string with your sql database

            //insert excel data in student table
             MySqlCommand cm = new MySqlCommand("insert into tbl_upload values(@Date,@Ref_No,@Partys_Name,@Pending,@<30 days,@30to60days,@60 to 90 days,@90 to 120 days,@(> 120 days ),@Post-Dated,@Final)", Con);

            cm.Parameters.AddWithValue("Date", "Date");

            cm.Parameters.AddWithValue("Ref_No", "Ref_No");

            cmd.Parameters.AddWithValue("Partys_Name", "Partys_Name");

            cm.Parameters.AddWithValue("<30 days ", "less30days");
             cm.Parameters.AddWithValue("30to60days", "bet30to60days");
             cm.Parameters.AddWithValue("60 to 90 days", "bet60to90days");
             cm.Parameters.AddWithValue("90 to 120 days", "bet90to120days");
             cm.Parameters.AddWithValue("(> 120 days )", "greater120days");
             cm.Parameters.AddWithValue("Post-Dated", "Posted_Dated");
            cm.Parameters.AddWithValue("Final", "Final");

            int i = cm.ExecuteNonQuery();

            if (i > 0)

            {

                Label2.Text = "Data inserted successfully";

            }           

        }       

            catch (Exception ex)

        {
                Label2.Text =ex.Message;
            }
        excelConnection.Close();
        Con.Close();
    }
Posted
Updated 30-Dec-14 19:46pm
v2

1 solution

You forgot to add "Parameters.AddWithValue" line for "Pending" parameter and I can see some whitespaces added here and there for strings...It seems you need to verify if those needed and update code accordingly.

Once that done...let us know if it works (or new error message). Thanks.
   
v2
Comments
Member 11264646 31-Dec-14 2:01am
   
Thank you for your reply
i am update that line but i got the same error.
Snesh Prajapati 31-Dec-14 2:30am
   
Thanks for update...please check if below links can help:
http://csharp.net-informations.com/excel/csharp-excel-oledb-insert.htm
http://www.codeproject.com/Articles/8500/Reading-and-Writing-Excel-using-OLEDB

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