Solved

Looping through Gridview

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now