Retrieve Email Address BoundField Data from Gridview in Code Behind

Posted on 2012-09-16
Last Modified: 2012-09-19
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

Question by:pposton
    LVL 20

    Expert Comment

    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.

    Author Comment

    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.
    LVL 20

    Assisted Solution

    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)
    LVL 83

    Accepted Solution

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    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 (…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    737 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

    19 Experts available now in Live!

    Get 1:1 Help Now