For Each Next loop returns first row data for every row

gspeedtech

Member
Joined
Mar 13, 2010
Messages
11
Programming Experience
Beginner
I am trying to populate a dataset with data from a dynamic SQL dataset created by a code generator (PDSA). If I want the first row of data, or I use a specific "Where" clause to retrieve 1 row, I have no problem. But, when I loop through the dataset that has four entries, instead of getting the four entries, I get the first row 4 times. Any idea why?

Code Example:

VB.NET:
Dim DS_C as New DS
Dim dr_A As DS_C.Tbl_ARow		        

        Me.DS_C.Tbl_A.Clear()

        Dim bo As PDSA.DataLayer.tbl_BDC = New PDSA.BusinessLayer.tbl_B

        With bo
            .ConnectionString = AppConfig.SQLConnectString
            .SelectFilter = PDSA.DataLayer.tbl_BDC.SelectFilters.All
            .WhereFilter = tbl_BDC.WhereFilters.None
            .Load()
        End With        

        For Each dr As DataRow In bo.DataSet.Tables(0).Rows

            dr_A = DS_C.Tbl_A.NewRow

            With dr_A
                .CustomerID = bo.CustomerID
                .FirstName = bo.FirstName
                .LastName = bo.LastName
                .Street = bo.Street
                .City = bo.City
                .State = bo.State
                .ZipCode = bo.ZipCode
            End With

            DS_C.Tbl_A.AddTbl_ARow(dr_A)

        Next

If I try to change it to use dr instead of bo , it wont accept it:
.CustomerID = dr.CustomerID(CustomerID is not a member of System.Data.DataRow)
 
Last edited:
regarding error: "is not a member of System.Data.DataRow"

VB.NET:
        For Each dr As DataRow In bo.DataSet.Tables(0).Rows

            dr_A = DS_C.Tbl_A.NewRow

            With dr_A
                .CustomerID = bo.CustomerID
I am unfamiliar with "DS_C" perhaps these are custom classes? anyways regarding :
If I try to change it to use dr instead of bo , it wont accept it:
.CustomerID = dr.CustomerID(CustomerID is not a member of System.Data.DataRow)

I think the reason why it won't work is because you are casting the objects from "bo.DataSet.Tables(0).Rows" into the type "DataRow" in your for each loop. Then dr_A is of type "DS_C.Tbl_ARow". So if you casted the objects from "bo.DataSet.Tables(0).Rows" to type "DS_C.Tbl_ARow" then it should work, ONLY IF that is a valid cast of course... So something like
 For Each dr As DS_C.Tbl_ARow In bo.DataSet.Tables(0).Rows

            dr_A = DS_C.Tbl_A.NewRow

            With dr_A
                .CustomerID = dr.CustomerID
 
(bo, DS_C, dr_A, tbl_A) are alias's to replace actual names for confidentiality

If I try using DS_C.Tbl_ARow

I get type 'DS_C.Tbl_ARow' is not defined

For Each dr As DS_C.Tbl_ARow In bo.DataSet.Tables(0).Rows
 
Back
Top