Reputation: 686
On production system we are SOMETIMES getting below error message while reading excel into datatable (same code same file will not work today but will on other day) .
System.Data.OleDb.OleDbException (0x80004005): Invalid argument. System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr) at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) at System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior) at
System.Data.OleDb.OleDbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior) at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) at System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior) at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)
But the problem is that on production server it work well today but tomorrow or day after tomorrow it will not work well and then again it will start working well.
Below is the code.
string ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + readFilePath + ";Extended Properties=\"Excel 12.0;\"";
ExcelConnection = new OleDbConnection(ConnectionString);
string ExcelQuery = "Select FORMAT(SAMPDATE,'dd/MM/yyyy') as SAMPDATE,FORMAT(LANDED_ON,'dd/MM/yyyy') as LANDED_ON,FORMAT(RECDATE,'dd/MM/yyyy') as RECDATE,* from [Sheet1$]";
ExcelCommand = new OleDbCommand(ExcelQuery, ExcelConnection);
ExcelConnection.Open();
ExcelAdapter = new OleDbDataAdapter(ExcelCommand);
ExcelAdapter.Fill(dtbExcelData);
ExcelConnection.Close();
I have also checked value of variable readFilePath it is correct , example value is D:\Cop\Web\ABC\PAL\FIleUploaded\ER01.xls.
I am not sure why exactly same code does not allow same excel document to get uploaded but next day same code same file works without any problem. Can somebody help me?
Upvotes: 1
Views: 1461
Reputation: 16397
Several suggestions.
If you run something as simple as this, it will purge any open instance, even those you can't see:
foreach (var process in Process.GetProcessesByName("Excel"))
{
process.Kill();
}
It will mean more work on your end to read the contents and turn it into a datatable, but it also creates an opportunity to skip the datatable all together, which might make sense, depending on what you are trying to do.
Two things to try here is first, use a datareader instead of a datatable. This allows you to not assume anything.
In this example, I think column A is a date, but rather than assume anything I render it as a string and use DateTime.TryParse
to test it.
ExcelCommand = new OleDbCommand(ExcelQuery, ExcelConnection);
OleDbDataReader reader = ExcelCommand.ExecuteReader();
DateTime orderDate;
while (reader.Read())
{
string colA = reader.GetValue(0).ToString();
if (DateTime.TryParse(colA, out orderDate))
{
// do something with orderDate here
}
}
reader.Close();
Honestly, I'm not sure if this will work, but it might be worth a shot if you are not 1,000% sure it's not a content issue.
Clearly, this is a lot more work than just slamming the contents into a datatable, but to be honest I've never been a fan of datatables anyway.
Upvotes: 1