Hello Friends,
One help needed....
I have a product database....in which I have productid,prodname,inventoryvalue,reordervalue
I have to design a email system that as soon as the product reaches the reorder level it shud generate an email....
but the condition is there will be many products in the database....and on one button click it should send only one email but with all product names those reached the reorder level....
I tried the below thing but completely a waste...
Please do help me....
Private Sub btnEmail_Click(sender As System.Object, e As System.EventArgs) Handles btnEmail.Click
'Open Connection
Dim reorder As Integer
Dim inventory As Integer
Dim prodname As String
Dim prodcount As Integer
'get the total number of product in database
Try
Dim myCommand1 As SqlCommand
myCommand1 = New SqlCommand("SELECT count(*) as 'Total' FROM [Product]", Connection)
Dim reader1 As SqlDataReader = myCommand1.ExecuteReader
While reader1.Read()
prodcount = reader1("Total")
End While
Debug.Print(prodcount.ToString)
reader1.Close()
'get the values of product names in a array
Dim ds As New DataSet
Dim sqlstring As String
sqlstring = "select ProdName from Product"
Dim da As SqlDataAdapter = New SqlDataAdapter(sqlstring, Connection)
da.Fill(ds, "Product")
Dim temp As Integer = ds.Tables("Product").Rows.Count
Dim arr1() As String
For i = 0 To temp - 1
arr1(i) = ds.Tables("Product").Rows(i).Item("ProdName")
Next
For i As Integer = 1 To prodcount
prodname = arr1(i)
Debug.Print("in for: " + i.ToString)
Dim myCommand As SqlCommand
myCommand = New SqlCommand("SELECT * FROM [Product] where ProdName='" + prodname + "'", Connection)
Dim reader As SqlDataReader = myCommand.ExecuteReader
reader.Read()
prodname = reader("ProdName")
reorder = reader("ReOrderLimit")
inventory = reader("Inventory")
Debug.Print("prodname: " + prodname)
Debug.Print("Inventory: " + inventory.ToString)
Debug.Print("reorder: " + reorder.ToString)
If inventory <= reorder Then
Dim body As String = "Please send these medicines: " + prodname
Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()
SmtpServer.Credentials = New Net.NetworkCredential("abc@def.com", "abc123def")
SmtpServer.Port = 28
SmtpServer.Host = "mail.abc.com"
mail = New MailMessage()
mail.From = New MailAddress("abc@def.com")
mail.To.Add("abc@def.com")
mail.Subject = "Lack of List of medicines"
mail.Body = body
SmtpServer.Send(mail)
MsgBox("mail sent sucessfully for reorder")
End If
reader.Close()
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
'Close connection
End Sub