[RESOLVED] Populate listview with a MySQL Database
Ok, so Ive been searching around but havent been able to find a relevant answer.
I have a local MySQL server which has a database. Now, I want to populate a listview with all entries (Rows) in that database. How would I go about?
EDIT:
Ive manage to write entries to the database, and im using the following code. I just need to know how to populate a listview..
Code:
con = New MySqlConnection()
Dim command As New MySqlCommand
Dim conString As String
Dim SQL As String
Dim to_name As String
Dim from_name As String
Dim emne_name As String
Dim cat_name As String
Dim message As String
Dim operator_name As String
conString = "server=localhost;" _
& "user id=root;" _
& "password=123test;" _
& "database=radiologg"
from_name = ComboBox_fra.Text
to_name = ComboBox_Til.Text
emne_name = TextBox_emne.Text
cat_name = ComboBox_emne.Text
message = RTextBox_msg.Text
operator_name = label_operator_value.Text
SQL = "INSERT INTO samband_logg(time, fra, til, topic, cat, message, operator) VALUES(?time, ?from, ?to, ?topic, ?cat, ?message, ?operator)"
command.Parameters.AddWithValue("?time", from_name)
command.Parameters.AddWithValue("?from", from_name)
command.Parameters.AddWithValue("?to", to_name)
command.Parameters.AddWithValue("?topic", emne_name)
command.Parameters.AddWithValue("?cat", cat_name)
command.Parameters.AddWithValue("?message", message)
command.Parameters.AddWithValue("?operator", operator_name)
con.ConnectionString = conString
command.Connection = con
command.CommandText = SQL
Try
con.Open()
command.ExecuteNonQuery()
Catch ex As MySqlException
MessageBox.Show("Error connecting to database: " & ex.Message)
Finally
If con.State <> ConnectionState.Closed Then
con.Close()
MessageBox.Show("Rapport lagt in")
Me.Close()
End If
End Try
Re: Populate listview with a MySQL Database
Just use a reader...
If your listview name it's myList:
VB.NET Code:
Dim myReader As MySqlDataReader
myReader = command.ExecuteReader
While myReader.Read
myList.Items.Add(myReader(0) & myReader(1) & ... Rest of values)
End While
Re: Populate listview with a MySQL Database
Thanks, that did work.. but the layout is a bit messed up..?
Instead of displaying one row per row its showing one row per cell (cramping lots of values together in each cell..
How to fix?
Thanks!
Re: Populate listview with a MySQL Database
That's because of the:
myList.Items.Add(myReader(0) & myReader(1) & ... Rest of values)...
Just add the first as item and the others as subitens
Re: Populate listview with a MySQL Database
Quote:
Originally Posted by
mickey_pt
That's because of the:
myList.Items.Add(myReader(0) & myReader(1) & ... Rest of values)...
Just add the first as item and the others as subitens
Thank you very much!