获取错误,因为“输入数组长于此表中的列数" [英] Getting Error as "input array is longer than the number of columns in this table"

查看:14
本文介绍了获取错误,因为“输入数组长于此表中的列数"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码.

Public Function comb1(ByVal SName As String) As DataTable

        Dim dt As New DataTable
        cmd = New SqlCommand("Select Distinct RName from tb_RS_New", con)
        dr2 = cmd.ExecuteReader
        While (dr2.Read())
            dt.Rows.Add(dr2("RName"))
        End While
        Return dt

End Function

在加载页面时,抛出错误为输入数组长度超过此表中的列数"

While loading the page, the error was thrown as "input array is longer than the number of columns in this table"

我的代码有什么问题.

需要帮助

推荐答案

您需要先在此数据表中添加列:

You need to add columns to this data table first:

Dim dt As New DataTable
dt.Columns.Add("RName", GetType(String))

此外,我对代码中的 concmddr2 变量了解不多,但我强烈建议您处理他们正确:

Also I don't know much about the con, cmd and dr2 variables in your code but I would strongly recommend you to dispose them properly:

Dim dt As New DataTable
dt.Columns.Add("RName", GetType(String))

Using con As New SqlConnection("connection string to the database")
    Using cmd = con.CreateCommand()
        con.Open()
        cmd.CommandText = "Select Distinct RName from tb_RS_New"
        Using dr = cmd.ExecuteReader()
            While (dr.Read())
                dt.Rows.Add(dr("RName"))
            End While
        End Using
    End Using
End Using

这篇关于获取错误,因为“输入数组长于此表中的列数"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆