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.


     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 = ""
                    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"
                End If


        End If
    End Sub

Open in new window

Who is Participating?
You have two options

1) Given that you already have the ID, get the email from the database for that ID.

2) Instead of ID, pass the row index as the command argument. Then you can retrieve any cell value using that index. See example
Obadiah ChristopherCommented:
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.
ppostonPresident/OwnerAuthor Commented:
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.
Obadiah ChristopherCommented:
If TypeOf colField Is BoundField Then
                    Dim email As BoundField
                    email = CType(email, BoundField)

Should be

If TypeOf colField Is BoundField Then
                    Dim email As BoundField
                    email = CType(colField, BoundField)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.