Populating a comboBox with an ArrayList
-
Hi, I am trying to populate a ComboBox with the contents obtained within an ArrayList, but instead of putting the names held within the ArrayList, it writes "System.Collections.ArrayList" in the place of the desired names. Could you take a look at my code and see where I am going wrong. I am using vb.net 2002 version. Any help is greatly appreciated. Many thanks John This is the code for the function selectFieldNames :-
Public fieldNames As New ArrayList() Public Function selectFieldNames(ByVal strSQL As String) As ArrayList ' select all field names from the selected table sqlite_cmd.CommandText = (strSQL) ' Now the SQLiteCommand object can give us a DataReader-Object: sqlite_datareader = sqlite_cmd.ExecuteReader() While sqlite_datareader.Read() Try fieldNames.Add(sqlite_datareader("name")) MessageBox.Show(sqlite_datareader("name")) Catch es As Exception MessageBox.Show(es.Message) End Try End While Return fieldNames End Function
This is the code for populating the ComboBox :-Private Sub frmCreateIndex_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load dbConn.openExistingDatabse("Data Source=" & getDBName() & ";Version=3;New=False;Compress=True;") dbConn.createSQLCommand() Dim a As Integer Try dbConn.selectFieldNames("pragma table_info(test)") For a = 0 To (dbConn.fieldNames.Count) - 1 cmbFieldIndex.Items.Add(dbConn.fieldNames) Next a Catch es As Exception MessageBox.Show(es.Message) End Try End Sub
-
Hi, I am trying to populate a ComboBox with the contents obtained within an ArrayList, but instead of putting the names held within the ArrayList, it writes "System.Collections.ArrayList" in the place of the desired names. Could you take a look at my code and see where I am going wrong. I am using vb.net 2002 version. Any help is greatly appreciated. Many thanks John This is the code for the function selectFieldNames :-
Public fieldNames As New ArrayList() Public Function selectFieldNames(ByVal strSQL As String) As ArrayList ' select all field names from the selected table sqlite_cmd.CommandText = (strSQL) ' Now the SQLiteCommand object can give us a DataReader-Object: sqlite_datareader = sqlite_cmd.ExecuteReader() While sqlite_datareader.Read() Try fieldNames.Add(sqlite_datareader("name")) MessageBox.Show(sqlite_datareader("name")) Catch es As Exception MessageBox.Show(es.Message) End Try End While Return fieldNames End Function
This is the code for populating the ComboBox :-Private Sub frmCreateIndex_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load dbConn.openExistingDatabse("Data Source=" & getDBName() & ";Version=3;New=False;Compress=True;") dbConn.createSQLCommand() Dim a As Integer Try dbConn.selectFieldNames("pragma table_info(test)") For a = 0 To (dbConn.fieldNames.Count) - 1 cmbFieldIndex.Items.Add(dbConn.fieldNames) Next a Catch es As Exception MessageBox.Show(es.Message) End Try End Sub
You setup an indexer to go through all the elements of
fieldNames
but you never used it to return an actual element in the array!Try
dbConn.selectFieldNames("pragma table_info(test)")
For a = 0 To (dbConn.fieldNames.Count) - 1
cmbFieldIndex.Items.Add(dbConn.fieldNames)
Next a
Catch es As Exception
MessageBox.Show(es.Message)
End TryShould be:
Try
dbConn.selectFieldNames("pragma table_info(test)")
For a = 0 To (dbConn.fieldNames.Count) - 1
cmbFieldIndex.Items.Add(dbConn.fieldNames(a))
Next a
Catch es As Exception
MessageBox.Show(es.Message)
End TryDave Kreskowiak Microsoft MVP - Visual Basic
-
You setup an indexer to go through all the elements of
fieldNames
but you never used it to return an actual element in the array!Try
dbConn.selectFieldNames("pragma table_info(test)")
For a = 0 To (dbConn.fieldNames.Count) - 1
cmbFieldIndex.Items.Add(dbConn.fieldNames)
Next a
Catch es As Exception
MessageBox.Show(es.Message)
End TryShould be:
Try
dbConn.selectFieldNames("pragma table_info(test)")
For a = 0 To (dbConn.fieldNames.Count) - 1
cmbFieldIndex.Items.Add(dbConn.fieldNames(a))
Next a
Catch es As Exception
MessageBox.Show(es.Message)
End TryDave Kreskowiak Microsoft MVP - Visual Basic