[RESOLVED] [2008] ComboBox Load/Display Problem
I have a combobox that I'm trying to load from an Access DB table. My connection is good and I can pull data and display it in some textboxes. I wich though to load this data into a combobox so that the use can select the required record. While the program runs, no data is displayed in the combobox. The combobox is visable, but no data is visable in the dropdown.
Would someone please show me what I'm forgetting?
Code:
Imports System.Data.OleDb
Public Class NewGeneralIncidentForm
Dim dsIAData As New DataSet()
Dim intCurrentIdx As Integer = 0
Dim da1 As New OleDbDataAdapter()
Dim conn As New OleDbConnection
Private Sub NewGeneralIncidentForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'Data_Spill_InventoryDataSet.IA_Security_Team_Query' table. You can move, or remove it, as needed.
'Me.IA_Security_Team_QueryTableAdapter.Fill(Me.Data_Spill_InventoryDataSet.IA_Security_Team_Query)
' IA Staff Investigator Query needed.
' Location Information => Incident Location Query Form 8
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\1078282\My Documents\Visual Studio 2008\Projects\Event Reporter\Event Reporter\Data_Spill_Inventory.mdb;User Id=admin;Password=;"
da1.SelectCommand = New OleDbCommand("SELECT [SAS Employees 080929].LastName, [SAS Employees 080929].FirstName, [SAS Employees 080929].MiddleName, " _
+ "[SAS Employees 080929].EmployeeID FROM [SAS Employees 080929] WHERE ((([SAS Employees 080929].DoDTeam) Is Not Null)) " _
+ "ORDER BY [SAS Employees 080929].LastName, [SAS Employees 080929].FirstName")
da1.SelectCommand.Connection = conn
da1.Fill(dsIAData)
If dsIAData.Tables(0).Rows.Count > 0 Then
Label28.Text = Convert.ToString(dsIAData.Tables(0).Rows.Count)
txtRoom.Text = dsIAData.Tables(0).Rows(intCurrentIdx).Item("LastName").ToString()
txtAddress.Text = dsIAData.Tables(0).Rows(intCurrentIdx).Item("FirstName").ToString()
txtCity.Text = dsIAData.Tables(0).Rows(intCurrentIdx).Item("MiddleName").ToString()
txtState.Text = dsIAData.Tables(0).Rows(intCurrentIdx).Item("EmployeeID").ToString()
cboInvestigator.BeginUpdate()
'For Each dRow As DataRow In dsIAData.Tables("SAS Employees 080929").Rows
For Each dRow As DataRow In dsIAData.Tables(0).Rows
cboInvestigator.Items.Add(dRow("LastName").ToString() + " ," + dRow("FirstName").ToString())
Next
cboInvestigator.EndUpdate()
Else
MessageBox.Show("Fill Error")
End If
End Sub
Re: [2008] ComboBox Load/Display Problem
Try
vb.net Code:
Imports System.Data.OleDb
Public Class NewGeneralIncidentForm
Private dsIAData As New DataSet()
Private intCurrentIdx As Integer = 0
Private da1 As New OleDbDataAdapter()
Private conn As New OleDbConnection
Private sSQL As String = String.Empty
Private Sub NewGeneralIncidentForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'Data_Spill_InventoryDataSet.IA_Security_Team_Query' table. You can move, or remove it, as needed.
'Me.IA_Security_Team_QueryTableAdapter.Fill(Me.Data_Spill_InventoryDataSet.IA_Security_Team_Query)
' IA Staff Investigator Query needed.
' Location Information => Incident Location Query Form 8
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\1078282\My Documents\Visual Studio 2008\Projects\Event Reporter\Event Reporter\Data_Spill_Inventory.mdb;User Id=admin;Password=;"
sSQL = "SELECT [SAS Employees 080929].LastName, [SAS Employees 080929].FirstName, "
sSQL = sSQL & "[SAS Employees 080929].MiddleName, [SAS Employees 080929].EmployeeID "
sSQL = sSQL & "FROM [SAS Employees 080929] "
sSQL = sSQL & "WHERE ((([SAS Employees 080929].DoDTeam) Is Not Null)) "
sSQL = sSQL & "ORDER BY [SAS Employees 080929].LastName, [SAS Employees 080929].FirstName")
Dim command As New OleDbCommand(sSQL, conn)
Dim reader As OleDbDataReader = command.ExecuteReader()
cboInvestigator.Items.Clear()
If reader.HasRows = True Then 'make sure there is data returned
While reader.Read()
cboInvestigator.Items.Add(reader.GetString(0) & " ," & reader.GetString(1))
End While
Else
MessageBox.Show("No data matched your search criteria.")
End If
reader.Close()
End Sub
Re: [2008] ComboBox Load/Display Problem
In the interest of space I didn't show all of the code in the load event. If I had you would have noticed that I had an additional combobox.Clear() function. So the ComboBox was being loaded and then cleared. Thanks for getting me to look at my code with a close eye. :)