Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Looping through Gridview

Posted on 2007-03-21
5
Medium Priority
?
3,020 Views
Last Modified: 2013-11-26
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"
        End If
        Try
            For Each gvr As GridViewRow In gvClientRole.Rows
                Dim uEmail As Label = DirectCast(gvClientRole.SelectedRow.FindControl("lblEmail"), Label)
                varEmail = uEmail.Text
                MailHelper.SendMailMessage("admin@web.com", uEmail.Text, "web@web.com", "", subj, msgBod)
            Next
        Catch ex As Exception
            lblInfo.Text = "Send Failed."
        End Try

        lblInfo.Text = "Mail sent to all people in " & typRole & "."


    End Sub
0
Comment
Question by:wdarnellg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 6

Expert Comment

by:Rajesh_mj
ID: 18769427
Hi,

You have to filter the datset  which is set to the DataGridView.Thats the better thing
0
 

Author Comment

by:wdarnellg
ID: 18769465
I have a filter on the dataset and it is getting the result I want, except that I can't make the loop work to place each email in my 'send email' function's variable.
This is the query in the sqldatasource:
SELECT aspnet_Users.UserName, aspnet_Roles.RoleName, aspnet_Membership.Email FROM aspnet_Membership INNER JOIN aspnet_Users ON aspnet_Membership.UserId = aspnet_Users.UserId INNER JOIN aspnet_UsersInRoles ON aspnet_Membership.UserId = aspnet_UsersInRoles.UserId INNER JOIN aspnet_Roles ON aspnet_UsersInRoles.RoleId = aspnet_Roles.RoleId WHERE (aspnet_Roles.RoleName = N'Client')
0
 

Author Comment

by:wdarnellg
ID: 18769539
I cleaned up this code a bit.



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


        If rblChooseRole.SelectedValue = "Client" Then
            typRole = "Client"
        ElseIf rblChooseRole.SelectedValue = "ClientAndParent" Then
            typRole = "ClientAndParent"
        End If
        Try
            For Each gvr As GridViewRow In gvClientRole.Rows
                Dim uEmail As Label = DirectCast(gvClientRole.SelectedRow.FindControl("lblEmail"), Label)
                MailHelper.SendMailMessage("admin@dstroketennis.com", uEmail.Text, "dstroke@dstroketennis.com", "", subj, msgBod)
            Next
        Catch ex As Exception
            lblInfo.Text = "Send Failed."
        End Try

        lblInfo.Text = "Mail sent to all people in " & typRole & "."


    End Sub
0
 
LVL 15

Accepted Solution

by:
igor_alpha earned 2000 total points
ID: 18769572
Hi, wdarnellg!
In loop instead of getting Label control of each GridViewRow you get it from only gvClientRole.SelectedRow.
Instead of:
 For Each gvr As GridViewRow In gvClientRole.Rows
                Dim uEmail As Label = DirectCast(gvClientRole.SelectedRow.FindControl("lblEmail"), Label)
                MailHelper.SendMailMessage("admin@dstroketennis.com", uEmail.Text, "dstroke@dstroketennis.com", "", subj, msgBod)
            Next
Must be:
 For Each gvr As GridViewRow In gvClientRole.Rows
                Dim uEmail As Label = DirectCast(gvr.FindControl("lblEmail"), Label)
                MailHelper.SendMailMessage("admin@dstroketennis.com", uEmail.Text, "dstroke@dstroketennis.com", "", subj, msgBod)
            Next
0
 

Expert Comment

by:wppiexperts
ID: 20358995
*removed by poster's request by Netminder 1 Dec 2007*
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question