Solved

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

Posted on 2011-03-24
5
228 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
  • 2
  • 2
5 Comments
 
LVL 16

Assisted Solution

by:ToddBeaulieu
ToddBeaulieu earned 262 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Depends on whether he wants all tables, or just the one.
0
 

Author Comment

by:Mr_Shaw
Comment Utility
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
Comment Utility
thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
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: …

771 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

14 Experts available now in Live!

Get 1:1 Help Now