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

The 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 

An error was selected while loading the page because "the input array is longer than the number of columns in this table"

What's wrong with my code.

Help is needed

+4
source share
1 answer

First you need to add columns to this data table:

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

Also, I know little about the variables con , cmd and dr2 in your code, but I strongly recommend that you dispose of them correctly:

 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 
+11
source

Source: https://habr.com/ru/post/1302654/


All Articles