I am trying to look through each row in my data table and if a condition is not met, a messagebox appears. (Eventually, instead of the messagebox, I will populate to a DGV.

For Each eventRow As DataRow In PFC_Xpress.Tables("EVENT INFO").Rows
             If eventRow.Item("Incident Type") = "Party" Then
                    For Each personRow As DataRow In PFC_Xpress.Tables("Person").Rows
                     If personRow.Item("Event Association") = "Birthday" Then
                         MessageBox.Show("my own message")
                     Else
                        MessageBox.Show("Form Validated")
                     End If
                 Next
             End If
         Next

When I go through debugging, the program only goes through the first 'For Each' and never passes through the rest of the script. Can anyone provide some help/insight?

Dani AI

Generated

Brief summary and a few practical fixes (adds to , and ):

The reason you saw nothing at first was exactly what found — the Person table had no rows until the form was saved. Other common traps: comparing an object that can be DBNull, column names with spaces, and matching rows from the wrong event (you may be checking every Person row instead of only those related to the current EVENT INFO row).

If the goal is "show one result if any person for this event has Event Association = 'Birthday'", filter first and act once. Using a DataView RowFilter is simple and fast and avoids per-row MessageBoxes:

Dim dv As New DataView(PFC_Xpress.Tables("Person"))
dv.RowFilter = "[Event Association] = 'Birthday' AND [EventID] = " & eventId  ' adapt to your FK column
If dv.Count > 0 Then
    MessageBox.Show("Birthday found for this event")
Else
    MessageBox.Show("No birthday found — form validated")
End If

To populate a DataGridView with the matching people, either bind the filtered DataView or copy matching rows into a new DataTable:

Dim matches() As DataRow = PFC_Xpress.Tables("Person").Select("[Event Association] = 'Birthday' AND [EventID] = " & eventId)
If matches.Length > 0 Then
    Dim results As DataTable = PFC_Xpress.Tables("Person").Clone()
    For Each r As DataRow In matches
        results.ImportRow(r)
    Next
    DataGridView1.DataSource = results
End If

Extra tips: use brackets around column names with spaces, guard against DBNull (use DataRow.IsNull or DataRow.Field(Of String)), do case-insensitive compares or Trim() the values, and if you must loop, use Exit For when you find the first match to avoid multiple popups. If Person rows are children of EVENT INFO in your DataSet, prefer a DataRelation and GetChildRows — that keeps your queries scoped to the current event and avoids accidental matches from other events.

Recommended Answers

All 5 Replies

Have you thought of checking PFC_Xpress.Tables("EVENT INFO").Rows.count to check if there are any rows in your table?

Hi,
Just realize that your code may have issue of Column name so when you are fetching from data it would be great if you can remove space between the columnname. like "Event INFO" should be Event_info.

You can optimize the code with table filter "SELECT" like as i mention in below code

Sorry for c# code you can convert it into VB.NET
               foreach(DataRow dr in PFC_Xpress.Tables["EVENT INFO"].Select("[Incident Type]='Party'")
               {
                   foreach (DataRow personRow in PFC_Xpress.Tables["Person"].Rows){
                       if(personRow["[Event Association]"]=="Birthday"){
                            MessageBox.Show("my own message");
                       }
                       else {
                           MessageBox.Show("Form Validated");
                       }
                   }
               }

Well, I got part of the problem fixed. Perplexed B, you were right about the rows. The way my code was written, I would first have to save the form and then I could go through the datarows to search.

The problem I'm having with the code is if there is more than one person and 'Birthday' is listed as the 3rd person, I get a message box still with the other two people. How can I word this so I will only receive one messageboxif the condition is met?

Not totally sure what it is you're trying to achieve, but if the idea is to have a message box for each person who has his birthday, and if no-one has a birthday to validate your form (admittedly not the most likely scenario you must have in mind :) )then you could think of this appraoch:

dim party as boolean = false
For Each eventRow As DataRow In PFC_Xpress.Tables("EVENT INFO").Rows
             If eventRow.Item("Incident Type") = "Party" Then
                    For Each personRow As DataRow In PFC_Xpress.Tables("Person").Rows
                     If personRow.Item("Event Association") = "Birthday" Then
                        party = true
                         MessageBox.Show("my own message")
                     End If
                 Next
             End If
         Next
         If party = false then
            MessageBox.Show("form gets validated")
        End If

Let me know if something is not clear, or if your scenario is different.

Good luck.

That's it. Thanks Perplexed. Sometimes I rack my brain so hard, I forget the simple things. Now it's just a matter of figuring out how to setup and write to my data grid view instead of a message box.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.