Loop through dataset

Posted on 2007-10-18
Last Modified: 2008-01-15

I'm trying to go through a dataset row by row to do some changes for each row. I tried this code using a treeview checkbox click to activate the procedure:
Protected Sub RadTreeView2_NodeCheck(ByVal o As Object, ByVal e As Telerik.WebControls.RadTreeNodeEventArgs) Handles RadTreeView2.NodeCheck
        If e.NodeChecked.Value > 1 Then

            Dim cImage As New imagesDL

            Dim i As Integer

            For Each i In cImage.GetImagesByNodeID(e.NodeChecked.Value)

                            Label1.Text = cImage.imageID


                 End If


    End Sub


When I do this I get this error:

Server Error in '/ArkenImages' Application.

Unable to cast object of type 'System.Data.DataSet' to type 'System.Collections.IEnumerable'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Unable to cast object of type 'System.Data.DataSet' to type 'System.Collections.IEnumerable'.

Source Error:

Line 96:             Dim i As Integer
Line 97:
Line 98:             For Each i In cImage.GetImagesByNodeID(e.NodeChecked.Value)
Line 99:
Line 100:                'ILoad1.Value = WebImage.Load(Server.MapPath("Uploads/images"), cImage.imageID)

Grateful to get help in how to be able to go through each row in the dataset to perform some actions.

Question by:peternordberg
    LVL 12

    Accepted Solution

    normally, to loop through rows in a datatable (dataset.tables(0)) you'd do something like

    For each row as datarow in dt.Rows


    For each row as datarow in dataset.tables(0).Rows

    but it sounds like you're wanting to loop through something else?


    Author Comment

    Hi, thanks for answer!

    I tried this and it didn't complain:

     Dim cImage As New imagesDL

                Dim row As DataRow
                Dim ds As DataSet = cImage.GetImagesByNodeID(e.NodeChecked.Value)

                For Each row In ds.Tables(0).Rows

                    'ILoad1.Value = WebImage.Load(Server.MapPath("Uploads/images"), cImage.imageID)
                    Label1.Text = cImage.imageID

    But how would I get the id for each row?

    LVL 22

    Assisted Solution

    The DataRow contains an array of objects, one for each column in the DataTable, in the the same order as the columns. So, say your ID is in column3, then inside the for loop, you can:
    id = row(3).ToString()


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
    In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    734 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

    22 Experts available now in Live!

    Get 1:1 Help Now