vb.net : 输入数组比此表中的列数长
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18514606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
vb.net : Input array is longer than the number of columns in this table
提问by Helmuteke
i have follow code in my vb.net project the connection to the sql db is fine & ok , only when i try to fill the listbox i get a error
我在我的 vb.net 项目中遵循了代码,与 sql db 的连接很好,只有当我尝试填充列表框时才会出现错误
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
and
和
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
i get error on follow line
我在后续行中出错
myTable.Rows.Add(naam, myReader.GetString(0))
with follow description : Input array is longer than the number of columns in this table.
带有以下说明:输入数组比此表中的列数长。
someone who see ??
看到的人??
采纳答案by bastos.sergio
I suspect that your myTabledatatable does not have any columns...
我怀疑您的数据myTable表没有任何列...
Try changing:
尝试改变:
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
to this:
对此:
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
you should still get an error, but at least this way you'll know what columns are missing...
您仍然应该收到错误消息,但至少通过这种方式您会知道缺少哪些列...
update
更新
Also, change this:
另外,改变这个:
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.
to this:
对此:
'------------
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()

