Loop through Datareader and populate MailMessage Class Body Property Properly

I am attaching code.
I am using the mailmessage class and a txt file in App_Code. I want to do a sql query and embed 'tite' from the resultset into the body of the email.  

When I coded - body = body.Replace("##title##", titles)

I get the first 'title' back and nothing else. I added additional code -
Dim titles As String = reader("title").ToString()

Still get only first 'title' back from sql call. Checked sql call and count shows "6'.. which is what I would expect.
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.

omgangIT ManagerCommented:
Shouldn't it be

While reader.Read()
    count += 1
    Dim titles As String = reader("title").ToString()
    body = body.Replace("##title##", titles)
End While

Open in new window

check out SQLDataReader.Read method here  http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.read.aspx

OM Gang
omgangIT ManagerCommented:
Thanks.  I'm getting a bit on in years and failed to notice the code within the Word doc was VB.  Saw the topic area is C# and went with that.
OM Gang
mahpogAuthor Commented:
Omgang. I actually coded my original code as you show.  I still only get the first title back. I also have reviewed the reference you give, where I got original code syntax. I am starting to think it is the "dc-call-i.txt' file  used with for the mail body being the reason I see only the first 'title'.  I guess I need to define a loop inside the txt file?
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

mahpogAuthor Commented:
I dont mind the C#, I just converted to VB. Also, my problem is not the code in code behind, it is displaying the 6 'titles' in the dc-call-i.txt  file. I need to add a loop in it or something to display the results.
käµfm³d 👽Commented:
The Replace method will replace all instances of the target string within the source string. Your first loop iteration replaces all occurrences of "##title##"; by the time you get to the second iteration, there are no more "##title##" in the body.
omgangIT ManagerCommented:
If you switch to using the Replace string function instead you can then replace each ###title### one at a time.  You'd need to read the entire file contents into a string variable (e.g. use

strFileContents = My.Computer.FileSystem.ReadAllText("c:\MyFolder\dcall-i.txt")
strFileContents = Replace(strFileContents, "###title###", titles, , 1)

the , , 1) indicates to only replace the first occurrence of the search string.  Each time the While loop executes it will find the next occurrence of ###title### (because the previous occurrences were already changed).

OM Gang

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
mahpogAuthor Commented:
I am going to see if can do a collection or an array to get all the titles into one string and then output to 'title' in the replace.

thx for the help.

much to learn.
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

From novice to tech pro — start learning today.