LuckyLuke82 LuckyLuke82 - 3 months ago 31
Vb.net Question

Dynamic SQL - retrieve both checkbox values from DB

I'm retrieving data from DB when User selects checkboxes. Values in DB are "YES" or "NO". They both work fine separately, but If I click both checkboxes, I want to view all records that have both values. Here is my code:

Using cmd As New OracleCommand()

Dim SQL As String = "Select * FROM MyTable "
Dim Bind_SQL As String = " Where "

Dim Activity As String

If ChkActiveYES.Checked = True Then
Activity = "YES"
SQL = String.Concat(SQL, Bind_SQL, " ACTIVE_PLAYER = :activity")
cmd.Parameters.Add(New OracleParameter("activity", Activity))
Bind_SQL = " and "

ElseIf ChkActiveNO.Checked = True Then
Activity = "NO"
SQL = String.Concat(SQL, Bind_SQL, " ACTIVE_PLAYER = :activity")
cmd.Parameters.Add(New OracleParameter("activity", Activity))
Bind_SQL = " and "

ElseIf ChkActiveYES.Checked = True And ChkActiveNO.Checked = True Then
Activity = "NO"
SQL = String.Concat(SQL, Bind_SQL, " ACTIVE_PLAYER = :activity")
cmd.Parameters.Add(New OracleParameter("activity", Activity))
Bind_SQL = " and "

Activity = "YES"
SQL = String.Concat(SQL, Bind_SQL, " ACTIVE_PLAYER = :activity")
cmd.Parameters.Add(New OracleParameter("activity", Activity))
Bind_SQL = " and "

End If
End using


What am I missing here ?

Answer

It's not going to this part:

...
ElseIf ChkActiveYES.Checked = True And ChkActiveNO.Checked = True Then

because it already enters here:

If ChkActiveYES.Checked = True Then

Put the checking of if both are checked in the first part of your if statement.

Comments