• Hello and welcome to our new forums. We upgraded our forum sites to a more robust and modern system which we hope you will enjoy. Be sure to check out your profile by clicking the button on the top right and configure your preferences, signature, time zone, avatar, etc. as you wish. If you need help with using this new forum'ware try the help link on the bottom right.

    Click here to review your account now.

Question 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
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,027
Location
Sydney, Australia
Programming Experience
10+
Yes, you are missing something obvious. You posted the declaration of the GetString method:
Code:
Public Overrides Function GetString ([B][U]ordinal As Integer[/U][/B]) As String
Look at how you're calling it:
Code:
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.
 
Top Bottom