Solved

oledbexception : too many fields defined

Posted on 2004-09-30
8
2,427 Views
Last Modified: 2008-01-09
hi

I want to parse an excel file and store the results in a dataset.
I am using microsoft JET oledb for this.

the problem arises when there are extra columns in the excel file (even though they are not defined ), and i get the oledb exception: too many fields defined (when there are no columns in the excel file that are really "data"), since i am filling a dataset from a query for the above table selected.

say for example some excel file has 5 columns and 50 rows. every time i run my program on a new excel file that i receive, i need to make sure, all the EXTRA columns starting from column 6 to the end of whatever columns, are selected and then DELETED.

If i do the above step only my program runs fine or else ends up with the exception.

now to my question: is there a way from the program to let the program delete those EXTRA columns, so i dont end up in that exception that i get. ??
or else there is another way out of this situation and NOT get that exception.

thanks
messageman
0
Comment
Question by:messageforums
  • 3
  • 3
8 Comments
 
LVL 20

Expert Comment

by:TheAvenger
ID: 12198503
Can you show some code how you read the data from the excel file? The commands you execute, etc.?
0
 

Author Comment

by:messageforums
ID: 12201761
//Connect to Workbook
      OleDbConnection oleConn = new OleDbConnection();
      oleConn.ConnectionString = strConn;
      oleConn.Open();

//Sheet1 must be named accordingly
      OleDbDataAdapter oleCmd = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", oleConn);

      DataSet oDS = new DataSet();
                oleCmd.Fill(oDS);

      int oleRowCount = 1;
      //oDS.Tables.
      foreach(DataRow oleRow in oDS.Tables[0].Rows)
      {
                        // Do stuff here
                  }


one more thing is the field names in the excel have spaces in thehm (ex: "student ID", etc - in case we want to include them in the command). so i dont think we can put that as the field name

another issue is that i am selecting SHEET1. the program mandates me to name that particular worksheet to be Sheet1, inorder for my program to work. any way to escape this ?

thx
messageman
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12202277
Do you have any print ranges set?  You might be limited to 256 columns, and that could happen if you set a print range.

Bob
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:messageforums
ID: 12202452
hi ,


>>Do you have any print ranges set?  You might be limited to 256 columns, and that could happen if you set a print range.

can you please give me some code how to set it and the complete code that could be run and still satisfy the above requirements ?


thank you
messageman
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12202856
No, I mean that you shouldn't have any ranges set.  Excel interaction through OleDb is kind of stupid.

Bob
0
 

Author Comment

by:messageforums
ID: 12203761
hi bob

> Excel interaction through OleDb is kind of stupid.

I am new to all this stuff. so do excuse me for the quesitons. if interaction with excel through oledb is stupid. is there a better way. ? if so what way can i do it

please let me know.


messageman
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 60 total points
ID: 12203776
Maybe you can use Excel automation.   It might bring in the possibility of more hassles, but provides you with more specific information about columns, then the Jet engine provides.  Plus, you can do so many more things with it.  But, it also means that the client needs to have Excel.  So, if you are developing for an external client, then the OleDb is a simpler approach.

Bob
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now