Solved

How to call asp.net GridView's column into vb.net code?

Posted on 2011-02-11
5
843 Views
Last Modified: 2013-11-26
Hello experts,

I have an asp.net database, with email field. And, now I want to get that field, gridview's email column, into vb.net code. I have a code for sending mail, and want to replace Send To(test@yahoo.com) with that field.

Dim Email As New MailMessage("test@gmail.com", "test@yahoo.com", "TEST", body)

Open in new window


Thank you in advance for help to anyone.
0
Comment
Question by:dejandejanovic
  • 3
  • 2
5 Comments
 
LVL 3

Expert Comment

by:jmro20
ID: 34874947
If there are multiple records then something like this:
 
For Each row As GridViewRow In GridView1.Rows
            Dim Email As New MailMessage("test@yahoo.com", row.Cells("YourEmailColumnName").ToString, "TEST", body)
Next

Open in new window

0
 
LVL 3

Accepted Solution

by:
jmro20 earned 500 total points
ID: 34874968
If there is only one record in grid then:
Dim Email As New MailMessage("test@yahoo.com", GridView1.Rows(0).Cells("YourEmailColumnName").ToString, "TEST", body)

Open in new window

0
 

Author Comment

by:dejandejanovic
ID: 34877709
Thank you for reply. I have add your solution, but now get this code error:

Value of type 'System.Web.UI.WebControls.TableCellCollection' cannot be converted to 'String'.

For info my code is beggins with:
Imports System.Net.Mail
Partial Class details
    Inherits System.Web.UI.Page

Any idea?
0
 

Author Comment

by:dejandejanovic
ID: 34878239
Oh, ignore abow message. I didnt ad ToString.

But, still my email form does not send message os I would like to give a picture how my database is design, and work.
1. - From Search.aspx page I have gridview which is filtering by a few Dropdownlists.
    - When select a record from gridview it jump to another page Details.aspx.
2. - Details.aspx contain more informations about this select record.
    - in the end of page I have mail form. First field of this form is EmailGridview2 for which I have send this help (see printscreen).
3. This is my Send mail code:
Protected Sub sendmail(ByVal from As String, ByRef body As String)
        Dim Email As New MailMessage("test@gmail.com", EmailGridView2.Rows(0).Cells("email").ToString, "Subject Testi", body)

        Dim smtpclient As New SmtpClient
        smtpclient.Host = "smtp.gmail.com"
        smtpclient.EnableSsl = True

        Dim credentials As New System.Net.NetworkCredential("SmtpUserID", "SmtpPassword")
        smtpclient.Credentials = credentials
        smtpclient.Send(Email)

        lblValidation.Text = "Success"
        lblValidation.ForeColor = Drawing.Color.DarkGreen
        txtCaptcha.Text = ""
        TextBox1.Focus()
    End Sub

Open in new window


4. I'm still learning asp.net vb.net programming, and maybe do not understand some things. And I hope that I have used right solution, as Detail.aspx display only 1 record. So, I have used your second solution.
printscreen.png
0
 

Author Closing Comment

by:dejandejanovic
ID: 34879738
Found solution. I was just need to add .text.

GridView1.Rows(0).Cells("YourEmailColumnName").text.ToString

Thanks Jmro20.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

785 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