Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
Below is my code, I want upload a excel file as input.
Then,

step 1:I have saved file in one path.
step 2:Read Excel file.
step 3:Convert excel into data set.
step 4:Insert data set into table using bulkinsert query.

Unspecified error occur while executing the code on the line Connection1.Open();

now code is deployed in share point site throwing error...
Please help me.




C#
private static DataSet GetExcelAsDataSet(string fileName, bool firstRowIsHeader)
        {
            OleDbConnection excelConnection = null;
            OleDbDataAdapter adapter = null;
            DataSet ds = new DataSet();
            System.Data.DataTable dtTables = new System.Data.DataTable();
            //fileName = "C:\\myfolder\\UserInformationList.xlsx";
            string myconnection1 = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties='Excel 8.0;HDR=YES;'";
            using (OleDbConnection Connection1 = new OleDbConnection(myconnection1))
            {
               
                    Connection1.Open();
                    //to get the schema of the workbook.
                    dtTables = excelConnection.GetSchema();

                    //get the tables in the workbook
                    dtTables = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                    String[] excelSheets = null;
                    if ((dtTables != null))
                    {
                        excelSheets = new String[dtTables.Rows.Count];
                        int i = 0;

                        // Add the sheet name to the string array.
                        foreach (DataRow row in dtTables.Rows)
                        {
                            excelSheets[i] = row["TABLE_NAME"].ToString();
                            i++;
                        }
                    }

                    //prepare dataset from the tables in the workbook
                    foreach (string sheet in excelSheets)
                    {
                        OleDbCommand cmd = new OleDbCommand();
                        cmd.Connection = excelConnection;
                        cmd.CommandText = "Select * from [" + sheet + "]";
                        System.Data.DataTable dtItems = new System.Data.DataTable();
                        string SheetName = sheet.Trim('\'');

                        SheetName = SheetName.Substring(0, SheetName.IndexOf("$"));
                        dtItems.TableName = SheetName;
                        if (!ds.Tables.Contains(SheetName))
                        {
                            adapter = new OleDbDataAdapter();
                            adapter.SelectCommand = cmd;
                            adapter.Fill(dtItems);
                            ds.Tables.Add(dtItems);
                        }
                    }
                
            }
return ds;
}


Note: i have used same code in C# web application its working fine.
Posted
Comments
CHill60 15-Dec-15 9:50am    
I suspect the filename is incorrect for the SharePoint site
Baskar Gs 15-Dec-15 10:22am    
Is there any specific file name format is there for share point?
DanielBrownAU 15-Dec-15 17:51pm    
What path are you using for the filename/its location?
Baskar Gs 15-Dec-15 23:14pm    
File path will look like this
Baskar Gs 15-Dec-15 23:14pm    
fileName = "C:\\myfolder\\UserInformationList.xlsx";

1 solution

hi,

This is IIS Authentication Error.
I set
<identity impersonate="true" /> to <identity impersonate="false" />
in my share point web config file.

Now the code was working fine.

Thank you guys
 
Share this answer
 
v2

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