I don't like to admit it but I am building a vb.net winform that requires the use of reading text fields, check boxes or radio buttons to build a sql query and return results. the sql query part works fantastically however the select case statements don't seem to be evaluating the checkbox or radio checkstate correctly.
Select Case True
Case txtFirstName.Text.Length > 0 & chkActive.CheckState
dgvEmployees.DataSource = employess.FindEmployee(txtFirstName.Text)
dgvEmployees.Visible = True
Case txtFirstName.Text.Length > 0 & chkActive.CheckState
dgvEmployees.DataSource = employess.FindEmployee(txtFirstName.Text, , , "Active")
chkInactive.Enabled = False
dgvEmployees.Visible = True
Case txtFirstName.Text.Length > 0 & chkInactive.CheckState
dgvEmployees.DataSource = employess.FindEmployee(txtFirstName.Text, , , "Inactive")
chkActive.Enabled = False
dgvEmployees.Visible = True
Case txtLastName.Text.Length > 0 & chkActive.CheckState
dgvEmployees.DataSource = employess.FindEmployee(, txtLastName.Text)
dgvEmployees.Visible = True
Case txtLastName.Text.Length > 0 & chkActive.CheckState
dgvEmployees.DataSource = employess.FindEmployee(, txtLastName.Text, , "Active")
chkInactive.Enabled = False
dgvEmployees.Visible = True
Case txtLastName.Text.Length > 0 & chkInactive.CheckState
dgvEmployees.DataSource = employess.FindEmployee(, txtLastName.Text, , "Inactive")
chkActive.Enabled = False
dgvEmployees.Visible = True
Case txtEmpCode.Text.Length > 0 & rdbActive.Checked
dgvEmployees.DataSource = employess.FindEmployee(, , txtEmpCode.Text)
dgvEmployees.Visible = True
Case txtEmpCode.Text.Length > 0 & chkActive.Checked
dgvEmployees.DataSource = employess.FindEmployee(, , txtEmpCode.Text, "Active")
chkInactive.Enabled = False
dgvEmployees.Visible = True
Case txtEmpCode.Text.Length > 0 & chkInactive.CheckState = CheckState.Checked
dgvEmployees.DataSource = employess.FindEmployee(, , txtEmpCode.Text, "Inactive")
chkActive.Enabled = False
dgvEmployees.Visible = True
Case Else
MessageBox.Show("please enter an employee code, firstname or lastname")
End Select
Every time I debug i get an invalid cast exception for the radio button or check box, am I missing something?
In Visual Basic, &
performs a string concatenation. You want to use And
or AndAlso
, as was mentioned by Plutonix.
AndAlso
would be the better choice, as it skips evaluating the second condition if the first fails. For example, if txtFirstName.Text
has a length of 0, the program would not bother to check the state of chkActive.CheckState
. Actually, because of this, it might be theoretically a tiny bit more efficient to do check chkActive.CheckState
first, because that is probably a little faster to evaluate.