简体   繁体   中英

How to send to email (outlook) the selected items in SQL Server database using vb.net

I have 2 tables in my form, RegisteredSchedule and TodaySchedule . When the user inputs data such as Name, Gender, SchedueleDate and Incharge to my form and clicks the "Save" button, the data will go to the RegisteredSchedule table, and if the ScheduleDate set by users is equal to date Now , that record will show in TodaySchedule table and I want that selected data from SQL Server to be sent in an email - is that possible? Please help me I am a newbie.

Here is my code to if the ScheduleDate set by user is = DateNow . I want this select statement to be send in email also, not just show in table2.

Public Sub OnSchedule()
    Dim conn As New SqlConnection("SERVER=x\x;database = 
 x; user=x;pwd=x; ")

    conn.Open()
    Dim cmd As SqlCommand = conn.CreateCommand
    cmd.CommandText = String.Format("select PatientName,Gender,ScheduleDate,PersonInCharge from " _
    & "Schedule where ScheduleDate = CONVERT(date,getdate()) order by ScheduleDate")
    Dim dr As SqlDataReader = cmd.ExecuteReader()
    If dr.HasRows Then
        Dim dtSerial As New DataTable
        dtSerial.Load(dr)
        dgvOnSchedule.DataSource = dtSerial
    Else
        MsgBox("no data")

    End If
    dr.Close()
    conn.Close()
End Sub

here is my code in my email, i tried to put my select query in oMail.TextBody but didn't work. please suggest.

    Public Sub sendEmail()
    Dim oMail As New SmtpMail("TryIt")
    Dim oSmtp As New SmtpClient()
    oMail.To = New AddressCollection("x@x.co.th")
   oMail.Cc = New 
   AddressCollection("x@x.co.th,x@x.co.th")
   oMail.Subject = "test email from VB.NET project"

   'code below not work, what should i do to my oMail.textbody to show the 
   select condition ?

   oMail.TextBody = "On SChedule Date" & OnSchedule()



 Dim oServer As New SmtpServer("x.x.x.th")
   Try
        oSmtp.SendMail(oServer, oMail)
        MessageBox.Show("success")
    Catch ex As Exception
        MessageBox.Show("no success")
    End Try     
End Sub

On your TextBody assignment, you have called method OnSchedule() which does not return anything back, only assigns and populates a datagridview object

You can use a StringBuilder object

Then read DataGridView data using foreach loops and append those values to the stringbuilder object

StringBuilder sb = new StringBuilder();
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        foreach (DataGridViewCell cell in row.Cells)
        {
            sb.Append(cell.Value);
            sb.Append("\t");
        }
        sb.AppendLine();
    }
    textBox1.Text= sb.ToString();

As the final step, use this stringbuilder value in your assignment as follows

oMail.TextBody = "On SChedule Date" & sb.ToString()

For VB.NET code equivalent of above C# code, please use following

    Dim sb As New StringBuilder()
    For Each row As DataGridViewRow In DataGridView1.Rows
        For Each cell As DataGridViewCell In row.Cells
            sb.Append(cell.Value)
            sb.Append("\t")
        Next
    Next

Again you will use

oMail.TextBody = "On SChedule Date" & sb.ToString()

For creating the table as HTML object, you can use following code

Dim sb As New StringBuilder("<table>")
For Each row As DataGridViewRow In DataGridView1.Rows
    sb.Append("<tr>")
    For Each cell As DataGridViewCell In row.Cells
        sb.Append("<td>")
        sb.Append(cell.Value)
        sb.Append("</td>")
    Next
    sb.Append("</tr>")
Next
sb.Append("</table>")

This will create an HTML table script similar to following sample

<table><tr><td>1</td><td>Pre-School A</td></tr><tr><td>2</td><td>Pre-School B</td></tr><tr><td></td><td></td></tr></table>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM