Solved

"specified cast not valid" error happens while cycling through ListItems In .SelectedItems

Posted on 2006-11-03
6
292 Views
Last Modified: 2010-04-23
   Private Function CountyCodeSQL() As String
        Dim lvItem As ListViewItem
        Dim str As String
        With Me.lstAidCode
            If .SelectedItems.Count > 0 Then
                For Each lvItem In .SelectedItems  ERROR on this line*****
                    str = str & ", '" & lvItem.Text & "'"
                Next
            End If
        End With
        CountyCodeSQL = str
    End Function

*******SPECIFIED CAST IS NOT VALID

I'm new to .net and this doesn't make any sense to me.  Thanks for any help.
0
Comment
Question by:FatalErr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 10

Expert Comment

by:Kinger247
ID: 17870938
Whats in the listview item ?
0
 
LVL 1

Author Comment

by:FatalErr
ID: 17870999
list box properties-
MultiColumn = false
SelectionMode = MultipleSimple

It's populated from a char(2) field from SQL Server
0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 17871019
Hi FatalErr,

Is Me.lstAidCode a ListBox by any chance? If so, try this....

   Private Function CountyCodeSQL() As String

        Dim str As String
        With Me.lstAidCode
            If .SelectedItems.Count > 0 Then
                For i As Integer In .SelectedIndices
                    str = str & ", '" & .Items(i) & "'"
                Next
            End If
        End With
        CountyCodeSQL = str

    End Function

Regards,

Wayne
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 500 total points
ID: 17871024
Sorry, the function should be this....

   Private Function CountyCodeSQL() As String

        Dim str As String
        With Me.lstAidCode
            If .SelectedItems.Count > 0 Then
                For Each i As Integer In .SelectedIndices
                    str = str & ", '" & .Items(i) & "'"
                Next
            End If
        End With
        CountyCodeSQL = str

    End Function

ListViewItems are objects in a ListView, not a ListBox, hence the error you received.

Wayne
0
 
LVL 13

Expert Comment

by:iHadi
ID: 17871032
The code you posted is ok and should work (it worked for me)
0
 
LVL 1

Author Comment

by:FatalErr
ID: 17871105
Thanks for the help.  It's really not alot of fun switching to .NET

Stacey
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question