Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I'm trying to read Excel File with 40 columns in my web apps ,but after running it ,I got this error.
CSS
Too many fields defined.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.OleDb.OleDbException: Too many fields defined.


My Code is this :

OleDbConnection oleconn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" +
         "Data Source=" + strFilePath + ";Jet OLEDB:Engine Type=5;" +
         "Extended Properties='Excel 8.0;IMEX=1;HDR=NO'");
        DataTable tbl = new DataTable();


        OleDbCommand cmdSelect = new OleDbCommand(@"SELECT * FROM [" + SheetName + "$]", oleconn);
        OleDbDataAdapter ole_adapter = new OleDbDataAdapter();
        ole_adapter.SelectCommand = cmdSelect;
        oleconn.Open();
        ole_adapter.Fill(tbl);
        oleconn.Close();
        ole_adapter = null;
        return tbl;
Posted

1 solution

Don't use * for retrieving the columns, rather use the column names with square brackets "[]" like below.
Try with some columns, then go for all.
C#
OleDbCommand cmdSelect = new OleDbCommand(@"SELECT [Column1], [Column2] FROM [" + SheetName + "$]", oleconn);
 
Share this answer
 
Comments
Alireza_1362 27-May-13 0:52am    
Thanks for your answer ,One thing is My Column Format is something like this: 0.0001-0.5000 ,how can I mention them in front of select command ,I have tried in several ways but didn't get correct result
I don't get you. Can you explain again ?
Alireza_1362 27-May-13 2:28am    
Hi again, I mean my column in excel file is something like this :0.0001-0.5000,0.5001-1.0000
,this is column name ,so in select command I should retrieve data in this way:
"SELECT Country ,[0.0001-0.5000],[0.5001-1.0000] FROM [" + SheetName + "$]" ,but it doesn't work ,I can't even change the column name ,because it's client file. so would you help me ,thanks
Is the column name [0.0001-0.5000] ?
Alireza_1362 27-May-13 2:35am    
yes

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