|
-
Aug 24th, 2009, 04:17 PM
#1
Thread Starter
Lively Member
[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
Last edited by Untouchab1e; Aug 24th, 2009 at 05:26 PM.
Reason: Solved
-
Aug 24th, 2009, 04:55 PM
#2
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
Last edited by mickey_pt; Aug 24th, 2009 at 04:57 PM.
Reason: :)
Rate People That Helped You
Mark Thread Resolved When Resolved
-
Aug 24th, 2009, 05:10 PM
#3
Thread Starter
Lively Member
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!
-
Aug 24th, 2009, 05:16 PM
#4
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
Rate People That Helped You
Mark Thread Resolved When Resolved
-
Aug 24th, 2009, 05:23 PM
#5
Thread Starter
Lively Member
Re: Populate listview with a MySQL Database
 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!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|