Results 1 to 5 of 5

Thread: [RESOLVED] Populate listview with a MySQL Database

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    115

    Resolved [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

  2. #2
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: Populate listview with a MySQL Database

    Just use a reader...

    If your listview name it's myList:
    VB.NET Code:
    1. Dim myReader As MySqlDataReader
    2. myReader = command.ExecuteReader
    3. While myReader.Read
    4.   myList.Items.Add(myReader(0) & myReader(1) & ... Rest of values)
    5. 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

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    115

    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!

  4. #4
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    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

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    115

    Re: Populate listview with a MySQL Database

    Quote Originally Posted by mickey_pt View Post
    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
  •  



Click Here to Expand Forum to Full Width