Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi need to send data from ms sql table

Currently I can send one by one items.

below is my code. It is working. pls advice me

What I have tried:

This code working fine but one by one....

SQL
Private Sub SendEmail()
 
         Dim body As String = Me.PopulateBody
         Me.SendHtmlFormattedEmail(Session("email"), ".", body)
         Session.Remove("email")
End Sub

Private Sub SendHtmlFormattedEmail(ByVal recepientEmail As String, ByVal subject As String, ByVal body As String)
    Dim client As New SmtpClient()
    Dim mail As New MailMessage()
    mail.Body = body
    mail.[To].Add(recepientEmail)
    mail.Subject = "THE STOCK LOG"
    mail.IsBodyHtml = True
    client.Send(mail)
End Sub

Private Function PopulateBody() As String
       Dim data As SqlDataReader
       cmd = New SqlCommand
       cmd.Connection = conn
       cmd.CommandText = ("Select itemid, itemdes, balqty from tbl_stock_bal where itemid ='" & Me.txttItemID.Text & "'")
       cmd.CommandType = CommandType.Text
       conn.Open()
       data = cmd.ExecuteReader
 
       If (data.Read) Then
           Dim itemid As String = data.Item("itemid")
           Dim itemdes As String = data.Item("itemdes")
           Dim qty As String = data.Item("balqty")
 
 
 
 
       Dim body As String = String.Empty
       'Dim mail As New MailMessage()
       Dim reader As StreamReader = New StreamReader(Server.MapPath("Materials.html"))
       body = reader.ReadToEnd
       body = body.Replace("{Date}", Date.Now)
       body = body.Replace("{itemid}", itemid)
       body = body.Replace("{itemdes}", itemdes)
       body = body.Replace("{balqty}", qty)
       Session("email") = "maideen5@hotmail.com"
       Return body
End If
       cmd = Nothing
       conn.Close()
   End Function
Posted
Updated 6-May-16 1:36am
Comments

1 solution

If you want to do multiple rows then just think about what you would have to do. You put your code into a loop and loop through each row of your results. You'll probably want to use a StringBuilder and for each row of data you can create a string for your
<table><tbody><tr></tr></tbody></table>
and manually build an html table.
 
Share this answer
 
Comments
Maideen Abdul Kader 9-May-16 21:10pm    
Hi Ryan
thank you. I have tried as per your advice. But it is so long because it has more then 200 items and two hundred line in email
But Now I have done email attached PDF. Thank you for your reply.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900