Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ComboBox.Items.Count returns 1 when it should return 0

Status
Not open for further replies.

juice4145

Programmer
Oct 8, 2003
6
0
0
US
Hi All,
Here's my question.

I have two comboboxes on a form. The first (cboJob) is populated from a dataset on the form load. The second box (cboJobInfo) is populated based on what is selected in the first in the OnSelectedIndex Change Event. My issue is that not all of the items in cboJob have data to populate cboJobInfo with. But when an item from cboJob is selected that has no cboJobInfo items associated with it and I do cboJobInfo.Items.Count it returns a value of 1. I added a MessageBox.Show(cboJobInfo.Items.Count) before and after I set cboJobInfo.DataSource to the the DataSet. Before returns 0 and after returns 1. Also, when the Messagebox.Show executes after the datasource is loaded (where it returns 1) System.Data.DataRow appears in cboJobInfo, then disappears after Ok is clicked on the messagebox. Can anyone tell me whats going on here? I would like to execute some additional code based on whether cboJobInfo is populated or not and had been using "If cboJobInfo.Items.Count > 0 then" but since the count is returning 1, it is failing. Here's my code:

'Filling DataSet
Function PopulateDataSet(pTable as string, SQLString as string)
Dim da As New SqlClient.SqlDataAdapter(SQLString, Conn)
Dim ds As New DataSet()

da.Fill(ds, pTable)

PopulateDataSet = ds
End Function

'Function to Create the ComboBoxes
Function CreateCBO(pCbo as combobox, pCboField As String, pTable As String, pDs as DataSet)
With pCbo
MessageBox.Show("PreDSLoadCBO " & pCbo.Items.Count.ToString)
.DataSource = pDs.Tables(pTable)
MessageBox.Show("DSLoadCBO " & pCbo.Items.Count.ToString)
.ValueMember = pCboField
MessageBox.Show("DispMemLoadCBO " & pCbo.Items.Count.ToString)
.DisplayMember = pCboField
MessageBox.Show("ValMemLoadCBO " & pCbo.Items.Count.ToString)
End With
End Function

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strSQL As String = "SELECT DISTINCT Job FROM JobDocumentation ORDER BY Job"
Dim ds As DataSet = PopulateDataSet("JobDocumentation", strSQL)
CreateCBO(cboJob, "Job", "JobDocumentation", ds)
End Sub

Private Sub cboJob_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboJob.SelectedIndexChanged
If cboJob.SelectedIndex <> -1 Then
Dim strSQL As String = "SELECT * FROM JobDocumentation WHERE Job = '" & Trim(cboJob.SelectedValue.ToString) & "'"
Dim ds As DataSet = PopulateDataSet("JobDocumentation", strSQL)
CreateCBO(cboJobInfo, "Job_Info", "JobDocumentation", ds)
End If

If cboJobInfo.Items.Count > 0 Then
'Code to be executed when nothing in cboJobInfo
End If
End Sub

Thanks in advance.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top