Solved

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

Posted on 2006-11-03
6
291 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Prevent call a sub/function several times when data bind to gridview 21 37
Sql server insert 13 46
Error message when using PrintSocket.Send 7 26
vb.net sendkey 4 18
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

740 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