Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I encountered the following issue with Microsoft.ACE.OLEDB.12.0 ( 64-bit ), kindly advise :


Excel file has many sheets and huge number of columns but no. of rows on these worksheets are less than 2000, hence all worksheet are filling well except one having no. of rows are more then 2000. On local environment all working fine, but after hosting on IIS (on LIVE also) it's failing.
I have tried on LIVE server on debug mode (using visual studio) and it worked their. But the same site files via IIS failing.

Environment :
- AccessDatabaseEngine 2010 ( x64 ) + SP1
- Windows Server 2008 R2 Enterprise ( 64-bit )
- Microsoft SQL Server 2008 R2 Enterprise Edition (64-bit)
- IIS 7
- Simple Excel_Template.xlsm with columns layout A-AM

Scripting ( with ASP.NET - C# ) to retrieve selected data from MSSQL and insert into the excel template file.

Everything working FINE if the dataset / number of rows less than 2000.
The scripts started not behave correctly after 200X onwards :
- No exception when debugging
- OleDbCommand.ExecuteNonQuery successful ( return indicator 1 throughout entire loops )
- If put a break at 2000 row count then excel file .xlsm was generated successfully
- If put a break at 2010 row count then excel file .xlsm was generated but output file is blank ( exactly same as template file before insert any row/data )
- All processes, loops and connections are working fine.
- No data or formatting issue because the remaining rows having similar length and pattern

The scripts are similar to the below :

C#
public static bool fillExcelFile(string filePath, DataSet dataSet, string sheetName, string tempSheetName)
    {


        DataTable oledbdatatbl = null;
        using (OleDbDataAdapter oledbadap = new OleDbDataAdapter(string.Format("SELECT * FROM [{0}$]", sheetName),
                  string.Format(@"Data Source={0};Provider=Microsoft.ACE.OLEDB.12.0; Extended Properties=Excel 12.0;", filePath)))
        {
            try
            {
                oledbdatatbl = new System.Data.DataTable("ReportTable");
                oledbadap.Fill(oledbdatatbl);
            }
            catch (Exception ex)
            {
                Error_SendMail(ex);
                return false;
            }
        }






        System.Text.StringBuilder sbColumns = new System.Text.StringBuilder();
        int coloumsCount = 0;


        for (int icol = 0; icol < (oledbdatatbl.Columns.Count); icol++)
        {
            coloumsCount++;
            sbColumns.Append(string.Format(",[{0}]", oledbdatatbl.Columns[icol].ColumnName));

        }

        if (sbColumns.Length > 0)
            sbColumns.Remove(0, 1);




        using (OleDbConnection oledbConn = new OleDbConnection(string.Format(@"Data Source={0};Provider=Microsoft.ACE.OLEDB.12.0; Extended Properties=Excel 12.0;", filePath)))
        {
            using (OleDbCommand oledbCmd = new OleDbCommand())
            {
                oledbConn.Open();

                System.Text.StringBuilder sbValues = new System.Text.StringBuilder();
                int valuesCount = 0;

                for (int irow = 0; irow < dataSet.Tables[0].Rows.Count; irow++)
                {
                    if (sbValues.Length > 0)
                        sbValues.Remove(0, sbValues.Length);

                    try
                    {
                        valuesCount = 0;
                        for (int idatacol = 0; idatacol < dataSet.Tables[0].Columns.Count; idatacol++)
                        {
                            valuesCount++;

                            sbValues.Append(string.Format(",'{0}'", Convert.ToString(dataSet.Tables[0].Rows[irow][idatacol]).Contains("'") ?
                                Convert.ToString(dataSet.Tables[0].Rows[irow][idatacol]).Replace("'", "''") :
                                Convert.ToString(dataSet.Tables[0].Rows[irow][idatacol])));
                        }

                        if (sbValues.Length > 0)
                            sbValues.Remove(0, 1);
                    }
                    catch (Exception ex)
                    {
                        Error_SendMail(ex);
                        continue;
                    }


                    if (coloumsCount == valuesCount)
                    {
                        try
                        {
                            oledbCmd.CommandText = string.Format("insert into [{0}$]({1})values({2})", tempSheetName, sbColumns.ToString(), sbValues.ToString());
                            oledbCmd.CommandType = System.Data.CommandType.Text;

                            if (oledbConn.State == System.Data.ConnectionState.Closed)
                                oledbConn.Open();

                            oledbCmd.Connection = oledbConn;
                            oledbCmd.ExecuteNonQuery();
                        }
                        catch (Exception ex)
                        {

                            if (oledbConn.State == System.Data.ConnectionState.Open)
                                oledbConn.Close();
                            Error_SendMail(ex);
                        }
                    }
                }

                if (oledbConn.State == System.Data.ConnectionState.Open)
                    oledbConn.Close();
            }
        }
        return true;
    }
Posted
Updated 21-Jul-15 4:24am
v4
Comments
PIEBALDconsult 20-Jul-15 23:32pm    
Dunno, but I notice you keep setting the cmd.CommandText so I assume you are using string concatenation to put values in the SQL statement, and that's a bad idea.
Please use a parameterized statement, you can do that even with OleDb and the ACE engine, but you need to be careful, INSERT statements should be easy though.
Other than that, we'd need to see more of the code.
I have used ADO.net, the OleDb Provider, and the ACE engine to read and write Excel many times and not had issues like you describe.

Also, don't swallow the Exception, either do something with it ordon't catch it. You may be encountering an Exception without knowing it.
ajitsit07 21-Jul-15 2:32am    
Thanks for your reply. I have pasted the snippet of actual code. Please check and if I am doing any mistake please let me know.
PIEBALDconsult 21-Jul-15 10:29am    
I don't see anything that would be causing you the trouble you report, but I strongly suggest you use parameterized statements and use a DataReader rather than a DataAdapter, that may still help.
ajitsit07 22-Jul-15 1:53am    
I tried your advice,but no luck. Is there any memory limit in dumping data into the excel? Do you have any information? I did enough google too, many of the developers are facing this sticky problem, but no one has any solution. Please help to overcome this. Thanks.

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