Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
1.40/5 (2 votes)
See more:
this is my code error occured in insert excel data.


string conStr = "";
switch (fileExtens)
{
    case ".xls": //Excel 97-03

        conStr = ConfigurationManager.ConnectionStrings["Excel3ConString"].ConnectionString;
        break;

    case ".xlsx": //Excel 07

        conStr = ConfigurationManager.ConnectionStrings["Excel7ConString"].ConnectionString;
        break;
}
conStr = String.Format(conStr, locat);


OleDbConnection connExcel = new OleDbConnection(conStr);
OleDbCommand cmdExcel = new OleDbCommand();
OleDbDataAdapter oda = new OleDbDataAdapter();
DataTable dt = new DataTable();
cmdExcel.Connection = connExcel;

connExcel.Open();
DataTable dtExcelSchema;
dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
connExcel.Close();

connExcel.Open();
cmdExcel.CommandText = "SELECT * From [" + SheetName + "]";
oda.SelectCommand = cmdExcel;
oda.Fill(dt);
connExcel.Close();
return dt;
Posted
Comments
ZurdoDev 16-Oct-15 8:17am    
1. Give the entire error message.
2. Show what line of code causes the error.

With the above information we can help you; otherwise, we'll just be guessing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900