Getting a Cast Not Valid Exception

kres1983

New member
Joined
Jan 25, 2006
Messages
3
Programming Experience
1-3
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
 
So, if i'm right you are loading your table into a 2 dimensional array. But why are you converting to string when you've already called the getstring method of the datareader, i assume they are not all string values in the table. If not you could try the getvalue method fo the datareader instead. This returns the value of the column at the specified ordinal in it's native format.
 
Back
Top