1

私はvb.netプロジェクトのコードに従っています.SQLデータベースへの接続は問題ありません.リストボックスに入力しようとするとエラーが発生します.

Public Class Form1
    Private myTable As New DataTable()

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    GetSerialPortNames()
    FillListBox()

    '------------
    myTable.Columns.Add("naam", GetType(String))
    myTable.Columns.Add("waarde", GetType(Integer))   '<<<< change the type of this column to what you actually need instead of integer.


    ListBox1.DisplayMember = "naam"
    ListBox1.ValueMember = "waarde"
    ListBox1.DataSource = myTable

End Sub

Private Sub FillListBox()
    Dim naam As String
    Dim stringConn As String
    Dim stringCmd As String
    Dim myConn As MySqlConnection
    Dim myCmd As MySqlCommand

    'Frame your query here.
    stringCmd = "SELECT id,naam,voornaam FROM deelnemers WHERE finger = FALSE ORDER BY naam "

    'Frame your connection string here.
    stringConn = "********************************************"

    'Get your connection here.
    myConn = New MySqlConnection(stringConn)

    'Get a command by using your connection and query.
    myCmd = New MySqlCommand(stringCmd, myConn)

    'Open the connection.
    myConn.Open()

    'create a reader to store the datum which will be returned from the DB
    Dim myReader As MySqlDataReader

    'Execute your query using .ExecuteReader()
    myReader = myCmd.ExecuteReader()

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

    While (myReader.Read())
        'Add the items from db one by one into the list box.
        naam = myReader.GetString(1) & " " & myReader.GetString(2)
        'ListBox2.Items.Add((naam))
        myTable.Rows.Add(naam, myReader.GetString(0))
    End While

    'Close the reader and the connection.
    myReader.Close()
    myConn.Close()

End Sub

フォローラインでエラーが発生します

myTable.Rows.Add(naam, myReader.GetString(0))

次の説明: 入力配列は、このテーブルの列数よりも長くなっています。

誰かが見る??

4

1 に答える 1

1

あなたのmyTableデータテーブルには列がないと思われます...

変更してみてください:

While (myReader.Read())
    'Add the items from db one by one into the list box.
    naam = myReader.GetString(1) & " " & myReader.GetString(2)
    'ListBox2.Items.Add((naam))
    myTable.Rows.Add(naam, myReader.GetString(0))
End While

これに:

Dim row As DataRow
While (myReader.Read())
    'Add the items from db one by one into the list box.
    row = myTable.NewRow()
    row("naam") = myReader.GetString(1) & " " & myReader.GetString(2)
    row("waarde") = myReader.GetString(0)
    myTable.Rows.Add(row)
End While

それでもエラーが発生するはずですが、少なくともこの方法で、どの列が欠落しているかがわかります...

アップデート

また、これを変更します。

FillListBox()

'------------
myTable.Columns.Add("naam", GetType(String))
myTable.Columns.Add("waarde", GetType(Integer))   '<<<< change the type of this column to what you actually need instead of integer.

これに:

'------------
myTable.Columns.Add("naam", GetType(String))
myTable.Columns.Add("waarde", GetType(Integer))   '<<<< change the type of this column to what you actually need instead of integer.

FillListBox()
于 2013-08-29T15:23:53.523 に答える