I've got a series of Combo Box's on a form which I'm populating from a SQL Server database like this:
Private Sub LoadEmploymentStatus()
Dim cmd As SqlCommand = cnn.CreateCommand
cmd.CommandType = CommandType.StoredProcedur
e
cmd.CommandText = "procLoadEmploymentStatus"
Dim reader As SqlDataReader = cmd.ExecuteReader
Dim dt As New DataTable()
dt.Columns.Add("Employment
StatusIDAu
to", GetType(Integer))
dt.Columns.Add("Employment
Status", GetType(String))
Dim row As DataRow = dt.NewRow
row("EmploymentStatus") = "<SELECT ITEM>"
row("EmploymentStatusIDAut
o") = "0"
dt.Rows.Add(row)
While reader.Read
With reader
row = dt.NewRow
row("EmploymentStatus") = .GetValue(.GetOrdinal("Emp
loymentSta
tus"))
row("EmploymentStatusIDAut
o") = .GetValue(.GetOrdinal("Loo
kUp_Employ
mentStatus
IDAuto"))
dt.Rows.Add(row)
End With
End While
reader.Close()
ComboBox_EmploymentStatus.
DataSource
= dt
ComboBox_EmploymentStatus.
DisplayMem
ber = "EmploymentStatus"
ComboBox_EmploymentStatus.
ValueMembe
r = "EmploymentStatusIDAuto"
End Sub
The problem I have is that when I validate the ComboBox to ensure the user has selected a value, e.g:
If ComboBox_EmploymentStatus.
Visible = True Then
If CInt(ComboBox_EmploymentSt
atus.Selec
tedValue) = 0 Then
MessageBox.Show("Please select Employment Status!", "Empty Field!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation
)
ComboBox_EmploymentStatus.
Focus()
Exit Function
End If
End If
If one of the combo boxes is blank and I display the appropriate message box in the validation function it clears all the ComboBox's on the form.
Any ideas why? I don't want them to clear at all.
Start Free Trial