如何使用MySQL将项添加到列表框中

时间:2014-03-22 22:40:29

标签: mysql vb.net listbox

我正在尝试将所有行的标题(picture)添加到列表框中,我收到此错误: System.Data.dll中发生了未处理的“System.IndexOutOfRangeException”类型异常。不知道该做什么,我已经尝试添加函数,使其成为一个名为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

1 个答案:

答案 0 :(得分:1)

你有:

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

将其更改为:

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