mardi 24 janvier 2017

vb.Net if else statement not working properly in ms access search function

just want to ask for some help. i have this if else statement wherein i search in the datase using the conditions. the problem is the 3rd condition "cbxCategory isnot nothing Len(Trim(cbxSizeType.Text)) = 0 Then" is not working. i tried to remove the first two conditions to test if anything is wrong but the 3rd condition works if the 1st two condition is removed. here is the code, TIA!

Private Sub txtSearch_TextChanged(sender As Object, e As EventArgs) Handles txtSearch.TextChanged
    If Len(Trim(txtSearch.Text)) = 0 Then
        LoadInventory()
        cbxCategory.Text = Nothing
        cbxSizeType.Text = Nothing
        cbxSizeType.Items.Clear()
    End If
    If cbxCategory.Text = Nothing And cbxSizeType.Text = Nothing Then
        Try
            con = New OleDbConnection(cs)
            con.Open()
            cmd = New OleDbCommand("SELECT (ProductPicture) as [Image],(ProductCode) as [Product Code],(ProductName) as [Product Name],(ProductSizeType) as [Size/Type],(ProductCategory) as [Category] from [INVENTORY] where [ProductName] like '%" & Replace(txtSearch.Text, "'", "\'") & "%' ORDER BY [ProductCode] ASC", con)
            adp = New OleDbDataAdapter(cmd)
            ds = New DataSet()
            adp.Fill(ds, "INVENTORY")
            'FilteredDesc
            InventoryDGV.DataSource = ds.Tables("INVENTORY").DefaultView
            con.Close()
            InventoryDGV.ClearSelection()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Search", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    ElseIf cbxCategory.Text IsNot Nothing And cbxSizeType.Text IsNot Nothing Then
        Try
            con = New OleDbConnection(cs)
            con.Open()
            cmd = New OleDbCommand("SELECT (ProductPicture) as [Image],(ProductCode) as [Product Code],(ProductName) as [Product Name],(ProductSizeType) as [Size/Type],(ProductCategory) as [Category] from [INVENTORY] where [ProductName] like '%" & Replace(txtSearch.Text, "'", "\'") & "%' and [ProductCategory] = '" & cbxCategory.Text & "' and [ProductSizeType] = '" & cbxSizeType.Text & "' ORDER BY [ProductCode] ASC", con)
            adp = New OleDbDataAdapter(cmd)
            ds = New DataSet()
            adp.Fill(ds, "INVENTORY")
            'FilteredDesc
            InventoryDGV.DataSource = ds.Tables("INVENTORY").DefaultView
            con.Close()
            InventoryDGV.ClearSelection()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Search", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    ElseIf cbxCategory.Text IsNot Nothing And Len(Trim(cbxSizeType.Text)) = 0 Then
        Try
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Search", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
        con = New OleDbConnection(cs)
        con.Open()
        cmd = New OleDbCommand("SELECT (ProductPicture) as [Image],(ProductCode) as [Product Code],(ProductName) as [Product Name],(ProductSizeType) as [Size/Type],(ProductCategory) as [Category] from [INVENTORY] where [ProductName] like '%" & Replace(txtSearch.Text, "'", "\'") & "%' and [ProductCategory] = '" & cbxCategory.Text & "' ORDER BY [ProductCode] ASC", con)
        adp = New OleDbDataAdapter(cmd)
        ds = New DataSet()
        adp.Fill(ds, "FilteredDesc")
        'FilteredDesc
        InventoryDGV.DataSource = ds.Tables("INVENTORY").DefaultView
        con.Close()
        InventoryDGV.ClearSelection()
    End If
End Sub

Aucun commentaire:

Enregistrer un commentaire