Sebastien Therien
Sebastien Therien

Reputation: 1

Vb.net Mysql Multiple queries in one data

I'm trying to make a chat in Vb.net using MySQL and I want to load the chat into a list-box where would be the (username) ''par'' and the (message).

But it keeps showing me errors and i don't understand how to solve it.

Here's my code:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


    Dim stringConn As String
    Dim stringCmd As String
    Dim myConn As MySqlConnection
    Dim myCmd As MySqlCommand


    stringCmd = "SELECT par, message FROM chat"


    stringConn = "server=localhost; user id=studio; password=mypw; database=studio;"


    myConn = New MySqlConnection(stringConn)


    myCmd = New MySqlCommand(stringCmd, myConn)

    myConn.Open()


    Dim myReader As MySqlDataReader


    myReader = myCmd.ExecuteReader()

    'Reset your List box here.
    ListBox1.Items.Clear()

    While myReader.Read()
       --------------Here is my problem ---------------
        ListBox1.Items.Add(myReader.GetString(1//username//) & " " & myReader.GetString(2//message//))

----end-- of the---problem---

    End While

    myReader.Close()
    myConn.Close()
End Sub

Upvotes: 0

Views: 263

Answers (1)

pLurchi
pLurchi

Reputation: 61

You could try this:

If myReader.HasRows = True Then
    Do While myReader.Read()
        ListBox1.Items.Add(myReader(0) & " " & myReader(1))
    Loop
End If

Best Regards

Upvotes: 1

Related Questions