Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

I am not sure how to write this For Each loop properly?

Posted on 2011-03-24
5
Medium Priority
?
235 Views
Last Modified: 2012-05-11
I am getting an error reading: Unable to cast object of type 'System.Data.DataTable' to type 'System.Data.DataRow'.

From my code:

   Dim rows As DataRow
        For Each rows In DtSet.Tables
            Me.myOutPutBuffer.AddRow()
            Me.myOutPutBuffer.HotelName = rows.Table.Columns(1).ToString()
        Next

I am not sure how to write this For Each loop properly?
0
Comment
Question by:Mr_Shaw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 16

Assisted Solution

by:ToddBeaulieu
ToddBeaulieu earned 1048 total points
ID: 35209050
You're enumerating the tables, not the rows.

Dim row As DataRow
For Each row In DtSet.Tables(someTableIndex).Rows
    Me.myOutPutBuffer.AddRow()
    Me.myOutPutBuffer.HotelName = row.Columns(1).ToString()
Next

0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 952 total points
ID: 35209091
I think it should be something like this:

Dim table As DataTable
Dim row As DataRow
For Each table In DtSet.Tables
	For Each row In table.Rows
		Me.myOutPutBuffer.AddRow()
		Me.myOutPutBuffer.HotelName = row(1).ToString()
	Next
Next

Open in new window

0
 
LVL 16

Assisted Solution

by:ToddBeaulieu
ToddBeaulieu earned 1048 total points
ID: 35209102
Depends on whether he wants all tables, or just the one.
0
 

Author Comment

by:Mr_Shaw
ID: 35210192
I am working with SQL Server 2005 SSISI.

 am connecting to a Excel Datasource.

I want to extract the rows from an excel spreadsheet (as long as they have data).
0
 

Author Closing Comment

by:Mr_Shaw
ID: 35230505
thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

704 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