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
Solved

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

Posted on 2006-11-03
6
290 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
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

829 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