Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 610
  • Last Modified:

Nested While Loop

I have an asp.net 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 = "webmaster@domain.com"
     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 = "mail.domain.com"
        End While
End Sub
  • 3
  • 2
1 Solution
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.
azyet24Author Commented:
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.

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

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

Is name not showing at all? Or what?

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Inside first loop reiniailize the sbody variable.

sbody = ""
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.
azyet24Author Commented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now