Visual Basic Development Bookmark and Share   
 Home > Visual Basic Language > Populate a ListBox with data filtered by a ComboBox?
 

Populate a ListBox with data filtered by a ComboBox?

Hello, I am trying to populate aListBox witha field(RName) from a LINQ to SQL database (DataClasses1.dbml),table(RTables) filteredby field RType. The RType is being selected by the user from ComboBox1 which is databound to that field. ComboBox1 displaysthe dataproperly but the ListBox displays nothing. This is the code but I am not sure where to put it or why it doesn't work. I've tried to put it in the ComboBox1_SelectedIndexChanged, the ListBox event,and the Form Load event. Thanks for looking. Any help is appreciated.

DimdbAsNewDataClasses1DataContext
DimRByType=FromrpIndb.RTablesWhererp.RType=(ComboBox1.Text)Selectrp.RName
ForEachrpInRByType
ListBox1.Items.Add(RByType)
Next
audm  Friday, August 15, 2008 2:39 AM
Hi Audm,

Modify the code snippet similar the following code snippet. Hope this helps.

PublicClassForm1
PrivateSubForm1_Load(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesMyBase.Load
DimctAsNewDataClasses1DataContext("DataSource=.\Riquel;InitialCatalog=Northwind;IntegratedSecurity=True")
DataGridView1.DataSource=FromcustomerInct.ShippersSelectcustomer
DimRByType=FromrpInct.ShippersWhererp.CompanyName="Linqtest"Selectrp.CompanyName
ForEachrpInRByType.ToArray
ListBox1.Items.Add(rp)
Next
EndSub
EndClass



Best regards,
Riquel

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Riquel_Dong  Tuesday, August 19, 2008 11:56 AM
Hi Audm,

Add a breakpoint to check whether the RByType contains the data. You also can read this article about how to use Linq to SQL in this scenario.

Best regards,
Riquel

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Riquel_Dong  Monday, August 18, 2008 10:54 AM
Thanks, Riquel. I'll try that.
audm  Monday, August 18, 2008 12:37 PM
Riquel,

I tried what you said and there is data if I do a watch whilestepping through the codebut I don't see any with using a breakpoint. I am getting the exception:

A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Do you know what that is? I looked at the article you suggested and although the example is in C#, I could understand it enough to realize that they were doing the same thing I am. I couldn't see anywhere where I could've done it wrong.

audm  Monday, August 18, 2008 1:56 PM
Hi Audm,

Modify the code snippet similar the following code snippet. Hope this helps.

PublicClassForm1
PrivateSubForm1_Load(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesMyBase.Load
DimctAsNewDataClasses1DataContext("DataSource=.\Riquel;InitialCatalog=Northwind;IntegratedSecurity=True")
DataGridView1.DataSource=FromcustomerInct.ShippersSelectcustomer
DimRByType=FromrpInct.ShippersWhererp.CompanyName="Linqtest"Selectrp.CompanyName
ForEachrpInRByType.ToArray
ListBox1.Items.Add(rp)
Next
EndSub
EndClass



Best regards,
Riquel

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Riquel_Dong  Tuesday, August 19, 2008 11:56 AM

You can use google to search for other answers

Custom Search

More Threads

• Try-Catch and good programming practices...
• form.show focus
• File Reference Vs. Project Reference? What's the difference?
• Can someone please help me convert the following C# code to VB code
• SendMessage to refresh windows explorer
• please help with Moving folders
• Calling Arrays into Function/Subroutine calculations
• Shell "File Not Found" Error
• Bold the selected tab in a tab control
• crop image & save it to new file, howto?