Results 1 to 13 of 13

Thread: Listbox on windows form

  1. #1

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Listbox on windows form

    I am being driven insane by this stupid little problem.
    I use the following code to bind a list of tables from my database to a listbox control on a windows form :
    Code:
     
    Dim strConnectionString
            Dim dtTables As New DataTable
            strConnectionString = "data source=server;database=dbname;user id=doggy;password=poopoo"
            'Create connection object
            Dim Connect As New SqlConnection(strConnectionString)
            Dim strSQL As String
            strSQL = "Select Name from dbo.sysobjects "
            strSQL += "where OBJECTPROPERTY(id, N'IsView') = 1 "
            strSQL += "OR OBJECTPROPERTY(id, N'IsUSERTABLE') = 1 "
            Dim daSQL = New SqlDataAdapter(strSQL, Connect)
            daSQL.fill(dtTables)
            lstTables.DataSource = dtTables
            lstTables.DisplayMember = "Name"
    I then use the following code (In the selected index changed event from the listbox) to display the chosen table name in a label control :
    Code:
    lblTable.Text = lstTables.SelectedItem
    I keep getting the error :
    An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll

    AAAAArrrrgggghhhh....whats wrong here. ASP.net is so easy.
    Last edited by venerable bede; Dec 23rd, 2004 at 09:30 AM.

    Parksie

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Listbox on windows form

    is it a listbox or listview? cause you called it both

    also put on option strict and your error may become more apparent

  3. #3

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Re: Listbox on windows form

    Its a listbox.

    Where do I call it a listview ?

    Parksie

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Listbox on windows form

    Quote Originally Posted by venerable bede
    Its a listbox.

    Where do I call it a listview ?
    the first line of ur post

    I use the following code to bind a list of tables from my database to a listview control on a windows form

  5. #5
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Listbox on windows form

    anyway, I am not a big fan of data binding, but try something like this, since listboxes hold objects and not strings like VB6

    lblTable.Text = lstTables.SelectedItem.ToString

  6. #6

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Re: Listbox on windows form

    Apologies

    Parksie

  7. #7
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Listbox on windows form

    hahaha

    well did it work?

  8. #8

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Re: Listbox on windows form

    Nope



    GGGGrrrrrrrrrrrrrrrrrrrr

    Parksie

  9. #9
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Listbox on windows form

    ok so the listbox populates correctly though??? its just the line of code with the label you are having trouble with?

  10. #10

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Re: Listbox on windows form

    It populates the Listbox with all my tables but when I click on an entry I get :

    Code:
    An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll
    
    Additional information: Cast from type 'DataRowView' to type 'String' is not valid.
    This is childsplay. I am ashamed of myself.

    Parksie

  11. #11
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Listbox on windows form

    ok here it is
    Code:
    lblTable.Text = lstTables.GetItemText(lstTables.SelectedItem)

  12. #12

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Resolved Re: Listbox on windows form

    I love U Kleinma.

    May Santa shower you with gifts.
    A new high spec PC, IPAQ, He man action figures and Skull mountain and a Jag....with some naked nuns and a midget thrown in.

    You can have Bodwad for a few hours gratification as well.


    I still feel a right pensi though.

    Parksie

  13. #13
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Listbox on windows form

    I want castle grayskull please, mine was demolished as a child in a freak accident when somehow it fell (was thrown) off my roof

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width