Skip to content

External table is not in the expected format. ASP.net C#

birju gohel

Asked Feb 12, 2015, 9:35 PM EST2 answers
Share
LinkedInX
**

> Asp.net C# Code

**
I have one excel File 2013 Version Create And Excel File Data Insert Sql Server 2012 Table So Error
("External table is not in the expected format. in asp.net")

Excle File Image


Database Table Image
 
 
 

My Above Two Table And Colum
So iahve Trasfer Data Excel 2013 Version Filedata in sql Server 2012 Table .
 
 
My code

 
 
Please Any Solution Give me .

string excelConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 8.0", path);


External table is not in the expected format.

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    // Globaly Declare String oldb public static string excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;";
    0
  • 0
    Global Declare This tow Variable ...
    =========================================
      public static string path = @"C:\Users\asp\Desktop\ColoreCode.xlsx";
        public static string excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;";

    ====================================================
    Click Event
    ========================================================

     protected void Button1_Click(object sender, EventArgs e)
        {
            //string CurrentFilePath = Path.GetFullPath(FileUpload1.PostedFile.FileName);
            //InsertExcelRecords(CurrentFilePath);  

            //connection();

            if (!FileUpload1.HasFile)
            {
                try
                {
                    //string path = string.Concat(Server.MapPath("~/Image/ColoreCode.xlsx"));//+ FileUpload1.FileName));

                    //FileUpload1.SaveAs(path);

                    // Connection String to Excel Workbook

                   // string excelConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 8.0", path);
                   // string excelconnectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";"+"Extended Properties="Excel 12.0;HDR=Yes";
                  
                    OleDbConnection connection = new OleDbConnection();

                    connection.ConnectionString = excelConnectionString;

                    OleDbCommand command = new OleDbCommand("select * from [Sheet5$]", connection);

                    connection.Open();

                    // Create DbDataReader to Data Worksheet

                    DbDataReader dr = command.ExecuteReader();

                    // SQL Server Connection String

                    string sqlConnectionString = @"Data Source=ASP4;Initial Catalog=Cricketers;User ID=xxxxxxx;Password=xxxxxxxxxx";

                    // Bulk Copy to SQL Server

                    SqlBulkCopy bulkInsert = new SqlBulkCopy(sqlConnectionString);

                    bulkInsert.DestinationTableName = "ColorMaster";

                    bulkInsert.WriteToServer(dr);

                    Label1.Text = "Ho Gaya";

                }

                catch (Exception ex)
                {

                    Label1.Text = ex.Message;

                }

            }

        }
    0