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?

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.