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 ???
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
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
If Me.ToolStripComboBox1.Sele
Dim MultyQuerry = From tbl_Name In db.tbl_Names _
Where tbl_Name.Continent = Me.ToolStripComboBox1.Sele
And tbl_Name.Country = Me.ToolStripComboBox2.Sele
Select tbl_Name
Me.Tbl_NameBindingSource.D
End If
If Me.ToolStripComboBox1.Sele
Dim MultyQuerry1 = From tbl_Name In db.tbl_Names _
Where tbl_Name.Continent = Me.ToolStripComboBox1.Sele
Select tbl_Name
Me.Tbl_NameBindingSource.D
End If
If Me.ToolStripComboBox1.Sele
Dim MultyQuerry2 = From tbl_Name In db.tbl_Names _
Where tbl_Name.Country = Me.ToolStripComboBox2.Sele
Select tbl_Name
Me.Tbl_NameBindingSource.D
End If
If Me.ToolStripComboBox1.Sele
Dim MultyQuerry3 = From tbl_Name In db.tbl_Names
Select Case db.tbl_Names
End Select
Me.Tbl_NameBindingSource.D
End If
End Sub