I have created codes for connection to SQL Server and I am able to count rows of the datatable as following.
Dim con As New SqlConnection
Dim cmd1 As New SqlCommand
Dim dt As New DataTable
Dim da As New SqlDataAdapter
da.SelectCommand = cmd1
con.ConnectionString = "Data Source=DS;Integrated Security=SSPI;Initial Catalog=test"
con.Open()
cmd1.Connection = con
cmd1.CommandText = "select * from tblTEST"
cmd1.ExecuteNonQuery()
da.Fill(dt)
Dim reader As SqlDataReader = cmd1.ExecuteReader()
Dim C1Count, C2Count, C3Count As Integer
For p As Integer = 0 To dt.Rows.Count - 1
If dt.Rows(p).Item(0).ToString.Substring(0, 2) = "C1" And GetChar(dt.Rows(p).Item(0).ToString, 3) = "A" Then
C1Count += 1 'count class 1
ElseIf dt.Rows(p).Item(0).ToString.Substring(0, 2) = "C2" And GetChar(dt.Rows(p).Item(0).ToString, 3) = "B" Then
C2Count += 1 'count class 2
ElseIf dt.Rows(p).Item(0).ToString.Substring(0, 2) = "C3" And GetChar(dt.Rows(p).Item(0).ToString, 3) = "C" Then
C3Count += 1 'count class 3
End If
Next
------------------------------
Everything works fine until I break my codes down and move the connection codes to a separate class, DataAccess, where other classes can call to this class. In the new form1, the for loop above is changed to:
For p As Integer = 0 To (DataAccess.GetClassCount(Class) - 1)
Where GetClassCount(Class) is a function I create in the DataAccess class that returns an integer for the count. I just copy and paste my connection codes with the dataAdapter into this function. But when I run it, it stops at da.Fill(dt) and jump out the program. Please help me to identify what the problem is and how to fix it. Thank you in advance.