Click here to Skip to main content
15,905,325 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I get the above error while trying to upload excel sheet to datagrid. After series of debugging, it throws the error just when it gets to the "Connection.Open()" section of my code, Below is my code

What I have tried:

C#
protected void btnUpload_Click(object sender, EventArgs e)
        {
            string FilePath = ConfigurationManager.AppSettings["FilePath"].ToString();
            string filename = string.Empty;
            
            if (FUpload.HasFile)
            {
                try
                {
                    string[] allowedFile = { ".xls", ".xlsx" };
                    string fileExtension = System.IO.Path.GetExtension(FUpload.PostedFile.FileName);
                    bool isValidFile = allowedFile.Contains(fileExtension);
                    if (!isValidFile)
                    {
                        ErrorMessage.ForeColor = System.Drawing.Color.Red;
                        ErrorMessage.Text = "Please Upload only excel file";
                    }
                    else
                    {
                        filename = Path.GetFileName(Server.MapPath(FUpload.FileName));
                        FUpload.SaveAs(Server.MapPath(FilePath) + filename);
                        string filePath = Server.MapPath(FilePath) + filename;
                        OleDbConnection connection = null;
                        if (fileExtension == ".xls")
                        {
                            connection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + "; Extended Properties='Excel 8.0;HDR=YES;'");
                        }
                        else if (fileExtension == ".xslx")
                        {
                 connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " + filePath + ";Extended Properties='Excel 12.0 Xml;HDR=YES;'");
                        }
                        connection.Open();
                        DataTable data = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                        string getExcelSheetName = data.Rows[0]["Table_Name"].ToString();
                        OleDbCommand excelCommand = new OleDbCommand(@"SELECT * FROM[ getExcelSheetName$]", connection);
                        OleDbDataAdapter ExcelAdapter = new OleDbDataAdapter(excelCommand);
                        DataSet ExcelDataSet = new DataSet();
                        ExcelAdapter.Fill(ExcelDataSet);
                        connection.Close();
                        GridView1.DataSource = ExcelDataSet;
                        GridView1.DataBind();

                    }
                }
                catch (Exception ex)
                {

                }
            }
            else
            {
                ErrorMessage.Text = " Please select a file to upload";
            }
        }
Posted
Updated 21-Jun-17 17:40pm
v3
Comments
AnvilRanger 21-Jun-17 9:09am    
Never use an empty catch, this just swallows the exception. Look at you exception variable, ex, and look at the inner exception and that will give you a better clue as to your error.
Mcbaloo 21-Jun-17 9:49am    
External table is not in the expected format.. That is the error i got after printing it out to the screen
Mcbaloo 21-Jun-17 9:16am    
Thanks for the suggestion. i missed that part. Will run it now and type the error when done
Mcbaloo 21-Jun-17 9:33am    
External table is not in the expected format.. That is the error i got after printing it out to the screen

1 solution

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