Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用MySQL将项目添加到列表框_Mysql_Vb.net_Listbox - Fatal编程技术网

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

如何使用MySQL将项目添加到列表框,mysql,vb.net,listbox,Mysql,Vb.net,Listbox,我试图将所有行中的标题()添加到列表框中,但出现以下错误: System.Data.dll中发生类型为“System.IndexOutOfRangeException”的未处理异常。我不知道现在该怎么做,我已经尝试添加函数,使其成为名为item的变量 Public Function updatenews() Dim MySqlConnection As New MySqlConnection() Dim newsmydatatable As New DataTable D

我试图将所有行中的标题()添加到列表框中,但出现以下错误: 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
如果您有:

      ListBox1.Items.Add(newsmydatatable.Rows(rowcount).Item("title"))
rowcount += 1
将其更改为:

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

“else”中的代码看起来有点乱七八糟-可能是在编辑中-但看起来你已经爱上了那颗老栗子-如果rows.count是10,那么它们的编号是从0到9Awh,谢谢!刚刚将0更改为`现在可以工作了!谢谢并删除两个for循环,您只需要一个:)