nick447923
Member
- Joined
- Nov 10, 2009
- Messages
- 14
- Programming Experience
- Beginner
I am in a intro VB.net college class and am a little frustrated with a problem I am having using a data reader to read the result set of a query.
Dim automobile As New Automobile
Dim connection As OleDbConnection = PaulMeadeInsuranceDB.GetConnection
Dim selectStatement As String = _
"SELECT * FROM Automobiles WHERE CustNumber = " _
& CustNumber.ToString
Dim selectcommand As New OleDbCommand(selectStatement, connection)
Dim autoList As New List(Of Automobile)
Try
connection.Open()
Dim reader As OleDbDataReader _
= selectcommand.ExecuteReader(CommandBehavior.SingleResult)
If reader.Read Then
Do While reader.Read()
automobile.Modelyear = CInt(reader("Model Year").ToString)
automobile.Description = reader("Description").ToString
automobile.Vin = (reader("VIN#1"))
automobile.Symbol = CInt(reader("Symbol").ToString)
automobile.Age = CInt(reader("Age").ToString)
autoList.Add(automobile)
reader.Read()
Loop
Else
automobile = Nothing
End If
reader.Close()
Return autoList
Catch ex As OleDbException
Throw ex
Catch e As Exception
Throw e
Finally
connection.Close()
End Try
The data reader that reads the result has a depth of 0 which means, I think, that only one record was returned even though there are two records in AUTOMOBILES with CustNumber = CustNumber.ToString. If create a query in Visual Studio that is the exact same as the above Query except with a fixed CustNumber it returns two records instead of one. I have played around with the command behavior but no luck. I have also asked my programming teacher but he was unable to find what was wrong. I even tried changing the Query to "SELECT * FROM Automobiles" and I still get only one record even though there are four records in the Automobiles table!!
Any help would be much appreciated!
Dim automobile As New Automobile
Dim connection As OleDbConnection = PaulMeadeInsuranceDB.GetConnection
Dim selectStatement As String = _
"SELECT * FROM Automobiles WHERE CustNumber = " _
& CustNumber.ToString
Dim selectcommand As New OleDbCommand(selectStatement, connection)
Dim autoList As New List(Of Automobile)
Try
connection.Open()
Dim reader As OleDbDataReader _
= selectcommand.ExecuteReader(CommandBehavior.SingleResult)
If reader.Read Then
Do While reader.Read()
automobile.Modelyear = CInt(reader("Model Year").ToString)
automobile.Description = reader("Description").ToString
automobile.Vin = (reader("VIN#1"))
automobile.Symbol = CInt(reader("Symbol").ToString)
automobile.Age = CInt(reader("Age").ToString)
autoList.Add(automobile)
reader.Read()
Loop
Else
automobile = Nothing
End If
reader.Close()
Return autoList
Catch ex As OleDbException
Throw ex
Catch e As Exception
Throw e
Finally
connection.Close()
End Try
The data reader that reads the result has a depth of 0 which means, I think, that only one record was returned even though there are two records in AUTOMOBILES with CustNumber = CustNumber.ToString. If create a query in Visual Studio that is the exact same as the above Query except with a fixed CustNumber it returns two records instead of one. I have played around with the command behavior but no luck. I have also asked my programming teacher but he was unable to find what was wrong. I even tried changing the Query to "SELECT * FROM Automobiles" and I still get only one record even though there are four records in the Automobiles table!!
Any help would be much appreciated!