[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 671
  • Last Modified:

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

0
pposton
Asked:
pposton
  • 2
2 Solutions
 
informaniacCommented:
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
0
 
ppostonAuthor 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.
0
 
informaniacCommented:
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)
0
 
CodeCruiserCommented:
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

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewcommandeventargs.aspx
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now