?
Solved

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

Posted on 2006-11-03
6
Medium Priority
?
294 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
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!

 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 2000 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

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!

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

762 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