-
Need with selecting items from a list box trigger by what selected in dropdown list
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. 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.From = "hsiinfo@us-hsi.com"
objMailMessage.To = "" & citiesWhere & ""
strHtmlBody = "<TABLE>"
strHtmlBody = strHtmlBody & "<TR><TD>Created By:</TD><TD>The report <strong>'" & Me.txtTitle.Text & "'</strong> has been uploaded for you and is ready for your review. <br>Please go to <a href='http://www.us-hsi.com'>www.us-hsi.com.</a><br>After logging in, click the Monthly Downloads link. You may then open or save your report.<br>If you have any questions, go to the Customer Service menu item and click New Request. <br> Type your inquiry in the space provided. A Customer Service Representative will respond shortly.<br><br><br>Health Systems International<br>5975 Castle Creek Parkway<br>Suite 100<br>Indianapolis, IN 46250<br><br>Phone: 1-800-962-6831<br>E-Mail: HSIInfo@us-icc.com</TD>"
strHtmlBody = strHtmlBody & "</TABLE>"
objMailMessage.Body = strHtmlBody
objMailMessage.BodyFormat = MailFormat.Html
SmtpMail.SmtpServer = "mail.us-hsi.com"
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@go.net)
Marc(marc@go.net)
Bill(bill@go.net)
Steve(steve@go.net)
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.
-
Welcome To DevX 
You have to loop through the entire listbox each time you run your code. Otherwise, your code won't know what you have selected. Here is an example
Code:
Dim i As Integer
For i = 0 To ListBox1.Items.Count - 1
If ListBox1.GetSelected(i) = True Then
'do your code here
End If
Next
I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
Please use [Code]your code goes in here[/Code] tags when posting code.
Before posting your question, did you look here?
Got a question on Linux? Visit our Linux sister site.
Modifications Required For VB6 Apps To Work On Vista
-
 Originally Posted by Hack
Welcome To DevX
You have to loop through the entire listbox each time you run your code. Otherwise, your code won't know what you have selected. Here is an example
Code:
Dim i As Integer
For i = 0 To ListBox1.Items.Count - 1
If ListBox1.GetSelected(i) = True Then
'do your code here
End If
Next
I tried this and I get an error 'Index was outside the bounds of the array"
Here is my code:
Dim citiesWhere As String
citiesWhere = String.Empty
Dim i As Integer
For i = 0 To LBSendTo.Items.Count - 1
If LBSendTo.GetSelectedIndices(i) = True Then
citiesWhere += "" & LBSendTo.GetSelectedIndices(i) + ";"
End If
Next
For the control on LBSendTo the only option was GetSelectedIndices and cities where is my variable so if they select more than one email address to send to: objMailMessage.To = "" & citiesWhere & ""
-
Are you sure you are using a ListBox?
There are only 4 available items beginning with G for a listbox:
GetChildAtPoint
GetContainerControl
GetItemText
GetSelected
I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
Please use [Code]your code goes in here[/Code] tags when posting code.
Before posting your question, did you look here?
Got a question on Linux? Visit our Linux sister site.
Modifications Required For VB6 Apps To Work On Vista
-
 Originally Posted by Hack
Are you sure you are using a ListBox?
There are only 4 available items beginning with G for a listbox:
GetChildAtPoint
GetContainerControl
GetItemText
GetSelected
Yes I am sure. Here is my source code for the listbox.
<asp:ListBox ID="LBSendTo" runat="server" Height="54px" SelectionMode="Multiple"
Width="430px" Rows="1"></asp:ListBox>
The only G options are GetSelectedIndices,GetType, GetHashCode. I am using VS200 2.0 SP1 if that helps.
-
Well, now I know why my example didn't work.
You are using ASP.NET which is a slightly different animal from VB.NET (which my code example was)....so you you won't get more examples you can't use, I've moved your thread to the ASP.NET section.
Last edited by Hack; 12-09-2008 at 01:57 PM.
I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
Please use [Code]your code goes in here[/Code] tags when posting code.
Before posting your question, did you look here?
Got a question on Linux? Visit our Linux sister site.
Modifications Required For VB6 Apps To Work On Vista
-
I just googled around a bit and I think I have an example of what you are asking.
See if this helps at all.
http://www.velocityreviews.com/forum...ted-value.html
I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
Please use [Code]your code goes in here[/Code] tags when posting code.
Before posting your question, did you look here?
Got a question on Linux? Visit our Linux sister site.
Modifications Required For VB6 Apps To Work On Vista
-
 Originally Posted by Hack
It helps but I don't know how I would apply to my code exactly. I have asked all around and haven't gotten a straight answer yet. How does the code I am using apply to the example you showed me.
-
Does anyone have any ideas on this?
Similar Threads
-
By admol in forum ASP.NET
Replies: 1
Last Post: 03-09-2007, 09:44 AM
-
By leafdodger in forum .NET
Replies: 3
Last Post: 10-27-2006, 02:57 PM
-
By Xtreme17 in forum VB Classic
Replies: 3
Last Post: 01-25-2006, 07:31 PM
-
By folbabe in forum VB Classic
Replies: 1
Last Post: 08-30-2002, 11:28 PM
-
By folbabe in forum VB Classic
Replies: 3
Last Post: 08-27-2002, 08:52 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks