Looping through Gridview
Posted on 2007-03-21
I am trying to collect email addresses from a gridview in asp.net in order to send emails to users in specific roles. I am using a sqldatasource that has populated the gridview, and the code I am using works as long as the selected index is set to 0. But that only gets me the same email address repeatedly as the loop iterates (or tries to) through its row count. I need to re-select each row I am guessing, but no clue how to do that programatically. Any help? worth 500
Here is the code:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSendMail.Click
Dim subj As String = txtSubject.Text
Dim msgBod As String = txtBody.Text
Dim typRole As String
Dim varEmail As String
Dim i As Integer
If rblChooseRole.SelectedValue = "Client" Then
typRole = "Client"
ElseIf rblChooseRole.SelectedValue = "ClientAndParent" Then
typRole = "ClientAndParent"
For Each gvr As GridViewRow In gvClientRole.Rows
Dim uEmail As Label = DirectCast(gvClientRole.SelectedRow.FindControl("lblEmail"), Label)
varEmail = uEmail.Text
MailHelper.SendMailMessage("email@example.com", uEmail.Text, "firstname.lastname@example.org", "", subj, msgBod)
Catch ex As Exception
lblInfo.Text = "Send Failed."
lblInfo.Text = "Mail sent to all people in " & typRole & "."