Solved

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

Posted on 2011-03-24
5
229 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
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

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

Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

929 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

11 Experts available now in Live!

Get 1:1 Help Now