Link to home
Start Free TrialLog in
Avatar of pposton
ppostonFlag for United States of America

asked on

Retrieve Email Address BoundField Data from Gridview in Code Behind

Here's some background for the issue.  I have a gridview which displays applications for membership to our site.  When I click the "Approve" button, it basically transfers data from the application table to the users table.  Also, I would like, when the "Approve" button is fired, to send an html email to the user letting them know they are now approved to use the site.

Below is my (relevant) code for firing off the email. My problem is that in the line "email = CType(email, BoundField)", intellisense says that the second "email" ...Variable email is used before it has been assigned.  A Null reference exception could result at runtime.  I suspect it has something to do with the line "strTo.DataField" but I'm not sure.

Any ideas are appreciated.

Thanks!!

     Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)      
If e.CommandName = "Approve" Then

            Dim gv As GridView = GridView1
            Dim colFields As DataControlFieldCollection = gv.Columns
            For Each colField As DataControlField In colFields
                If TypeOf colField Is BoundField Then
                    Dim email As BoundField
                    email = CType(email, BoundField)

                    Dim strFrom = "support@charityck.com"
                    Dim strTo = email
                    Dim MailMsg As New MailMessage(New MailAddress(strFrom), New MailAddress(strTo.DataField))

                    Dim Body As String ="a bunch of html code"
                    MailMsg.BodyEncoding = Encoding.Default
                    MailMsg.Subject = "Welcome!"
                    MailMsg.Body = Body
                    MailMsg.Priority = MailPriority.High
                    MailMsg.IsBodyHtml = True
                    'Smtpclient to send the mail message
                    Dim SmtpMail As New SmtpClient
                    SmtpMail.Host = "localhost"
                    SmtpMail.Send(MailMsg)
                End If

            Next

            Response.Redirect("adminApplications.aspx")
        End If
    End Sub

Open in new window

Avatar of Obadiah Christopher
Obadiah Christopher
Flag of India image

I suggest you use the CommandArgument  for the button.

Set the CommandArgument to the to email-id.

And in the RowCommand event just set it.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand(v=vs.80).aspx

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.commandargument.aspx
Avatar of pposton

ASKER

I'm new at this stuff, but I don't think I can do that.  I'm already using the commandargument on the button to pull the id for each record so that it can run the stored procedure to approve the application.
SOLUTION
Avatar of Obadiah Christopher
Obadiah Christopher
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial