Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am reading an excel spreadsheet in C# using OleDb connection. I get an error (See below) if any of the column headers in the excel is "Domain".

The error message is (raw)
System.Data.OleDb.OleDbException (0x80004005): IErrorInfo.GetDescription failed with E_FAIL(0x80004005).
   at 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, Strin
g srcTable, IDbCommand command, CommandBehavior behavior)
   at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command,
CommandBehavior behavior)
   at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet)
   at ExcelReader.Program.Main(String[] args) in C:\Users\Eser\Development\ExcelReader\ExcelReader\Program.cs:line 32

More message that I gathered from the Exception handling:

Message: IErrorInfo.GetDescription failed with E_FAIL(0x80004005).
Native: -533136361
Source: IErrorInfo.GetSource failed with E_FAIL(0x80004005).
SQL: 3000

------------------------------------------------------------------
The c# statements are (not working!!!) and the excel column header contains "Domain":

C#
OleDbCommand objCmdSelect = new OleDbCommand("SELECT DISTINCT * FROM [Sheet1$A1:F1228] WHERE Domain = 'Finance' AND Discipline = 'Taxation'", objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
objAdapter1.SelectCommand = objCmdSelect;
DataSet ds1 = new DataSet();
objAdapter1.Fill(ds1);


------------------------------------------------------------------
The c# statements are (working!!!) and the excel column header now contains "Domaine" or anything else (anything other than Domain):

C#
OleDbCommand objCmdSelect = new OleDbCommand("SELECT DISTINCT * FROM [Sheet1$A1:F1228] WHERE Domaine = 'Finance' AND Discipline = 'Taxation'", objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
objAdapter1.SelectCommand = objCmdSelect;
DataSet ds1 = new DataSet();
objAdapter1.Fill(ds1);


Any suggestion/idea why this header keyword not being accepted by the query?

Eser

[edit]code blocks added[/edit]
Posted
Updated 11-Nov-12 4:36am
v2

Thanks Milind. Your solution solved the issue.
 
Share this answer
 
Comments
MT_ 13-Nov-12 4:21am    
Your welcome. Glad it helped. Please mark the solution as answer/upvote.
Hi,

I suspected that Domain is reserved word. Hence I searched on google a bit and found that it is indeed a reserved word.

http://support.microsoft.com/kb/321266[^]

So what you need to do is use square bracket around as shown below.
OleDbCommand objCmdSelect = new OleDbCommand("SELECT DISTINCT * FROM [Sheet1$A1:F1228] WHERE [Domain] = 'Finance' AND Discipline = 'Taxation'", objConn);


Note the square bracket around word domain.

This should solve the problem.

Hope this helps, If it does, mark the answer and/or upvote.
Milind
 
Share this answer
 

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