Solved

oledbexception : too many fields defined

Posted on 2004-09-30
8
2,436 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Events in static methods 3 50
Memory Usage 2 50
Authentication of Web Services 3 49
Code works but it's slow 23 30
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

912 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

17 Experts available now in Live!

Get 1:1 Help Now