Let me preface this by saying I hope someone understands what i am trying to do here. I would really appreciated someones help.
I am trying to send an email(Auto email function) using items in a list box. You can select one or more items to send. The problem I am having is it keeps selecting the first item in the list box instead of what I selected(which could be more than one item). I am setting this equal to a variable called cities where. How do I get this to work to show the items I selected on the To = "" & cities where & "" line. Here is my code for the email:
Code for the Dropdown list selected change:
When I click on the send button it put the first item list in the listbox in the to: email. When it should put what I select as the to: email.
For example, the listbox will have let's say 4 names and email addresses:
John(john's email address)
Marc(marc's email address)
Bill(bill's email address)
Steve(steve's email address)
I select Bill or Bill and Steve but instead of selecting Bill or Bill and Steve's email address it will select John's email address to send to when I didn't select John in the list box when i go through the for/next loop because it is the first item listed.
BTW I do have the list box set for multiple selection.
I know this is a lot to absorb but I felt I needed to include as much as I can so someone could understand what was going on.
I am trying to send an email(Auto email function) using items in a list box. You can select one or more items to send. The problem I am having is it keeps selecting the first item in the list box instead of what I selected(which could be more than one item). I am setting this equal to a variable called cities where. How do I get this to work to show the items I selected on the To = "" & cities where & "" line. Here is my code for the email:
Code:
Function AutoEmail()
Dim objMailMessage As MailMessage
Dim strHtmlBody As String
conn = New SqlClient.SqlConnection(strconn)
conn.Open()
Dim currentAll As String
Dim strSQL = "SELECT d.group_descr, a.first_name, a.last_name, a.user_id, b.email, d.group_id, d.parent_group_id FROM icc_user_data AS a LEFT OUTER JOIN icc_users AS b ON a.user_id = b.user_id LEFT OUTER JOIN icc_users_groups AS c ON a.user_id = c.user_id LEFT OUTER JOIN icc_groups AS d ON c.group_id = d.group_id WHERE (b.user_id NOT IN (249)) AND (d.group_id IS NOT NULL) AND (d.group_id <> '8') AND (d.group_id <> '10') AND (d.group_id <> '46') AND (d.group_id <> '3') AND (d.group_id <> '47') AND (d.group_id <> '9') ORDER BY d.group_descr"
Dim Com As New System.Data.SqlClient.SqlCommand(strSQL, conn)
Dim rdr As System.Data.SqlClient.SqlDataReader = Com.ExecuteReader()
Dim citiesWhere As String
citiesWhere = String.Empty
Dim Item As ListItem
For Each Item In LBSendTo.Items
If (Item.Selected) Then
citiesWhere += "" & Item.Text + ";"
End If
Next
objMailMessage = New MailMessage
objMailMessage.To = "" & citiesWhere & ""
strHtmlBody = "<TABLE>"
strHtmlBody = strHtmlBody & "</TABLE>"
objMailMessage.Body = strHtmlBody
objMailMessage.BodyFormat = MailFormat.Html
SmtpMail.Send(objMailMessage)
End Function
Code for the Dropdown list selected change:
Code:
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
conn = New SqlClient.SqlConnection(strconn)
conn.Open()
Dim strSQL = "SELECT d.group_descr, a.first_name, a.last_name, a.user_id, b.email, d.group_id, d.parent_group_id FROM icc_user_data AS a LEFT OUTER JOIN icc_users AS b ON a.user_id = b.user_id LEFT OUTER JOIN icc_users_groups AS c ON a.user_id = c.user_id LEFT OUTER JOIN icc_groups AS d ON c.group_id = d.group_id WHERE (b.user_id NOT IN (249)) AND (d.group_id IS NOT NULL) AND (d.group_id <> '8') AND (d.group_id <> '10') AND (d.group_id <> '46') AND (d.group_id <> '3') AND (d.group_id <> '47') AND (d.group_id <> '9') and d.group_id = '" & Me.DropDownList1.SelectedValue & "' ORDER BY d.group_descr"
Dim Com As New System.Data.SqlClient.SqlCommand(strSQL, conn)
Dim rdr As System.Data.SqlClient.SqlDataReader = Com.ExecuteReader
rdr.Read()
Me.DropDownList1.SelectedValue = rdr.Item(5).ToString
fillMonthlyDownloadsListBox()
LoadMonthlyDownloadsGrid()
End Sub
When I click on the send button it put the first item list in the listbox in the to: email. When it should put what I select as the to: email.
For example, the listbox will have let's say 4 names and email addresses:
John(john's email address)
Marc(marc's email address)
Bill(bill's email address)
Steve(steve's email address)
I select Bill or Bill and Steve but instead of selecting Bill or Bill and Steve's email address it will select John's email address to send to when I didn't select John in the list box when i go through the for/next loop because it is the first item listed.
BTW I do have the list box set for multiple selection.
I know this is a lot to absorb but I felt I needed to include as much as I can so someone could understand what was going on.