Click here to Skip to main content
15,922,166 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hi
I'm Developing an application which will get the excel file from the user and read the data and finally it will store it in sql server 2008.

But it keep on throwing the above error when i try to open a excel file connection.

Here is my code:

protected void btnSave_Click(object sender, EventArgs e)
        {
            string FileName = lblFileName.Text;

            string Extension = Path.GetExtension(FileName);

            string FolderPath = Server.MapPath(ConfigurationManager.AppSettings["FolderPath"]);

            string CommandText = "";

            switch (Extension)
            {

                case ".xls": //Excel 97-03

                    CommandText = "spx_ImportFromExcel03";

                    break;

                case ".xlsx": //Excel 07

                    CommandText = "spx_ImportFromExcel07";

                    break;

            }

            //Read Excel Sheet using Stored Procedure

            //And import the data into Database Table

            String strConnString = ConfigurationManager.ConnectionStrings["CARGONETConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(strConnString);
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = CommandText;
            cmd.Parameters.Add("@SheetName", SqlDbType.VarChar).Value = ddlSheets.SelectedItem.Text;
            cmd.Parameters.Add("@FilePath", SqlDbType.VarChar).Value =FolderPath + FileName;
            cmd.Parameters.Add("@HDR", SqlDbType.VarChar).Value =rbHDR.SelectedItem.Text;
            cmd.Parameters.Add("@TableName", SqlDbType.VarChar).Value = "TB_TransAgentSeaFreightRate";
            cmd.Connection = con;
            try
            {
                con.Open();
                object count = cmd.ExecuteNonQuery();
                lblMessage.ForeColor = System.Drawing.Color.Green;
                lblMessage.Text = count.ToString() + " records inserted.";
            }

            catch (Exception ex)
            {

                lblMessage.ForeColor = System.Drawing.Color.Red;
                lblMessage.Text = ex.Message;

            }

            finally
            {

                con.Close();
                con.Dispose();
                Panel1.Visible = true;
                //Panel2.Visible = false;
                dg_AgentSFR.Visible = true;
            }
           
        }


Anyone please help me.
Thanks in advance.
Posted
Updated 23-Nov-20 3:53am
Comments
ZurdoDev 21-Mar-14 7:10am    
Refer to http://www.connectionstrings.com/

The error seems to say you have an issue with your connection string.

1 solution

My error was gone I run the below code:

USE [master] 
GO 
EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'AllowInProcess', 1 
GO 
EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'DynamicParameters', 1 
GO  
 
Share this answer
 
Comments
Joshua Magsino 21-Nov-16 1:27am    
This answer worked for me! Many thanks!

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