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
azyet24Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pauljk1619Commented:
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.
0
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.

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

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


Is name not showing at all? Or what?
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

naveenkohliCommented:
Inside first loop reiniailize the sbody variable.

sbody = ""
0
naveenkohliCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.  
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.