Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Error as "input array is longer than the number of columns in this table"

Tags:

c#

vb.net

Code.

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"

What wrong in my code.

Need Help

like image 683
Gopal Avatar asked Feb 28 '10 09:02

Gopal


1 Answers

You need to add columns to this data table first:

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

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
like image 140
Darin Dimitrov Avatar answered Nov 02 '22 07:11

Darin Dimitrov