Vb.net – Looping through a strongly typed DataSet

datasetdatatablestrongly-typed-datasetvb.net

How do you loop through a strongly typed DataSet?

We have the following defined in the DataSet designer:

DataSet name:      DataSetSchedules
DataTable name:    DataTableSchedules
TableAdapter name: DataTableDataAdapterSchedules

This is a code sample I found using Google:

Dim col As DataColumn
Dim dt as DataTable
Dim dr as DataRow
Dim strMyValue AS String = ""

dt = ds.Tables(0)

For Each dr In dt.Rows
   For Each col In dt.Columns
    StrMyValue = dr(col.ColumnName)
   Next
Next

Since a DataSet was already created in the DataSet desiner, I tried this:

Dim col As DataColumn
Dim dt as DataTable
Dim dr as DataRow
Dim strMyValue AS String = ""

dt = DataSetSchedules.Tables(0)

Intellisence told me that "Tables" was not a choice so I'm stuck.

Most of the code samples I found show that this is how to do it, but I don't think that this applies to strongly typed DataSets.

Can you show the correct coding needed to loop through DataSetSchedules and get the value in dr(col.ColumnName)?

Best Answer

Try This :

 Dim DS as new DataSetSchedules

    DT = DS.Tables(0)

 For Each DR as DataRow In DT.Rows
   ' Code
 Next

or as @Tim Suggested use the strongly typed Tables. see MSDN

A typed DataSet is a class that derives from a DataSet. As such, it inherits all the methods, events, and properties of a DataSet. Additionally, a typed DataSet provides strongly typed methods, events, and properties. This means you can access tables and columns by name, instead of using collection-based methods