views:

216

answers:

1

Right now I am building a filter based on user input (textboxes and comboboxes) by a bunch of if statements. There must be a better way. Here is my current code:

    Private Function BuildProductsFilter() As String

        Dim RawFilterResults As String = ""

        If Not CompanyNameComboBox.SelectedValue Is Nothing AndAlso Not CompanyNameComboBox.SelectedValue.ToString = "[ALL]" Then
            RawFilterResults = "companyname = '" & CompanyNameComboBox.SelectedValue.ToString & "'"
        End If

        If QtyTextbox.Text > "" AndAlso IsNumeric(QtyTextbox.Text) Then
            RawFilterResults &= " and stock = " & QtyTextbox.Text
        End If

        If KeywordTextbox.Text > "" Then
            RawFilterResults &= " and (description like '%" & KeywordTextbox.Text & "%'"
            RawFilterResults &= "or descriptionlong like '%" & KeywordTextbox.Text & "%'"
            RawFilterResults &= "or details like '%" & KeywordTextbox.Text & "%')"
        End If

        If SKUTextbox.Text > "" Then

            If SKUTextbox.Text.StartsWith("*") Then
                RawFilterResults &= " and sku like '%" & SKUTextbox.Text & "'"
            ElseIf SKUTextbox.Text.EndsWith("*") Then
                RawFilterResults &= " and sku like '" & SKUTextbox.Text & "%'"
            Else
                RawFilterResults &= " and sku = '" & SKUTextbox.Text & "'"
            End If

        End If

        If Not AllowPurchaseCombobox.SelectedItem Is Nothing AndAlso Not AllowPurchaseCombobox.SelectedItem.ToString = "[ALL]" Then
            RawFilterResults &= " and allowpurchase = '" & AllowPurchaseCombobox.SelectedValue.ToString & "'"
        End If

        If Not ShowPriceCombobox.SelectedItem Is Nothing AndAlso Not ShowPriceCombobox.SelectedItem.ToString = "[ALL]" Then
            RawFilterResults &= " and showprice = '" & ShowPriceCombobox.SelectedValue.ToString & "'"
        End If

        If Not VirtualLoupeCombobox.SelectedItem Is Nothing AndAlso Not VirtualLoupeCombobox.SelectedItem.ToString = "[ALL]" Then
            RawFilterResults &= " and VirtualLoupe = '" & VirtualLoupeCombobox.SelectedValue.ToString & "'"
        End If

        If ImageTextbox.Text > "" Then
            Dim ImageDir As String = Path.GetDirectoryName(ImageTextbox.Text)
            RawFilterResults &= " and imageurl like '" & ImageDir & "%'"
        End If

        If CaratTextbox.Text > "" Then
            RawFilterResults &= " and carat = '" & CaratTextbox.Text & "'"
        End If

        If CutTextbox.Text > "" Then
            RawFilterResults &= " and cut = '" & CutTextbox.Text & "'"
        End If

        If ColorTextbox.Text > "" Then
            RawFilterResults &= " and color = '" & ColorTextbox.Text & "'"
        End If

        If ClarityTextbox.Text > "" Then
            RawFilterResults &= " and Clarity = '" & ClarityTextbox.Text & "'"
        End If

        If RawFilterResults.StartsWith(" and ") Then
            RawFilterResults = RawFilterResults.Substring(4)
        End If

        Return RawFilterResults

    End Function
+1  A: 

Use a fluent style interface. Simple sample here

Or better yet use and ORM so you don't have string encoded field names etc

TFD
Thanks for the help. This is definitly easer t use and read.
Wade73