Results 1 to 3 of 3

Thread: I don't understand datareader.getstring( )

  1. #1
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2012
    Posts
    4
    Reputation
    0

    I don't understand datareader.getstring( )

    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

  2. #2
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    14,008
    Reputation
    1731
    Yes, you are missing something obvious. You posted the declaration of the GetString method:
    Code:
    Public Overrides Function GetString (ordinal As Integer) As String
    Look at how you're calling it:
    Code:
    somestring = reader.getstring("eventname")
    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.

  3. #3
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2012
    Posts
    4
    Reputation
    0
    Thank you for your rapid response. Much appreciated.

Similar Threads

  1. Question Conversion from String Error - but I don't understand why it's happening?
    By thebatfink in forum VB.NET General Discussion
    Replies: 3
    Last Post: 12-19-2012, 11:42 AM
  2. Replies: 0
    Last Post: 06-12-2011, 6:05 AM
  3. Question strange info I don't understand
    By bhealy in forum VB.NET General Discussion
    Replies: 3
    Last Post: 05-29-2008, 6:32 AM
  4. Can't Combine Strings from ASCIIencoding.GetString
    By swu in forum VB.NET General Discussion
    Replies: 1
    Last Post: 04-26-2008, 4:01 AM
  5. Error I don't understand
    By PJRoyle in forum VB.NET General Discussion
    Replies: 1
    Last Post: 09-26-2004, 10:32 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •