Solved

Looping through Gridview

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Needing an event fired when a row is added on a datagridview 4 35
Loop not working 29 47
SQL Login 17 37
Allow user to hide and show datagridview columns 4 17
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

930 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

12 Experts available now in Live!

Get 1:1 Help Now