Need with selecting items from a list box trigger by what selected in dropdown list


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: Need with selecting items from a list box trigger by what selected in dropdown list

  1. #1
    Join Date
    Dec 2008
    Posts
    9

    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.

  2. #2
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    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

  3. #3
    Join Date
    Dec 2008
    Posts
    9
    Quote Originally Posted by Hack View Post
    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 & ""

  4. #4
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    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

  5. #5
    Join Date
    Dec 2008
    Posts
    9
    Quote Originally Posted by Hack View Post
    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.

  6. #6
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    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

  7. #7
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    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

  8. #8
    Join Date
    Dec 2008
    Posts
    9
    Quote Originally Posted by Hack View Post
    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

    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.

  9. #9
    Join Date
    Dec 2008
    Posts
    9
    Does anyone have any ideas on this?

Similar Threads

  1. SelectedItem in a List box
    By admol in forum ASP.NET
    Replies: 1
    Last Post: 03-09-2007, 09:44 AM
  2. List box selected value question
    By leafdodger in forum .NET
    Replies: 3
    Last Post: 10-27-2006, 02:57 PM
  3. Replies: 3
    Last Post: 01-25-2006, 07:31 PM
  4. Re: Selecting a text from a list box.(Q*bert)
    By folbabe in forum VB Classic
    Replies: 1
    Last Post: 08-30-2002, 11:28 PM
  5. Selecting a text from a list box.
    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center