Solved

Looping through Gridview

Posted on 2007-03-21
5
2,984 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
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 500 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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

730 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