Solved

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

Posted on 2006-11-03
6
288 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now