I am writing an application in vb.Net that pulls data from a table in a SQL Server, loads it into an array, and then checks certain fields against another array that I pulled in from an excel file. I am doing this because certain users are unable to simply import the excel file and query the results.
I've got everything working fine with the test data tables but when I switch the tables to the real ones needed it gives me a Cast Not Valid Exception on the line of code that loads the data table from the SQL Server. Is it because the table is too large to load into the array? Here is the code that gives the error message.
Sub DatabaseConnect()
'Create a Connection object.
myConn = New SqlConnection("Initial Catalog=MAT;" & "Data Source=localhost;Integrated Security=SSPI;")
'Create a Command object.
myCmd = myConn.CreateCommand
myCmd.CommandText = "SELECT * FROM tblMembers" 'This works with the test data
Try
'Open the connection.
myConn.Open()
'Use the SqlDataReader Object to Retrieve Data from SQL Server
myReader = myCmd.ExecuteReader()
'Load the data from the table into DbArray array
'Do While myReader.Read()
'DbRowCounter += 1
'Loop
Dim i As Integer
Do While myReader.Read()
For i = 0 To myReader.FieldCount() - 1
DbArray(DbRowCounter, i) = CStr(myReader.GetString(i)).Trim
Next
DbRowCounter += 1
''results = results & myReader.GetString(0) & vbTab & myReader.GetString(1) & vbLf
Loop
I've got everything working fine with the test data tables but when I switch the tables to the real ones needed it gives me a Cast Not Valid Exception on the line of code that loads the data table from the SQL Server. Is it because the table is too large to load into the array? Here is the code that gives the error message.
Sub DatabaseConnect()
'Create a Connection object.
myConn = New SqlConnection("Initial Catalog=MAT;" & "Data Source=localhost;Integrated Security=SSPI;")
'Create a Command object.
myCmd = myConn.CreateCommand
myCmd.CommandText = "SELECT * FROM tblMembers" 'This works with the test data
Try
'Open the connection.
myConn.Open()
'Use the SqlDataReader Object to Retrieve Data from SQL Server
myReader = myCmd.ExecuteReader()
'Load the data from the table into DbArray array
'Do While myReader.Read()
'DbRowCounter += 1
'Loop
Dim i As Integer
Do While myReader.Read()
For i = 0 To myReader.FieldCount() - 1
DbArray(DbRowCounter, i) = CStr(myReader.GetString(i)).Trim
Next
DbRowCounter += 1
''results = results & myReader.GetString(0) & vbTab & myReader.GetString(1) & vbLf
Loop