Avatar of sanlorenzo
sanlorenzo
 asked on

Visual Studio 2008 Multiselection querry with LINQ Query

With Visual Studio 2008 and SQL Server 2005 I have a Form where i drag the  data Class (tbl_Name )  Linked to a Table  Named  tbl_Name

The Table has 5 Column : NameID/Name/Town/Country/Continent

So the Form show a Grid representing the tbl_Name with the 5 column
I have in same form Toolstrip    2 combo box named as follow :

ToolStripComboBox1    It Populate Taking Data from the column Continent
ToolStripComboBox2   It Populate Taking  Data from the collumn Country

 In the Form I have also a Button : Button3 that i use to trigger the filter action

I am using the following code to filter the Grid by Continent and by Country and it works very well

 Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click


        Dim MultyQuerry = From tbl_Name In db.tbl_Names _
                                Where tbl_Name.Continent = Me.ToolStripComboBox1.SelectedItem.ToString _
                                And tbl_Name.Country = Me.ToolStripComboBox2.SelectedItem.ToString _
                                Select tbl_Name
        Me.Tbl_NameBindingSource.DataSource = MultyQuerry
End Sub

Now let me come to my Problem

All is working if i have both Combo Box with a value :

But it give me error if one of the comboBox is empty

Is anyone helping me to change my code so to handle the Null value ???

Thanks
Programming TheoryMicrosoft ApplicationsVisual Basic.NET

Avatar of undefined
Last Comment
sanlorenzo

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Nasir Razzaq

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
sanlorenzo

ASKER
here is teh code i develop , do you see any Problem , i try in any possible combination and it works
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

                If Me.ToolStripComboBox1.SelectedItem > "" And Me.ToolStripComboBox2.SelectedItem > "" Then

            Dim MultyQuerry = From tbl_Name In db.tbl_Names _
                                          Where tbl_Name.Continent = Me.ToolStripComboBox1.SelectedItem.ToString _
                                          And tbl_Name.Country = Me.ToolStripComboBox2.SelectedItem.ToString _
                                          Select tbl_Name
            Me.Tbl_NameBindingSource.DataSource = MultyQuerry

        End If
           If Me.ToolStripComboBox1.SelectedItem > "" And Me.ToolStripComboBox2.SelectedItem = "" Then

            Dim MultyQuerry1 = From tbl_Name In db.tbl_Names _
                                          Where tbl_Name.Continent = Me.ToolStripComboBox1.SelectedItem.ToString _
                                          Select tbl_Name

            Me.Tbl_NameBindingSource.DataSource = MultyQuerry1
        End If
              If Me.ToolStripComboBox1.SelectedItem = "" And Me.ToolStripComboBox2.SelectedItem > "" Then

            Dim MultyQuerry2 = From tbl_Name In db.tbl_Names _
                                          Where tbl_Name.Country = Me.ToolStripComboBox2.SelectedItem.ToString _
                                          Select tbl_Name
            Me.Tbl_NameBindingSource.DataSource = MultyQuerry2
        End If
               If Me.ToolStripComboBox1.SelectedItem = "" And Me.ToolStripComboBox2.SelectedItem = "" Then

            Dim MultyQuerry3 = From tbl_Name In db.tbl_Names

            Select Case db.tbl_Names

            End Select

            Me.Tbl_NameBindingSource.DataSource = MultyQuerry3

        End If


    End Sub
sanlorenzo

ASKER
Is there any one to help , if for instance the Combo Box instead being 2 become 4  , i see i should have a better code to apply ???

Thanks
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes