?
Solved

Looping through Gridview

Posted on 2007-03-21
5
Medium Priority
?
3,009 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

777 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