Nested While Loop

Posted on 2006-04-26
Last Modified: 2012-06-21
I have an email that iterates through an Access Db to send a personalized email to all the email addresses in the db.  This is working perfectly.  Inside the While objRdr.Read(), I have a nested While loop that iterates through the events table in the database.  This also works fine, but unfortunatly when I put these two together, it sends the email to the right person and iterates through the events, but it doesn't display the correct name.  Is there a problem with the nexted while loop?


Sub Submit(s As Object, e As EventArgs)
     Dim objCmd As OleDbCommand
     Dim objRdr As OleDbDataReader
     Dim objCmd1 As OleDbCommand
     Dim objRdr1 As OleDbDataReader
     Dim sEmail As String
       Dim sName As String
     Dim sBody As String
     Dim seventdate As String
     Dim seventtime As String
     Dim seventtitle As String
     Dim seventdescription As String
     Dim objMail As New MailMessage
     dim strbody as string
     objMail.Subject="This is my Subject"
     Dim objConn As New OleDbConnection(ConfigurationSettings.AppSettings("TEST"))
     objCmd = New OleDbCommand("Select fname, lname, email From Members", objConn)
     objRdr = objCmd.ExecuteReader()    
     While objRdr.Read()
     sEmail = objRdr("email")
     sName =objRdr("fname") & " " & objRdr("lname")
       objMail.To = sEmail
     objMail.From = ""
     objMail.BodyFormat = MailFormat.HTML
   strbody &= "<html><head><title>" & "</title></head><body>"
     objMail.Body &= " "
       strbody &= "<p>" & "Dear " & sName &  ":" & "</p>"

     lblemail.text += "Dear: " & objRdr("fname") & " " & objRdr("lname") & " " & objRdr("email") & "<br />"   'testing purposes only

     Dim objConn1 As New OleDbConnection(ConfigurationSettings.AppSettings("DSN"))
          objCmd1 = New OleDbCommand("Select * From Events WHERE eventdate >= #" & Date.Today & "# ORDER by eventdate ASC", objConn1)

          objRdr1 = objCmd1.ExecuteReader()

          While objRdr1.Read()
          seventtitle = objRdr1("title")
          seventdate = objRdr1("eventdate")
          seventtime = objRdr1("eventtime")          
          seventdescription = objRdr1("description")
         strbody &= "Event: " & seventtitle & "<br />" & _
                              "Date: " & seventdate & "<br />" & _
                               "Time: " & seventtime & "<br />" & _
                               "<br />" & _
                               "Details: " & seventdescription & "<br />"
                       lblevents.text += "Event: " & seventtitle & "<br />" & _       'testing purposes only
                              "Date: " & seventdate & "<br />" & _
                               "Time: " & seventtime & "<br />" & _
                               "<br />" & _
                               "Details: " & seventdescription & "<br />"
       End while
     objMail.Body = strbody & "</body></html>"
     SmtpMail.SmtpServer = ""
        End While
End Sub
Question by:azyet24
    LVL 7

    Expert Comment

    Which name doesn't display correctly?  The correct person's name or the correct event names?  The name in the label or on the email or both?

    My first observation is that the way it's currently set up, all events will go to every user.  Is this correct?  If they are only to have specific events per user, you need a MemberID in the events table and change the where clause in your second select to filter by that MemberID.  You may not want this, but I thought I'd throw it out there.

    Author Comment

    The correct email is retrieved and sent, but each email should contain the name matching the email address in the db.  right now every email contains the same Dear name: (the first record's name).

    on the test , it out puts all the names and email addresses, then cycles through the events.  Not sure why it won't display first record's name, all events, then repeat.

    Every person should receive all of the events.

    LVL 23

    Expert Comment

    I am assuming the following line is for displaying name..

    "<p>" & "Dear " & sName &  ":" & "</p>"

    Is name not showing at all? Or what?
    LVL 23

    Expert Comment

    Inside first loop reiniailize the sbody variable.

    sbody = ""
    LVL 23

    Accepted Solution

    If the inner loop generates the same information for all users, then there is an efficient way of doing this..

    Run the inner loop first and generate the events information stuff in a string.

    And then run the user specific loop and then use the events description string for each email.

    Currently you are opening new conneciton in inner loop for every single user.

    Author Comment

    Wow, moving that inner loop and then assigning the values to a string really speed up it's execution.  thanks for showing me that.  

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now