Solved

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

Posted on 2011-03-24
5
231 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 262 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 238 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 262 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

Suggested Solutions

Title # Comments Views Activity
VBA Delete selected Worksheets of each Workbook in a Folder 4 42
Access Schema 6 45
need assistance with a VBscript 3 37
VB.NET System.NullReferenceException 1 19
When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

756 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