Link to home
Start Free TrialLog in
Avatar of azyet24
azyet24Flag for United States of America

asked on

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?

Code:

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)
     objConn.open()
     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)
          objConn1.open()
          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
          objRdr1.close()
          objConn1.close()
   
     objMail.Body = strbody & "</body></html>"
     SmtpMail.SmtpServer = "mail.domain.com"
     SmtpMail.Send(objMail)              
     
     
        End While
     objRdr.close()
     objConn.close()
End Sub
Avatar of pauljk1619
pauljk1619

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.
Avatar of azyet24

ASKER

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.

Thanks,
Bobby
I am assuming the following line is for displaying name..

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


Is name not showing at all? Or what?
Inside first loop reiniailize the sbody variable.

sbody = ""
ASKER CERTIFIED SOLUTION
Avatar of naveenkohli
naveenkohli

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of azyet24

ASKER

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