8

I'm trying to insert data from excel file into a column of sql database.

protected void Button1_Click(object sender, EventArgs e)
    {
        using (OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["ExcelCon"].ConnectionString))
        {
            con.Open();
            OleDbCommand com = new OleDbCommand("Select * from [Sheet1$]", con);
            OleDbDataReader dr = com.ExecuteReader();
            using (SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Sql"].ConnectionString))
            {
                sqlcon.Open();
                using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlcon))
                {
                    bulkCopy.ColumnMappings.Add("ExcelColumnHeaderName", "SqlColumnName");
                    bulkCopy.DestinationTableName = "tableName";
                    bulkCopy.WriteToServer(dr);
                }
            }
            dr.Close();
            dr.Dispose();
        }
        Response.Write("Upload Successfull!");
    }

web config :

   <connectionStrings>
  <add name="ExcelCon" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\John\\Desktop\\ExcelFile.xlsx;Extended Properties=Excel 12.0"/>
  <add name="Sql" connectionString="Data Source=.\\SQLEXPRESS;AttachDbFilename='c:\\users\\John\\documents\\visual studio 2010\\Projects\\WebApplication3\\WebApplication3\\App_Data\\Database1.mdf';Integrated Security=True;User Instance=True;"/>
  <add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"
        providerName="System.Data.SqlClient" />
</connectionStrings>

The problem is I'm getting this error and I don't know why:

Exception Details: System.InvalidOperationException: Instance failure.
here:sqlcon.Open();
1
  • This question has nothing to do with ASP.NET nor with Excel. Most source code shown is unneeded. Commented May 20, 2012 at 17:51

1 Answer 1

21

Remove the double slashes from the connection string.

Sign up to request clarification or add additional context in comments.

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.