• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 944
  • Last Modified:

Loop through dataset


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.

2 Solutions
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?

peternordbergAuthor Commented:
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?

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now