Link to home
Start Free TrialLog in
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
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sanlorenzo
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
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