miguel.renaud
miguel.renaud

Reputation: 85

How do I add items to a list box using MySQL

I'm trying to add titles (picture) from all rows to a listbox and I'm getting this error: An unhandled exception of type 'System.IndexOutOfRangeException' occurred in System.Data.dll. Not sure what to do at this point I've tried adding functions, making it a variable called item..

Public Function updatenews()
    Dim MySqlConnection As New MySqlConnection()
    Dim newsmydatatable As New DataTable
    Dim rowcount As Integer = 0
    Dim amount As Integer

    MySqlConnection.ConnectionString = "server=" + host + "; user id=" + user + "; password=" + password + "; database=website;"
    Try
        MySqlConnection.Open()
    Catch myerror As MySqlException
        MessageBox.Show("Cannot connect news server: " & myerror.Message & "Please check your internet connection settings and try again. If problem persists contact support.")
        Label3.Text = "Error!"
    End Try
    Dim myadapter As New MySqlDataAdapter
    Dim newsmydatatable As New DataTable
    Dim sqlquary = "SELECT * FROM news;"
    Dim command As New MySqlCommand
    command.Connection = MySqlConnection
    command.CommandText = sqlquary
    myadapter.SelectCommand = command
    myadapter.Fill(newsmydatatable)
    Dim mydata As MySqlDataReader
    mydata = command.ExecuteReader()
    If mydata.HasRows = 0 Then

    Else

        amount = newsmydatatable.Rows.Count


        MsgBox(amount)
        For value As Integer = 0 To amount
            For value As Integer = 0 To amount
            ListBox1.Items.Add(newsmydatatable.Rows(rowcount).Item("title"))
            rowcount += 1
        Next

    End If
End Function

Upvotes: 0

Views: 193

Answers (1)

Frank_Vr
Frank_Vr

Reputation: 659

where you have:

      ListBox1.Items.Add(newsmydatatable.Rows(rowcount).Item("title"))
rowcount += 1

change it to:

   ListBox1.Items.Add(newsmydatatable.Rows(value).Item("title"))

Upvotes: 1

Related Questions