Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > VB.NET 2002/2003 Basics
|
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 22nd, 2004, 07:24 PM
Registered User
 
Join Date: Jul 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Listbox being poplutaed with Access database.

Hi. This is using VB.NET and Access. I am populating a listbox via datasource from an Access Database on FormLoad. I have the listbox set for multi selection. My problem is when I attempt to retrieve the selected items I am getting errors. I have been trying to use the SelectedItems of the ListBox. If I take away the DataSource and load the contents manually, it works fine. I am thinking that using ADO you must retrieve the selected items a different way, but I cannot find anything on the net for a solution. Any help would be greatly appreciated.

Thanks

 
Old July 22nd, 2004, 08:37 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Can you post the code related to the problem? Once you have items in a listbox, it shouldn't matter how they got there.
 
Old July 22nd, 2004, 10:04 PM
Registered User
 
Join Date: Jul 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The below code will work fine if I just add my items in the items properity manually. Each selected item appears correctly in the MessageBox.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim i As Integer

        For i = 0 To ListBox2.SelectedItems.Count - 1
            MessageBox.Show(ListBox2.SelectedItems(i).ToString ())
        Next

    End Sub
End Class


When I change the ListBox to use the following properties I get 'System.Data.DataRowView' in the MessageBox instead of the actual item:

        DataSource: DataSet21
        DisplayMember: Accessories.Accessories
        ValueMember: Accessories.ID

The ListBox loads the data fine, I just can't seem to access the items appropriately.

Please let me know if you need further information.





 
Old July 23rd, 2004, 11:12 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
Default

See if this post helps you out:

http://p2p.wrox.com/topic.asp?TOPIC_ID=13452

It has several examples on accessing items in a combobox, but the code for a listbox is very similar.

J
 
Old July 23rd, 2004, 07:42 PM
Registered User
 
Join Date: Jul 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you katsarosj that link was very useful. I am now able to view all of the items in my listbox, however I only want to retrieve the selected items. Would you know how to modify the code below to that?

        For Each MyItem As DataRowView In ListBox2.Items
            MessageBox.Show(MyItem.Item("accessories").ToStrin g())
        Next


 
Old July 23rd, 2004, 08:20 PM
Registered User
 
Join Date: Jul 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok, I got it all you have to do is change ListBox2.Items to ListBox2.SelectedItems.

Thanks to all that have helped me get this solved.






Similar Threads
Thread Thread Starter Forum Replies Last Post
ListBox.AddItem In Access 2000 tready Access VBA 3 August 17th, 2006 01:11 PM
Listbox entry in Database acces 2003 BramuS Classic ASP Databases 15 June 14th, 2005 03:02 PM
Access issues with ASP and a MS Access Database rj_conceptsnrec.com Classic ASP Databases 2 May 19th, 2005 12:44 PM
access listbox in onchange event ozcyberus1 JSP Basics 0 September 6th, 2004 05:34 PM
how to set checked listbox in access application. Skyblue Access VBA 2 September 5th, 2003 09:52 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.