|
-
Dec 23rd, 2004, 09:08 AM
#1
Thread Starter
Fanatic Member
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.
-
Dec 23rd, 2004, 09:12 AM
#2
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
-
Dec 23rd, 2004, 09:14 AM
#3
Thread Starter
Fanatic Member
Re: Listbox on windows form
Its a listbox.
Where do I call it a listview ?
-
Dec 23rd, 2004, 09:20 AM
#4
Re: Listbox on windows form
 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
-
Dec 23rd, 2004, 09:22 AM
#5
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
-
Dec 23rd, 2004, 09:25 AM
#6
Thread Starter
Fanatic Member
Re: Listbox on windows form
Apologies
-
Dec 23rd, 2004, 09:32 AM
#7
Re: Listbox on windows form
-
Dec 23rd, 2004, 09:35 AM
#8
Thread Starter
Fanatic Member
Re: Listbox on windows form
Nope
GGGGrrrrrrrrrrrrrrrrrrrr
-
Dec 23rd, 2004, 09:39 AM
#9
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?
-
Dec 23rd, 2004, 09:42 AM
#10
Thread Starter
Fanatic Member
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.
-
Dec 23rd, 2004, 09:44 AM
#11
Re: Listbox on windows form
ok here it is
Code:
lblTable.Text = lstTables.GetItemText(lstTables.SelectedItem)
-
Dec 23rd, 2004, 09:48 AM
#12
Thread Starter
Fanatic Member
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.
-
Dec 23rd, 2004, 09:58 AM
#13
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|