Resolved I don't understand datareader.getstring( )

BobInCalgary

New member
Joined
Dec 15, 2012
Messages
4
Programming Experience
3-5
I am reading an access database and populating various objects using oledb datareader.

The couple of lines of code below works as intended:
dim somestring as string
somestring = reader("eventname").ToString

(the data element 'eventname' is defined as a text field in the database)

however
dim somestring as string
somestring = reader.getstring("eventname")

throws the error "conversion from string 'eventname' to integer is not valid"

The msdn documentation states: OleDbDataReader.GetString Method
Gets the value of the specified column as a string
syntax:
Public Overrides Function GetString (ordinal As Integer) As String

My app works fine but I would have thought that .getstring would return a string that could be assigned to a string variable?
Perhaps I'm missing something obvious, but would appreciate any clarification.

Thanks
 
Yes, you are missing something obvious. You posted the declaration of the GetString method:
VB.NET:
Public Overrides Function GetString ([B][U]ordinal As Integer[/U][/B]) As String
Look at how you're calling it:
VB.NET:
somestring = reader.getstring([B][U]"eventname"[/U][/B])
Look at the error message:
"conversion from string 'eventname' to integer is not valid"
Do you see the issue now? It's not what GetString is returning that's the issue; it's what you're passing in. GetString takes a column index only, NOT a column name. It is a bit odd that they didn't provide an overload that takes a name but that's the way it is. You can use the GetOrdinal method to get the index of a column by name and then pass that result to GetString.
 
Back
Top