Trying to copy an entire colum from arraylist to listbox

liquidG

Member
Joined
Nov 2, 2007
Messages
5
Programming Experience
Beginner
Hi All,

I have been racking my brain on this problem for two days now, and I cant seem to figure it out... Hopefully someone can help me.

I copied a database table (3 columns wide) from my db to an arraylist. what I want to do is copy all contents of column 2 (string) to a listbox in vb.net. I cant seem to do it.

Another possible fix would be to copy all three columns to a listbox, but only have columns 2 visible. Is this possible? Is there a better tool to use than listbox or arraylist?

On a scale from 1 to 10 in vb.net programming capability, i would consider myself a 3 or 4, so please take it easy on me.

if I can supply any other information, please let me know.

Thanks.
 
VB.NET:
Listbox1.Datasource = yourDataTable
Listbox1.DisplayMember  = "column2name"
 
Thanks for the post John. I think im getting closer. I have the following code to try and populate listbox1:
Dim aa As Integer
DocFamilyArrayList = mydb.GetDocumentFamilies(myini.DBase)
For aa = 1 To DocFamilyArrayList.Count
ListBox1.Items.Add(DocFamilyArrayList(aa - 1).ToString)
Next
ListBox1.DisplayMember = 1
------------------------------------------
what I have listed in the listbox is five instances of "mysql.data.mysqlclient.mysqldatareader"

any suggestions?
 
So you have MySqlDataReaders in your array, I think you have done something wrong before this. There is a 7 part VB.Net MySql tutorial you should read, it tutors most matters of working with MySql and the Connector/NET library. Part 6 is about the MySqlDataReader, but you should also learn about the MySqlDataAdapter and MySqlCommand in part 4+5 and you will see data from database is usually kept in DataTables and not arrays.

Thread moved to MySql forum.
 
thank you. Much appreciated. i will review the documentation this evening.

I have been messing around with the code, and I am getting much closer. Here is a detailed description of the problem...


Ill tell you the end result, first... My combo box is listing the same data over and over. the data is pulling from an arraylist. the arraylist is populated from a MySQL database table with three columns. I want the second column of the arraylist to populate the combo box.
example: the combo box has 4 selections (funny.. exactly how many rows are in the test db table...)
Township Docs
Township Docs
Township Docs
Township Docs

The list should be...
Township Docs
HR Docs
Police Docs
AP Docs


the function I wrote to query the database (currently it has three columns and 4 rows) is below:
----------------------------------------------------------------------
Public Function GetDocumentFamilies(ByVal dataBase As String) As ArrayList

Dim objConnection As New MySqlConnection(_ConnectionString)
Dim objCommand As New MySqlCommand
Dim returnArrayList As New ArrayList

objCommand.Connection = objConnection
objCommand.CommandText = "select doc_family_id, doc_family_name from " & dataBase & ".doc_family_table"

objConnection.Open()

Dim myreader As MySqlDataReader = objCommand.ExecuteReader()


While myreader.Read
returnArrayList.Add(myreader)
End While

Return returnArrayList

myreader.Close()
objConnection.Close()


End Function
---------------------------------------------------------------------------------
theory 1 is that the datareader is just populating the same row of data 4 times? I cant be sure.

When the form with the combo box loads, the following code is executed:
---------------------------------------------------------------------------------
Private Sub frmEditDocumentTypes_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim myini As New msiINI
Dim mydb As New msiDB(myini.DataSource, myini.UserId, myini.Pwd)
Dim myarray As New ArrayList
Dim mycount As Integer




myarray = mydb.GetDocumentFamilies(myini.DBase)

For mycount = 0 To myarray.Count - 1
Me.cboDocumentFamily.Items.Add(myarray(mycount)(1) )

Next mycount
--------------------------------------------------------------------------------------

I am going to look at the link you sent in the last message this evening. If you have any 'quick fix' for the above, pls let me know.

I do appreciate your time reviewing my previous posts.
thanks.
 
John,

The mysql-vb.net tutorial was a fantastic resource. I think I have it figured out. for anyone else that has been having a similar issue, I will post my code later once I test it.
 
it works perfectly now! for anyone that is interested, below is the new code:

to access the db-
--------------------------------------------------------------------------
Public Function GetDocumentFamilies(ByVal dataBase As String) As DataTable

Dim objConnection As New MySqlConnection(_ConnectionString)
Dim objCommand As New MySqlCommand
Dim returnArrayList As New ArrayList
Dim objAdapter As New MySqlDataAdapter
Dim objData As New DataTable

objCommand.Connection = objConnection
objAdapter.SelectCommand = objCommand
objCommand.CommandText = "select doc_family_id, doc_family_name from " & dataBase & ".doc_family_table"

objConnection.Open()

objAdapter.Fill(objData)

Return objData
--------------------------------------------------------------------------

to populate the combo box:
--------------------------------------------------------------------------
Private Sub frmEditDocumentTypes_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim myini As New msiINI
Dim mydb As New msiDB(myini.DataSource, myini.UserId, myini.Pwd)
Dim myDataTable As New DataTable

myDataTable = mydb.GetDocumentFamilies(myini.DBase)

Me.cboDocumentFamily.DataSource = myDataTable
Me.cboDocumentFamily.DisplayMember = "doc_family_name"
Me.cboDocumentFamily.ValueMember = "doc_family_id"
--------------------------------------------------------------------------


thanks for your help, John! Much appreciated.
 

Latest posts

Back
Top