Visual Studio 2017 Datareader Read Again

  1. #1

    Penfound is offline

    Thread Starter

    Lively Member


    Resolved [RESOLVED] Unable to get string Value from DataReader

    I have a string variable tmpW and I'm trying to become the cord value from a DataReader column past I just get an error that it tin can'T catechumen a string to an integer, Doh!

    Code:

                                      Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM [BettingSlip]", Conn)         Dim dr As OleDbDataReader = cmd.ExecuteReader         Dim WCount As Integer = 0         Dim tmpW Every bit String         Dim tmpP As String         Dim tmpL As String         While dr.Read             tmpW = doctorGetString("RP")             If tmpW = "W" Then                 WCount += 1             Terminate If             Chart1.Series("Won").Points.AddXY(dr("RDate").ToString, dr("WCount").ToString)         End While         dr.Shut()         cmd.Dispose()
    What am I doing wrong please?

  2. #ii

    Re: Unable to go string Value from DataReader

    This will get the "alphabetize" of the column.
    You might try similar this:
    dr("RP").Tostring()

    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·


  3. #3

    Re: Unable to go string Value from DataReader

    The solution suggested past sapator will certainly piece of work in this case simply that's considering every type has a ToString method. If you lot find yourself in the same situation only requiring data of a unlike type then there'southward no equivalent. There is a more general solution.

    The GetString method of a data reader (and all other methods that become information of a specific type) take only a cavalcade ordinal, not a cavalcade proper name. If you want to utilise a proper name then you need to get the ordinal for that name, due east.thousand.

    vb.net Code:

                                    
    1. tmpW = doctorGetString(mdGetOrdinal("RP"))

    If you follow the CodeBank link in my signature you can check out my thread on Useful Extension Methods. Ane of the library projects in the attached solution includes methods that extend the information reader class to provide the very functionality you want, e.g.

    vb.cyberspace Lawmaking:

                                    
    1. Imports Arrangement.Data.Common

    2. Imports System.IO

    3. Imports Organisation.Runtime.CompilerServices

    4. Imports System.Threading

    5. ''' <summary>

    6. ''' Contains methods that extend the <see cref="DbDataReader"/> form.

    7. ''' </summary>

    8. Public Module DataReaderExtensions

    9.     ''' <summary>

    10.     ''' Gets the value of the specified column as a 32-bit signed integer.

    11.     ''' </summary>

    12.     ''' <param name="source">

    13.     ''' The input <see cref="DbDataReader"/>, which acts equally the <b>this</b> instance for the extension method.

    14.     ''' </param>

    15.     ''' <param proper noun="name">

    16.     ''' The name of the column.

    17.     ''' </param>

    18.     ''' <returns>

    19.     ''' The value of the specified column.

    20.     ''' </returns>

    21.     <Extension>

    22.     Public Function GetInt32(source Equally DbDataReader, name Every bit String) As Integer

    23.         Return source.GetInt32(source.GetOrdinal(name))

    24.     End Function

    25.     ''' <summary>

    26.     ''' Gets the value of the specified column every bit an instance of <see cref="String"/>.

    27.     ''' </summary>

    28.     ''' <param name="source">

    29.     ''' The input <come across cref="DbDataReader"/>, which acts as the <b>this</b> instance for the extension method.

    30.     ''' </param>

    31.     ''' <param name="name">

    32.     ''' The name of the column.

    33.     ''' </param>

    34.     ''' <returns>

    35.     ''' The value of the specified column.

    36.     ''' </returns>

    37.     <Extension>

    38.     Public Role GetString(source Equally DbDataReader, proper name As Cord) As String

    39.         Return source.GetString(source.GetOrdinal(name))

    40.     End Office

    41. Terminate Module

    Include that module in your project or reference the compiled library and you lot tin call GetString exactly as you're trying to now.

  4. #4

    Re: Unable to get cord Value from DataReader

    You can besides do:

    dr("RP") , this will return the actual value in they actual type.
    So (I wouldn't do that, look in specific situation) Dim x equally object = dr("RP") will e'er work simply you better specify the object type.
    You must besides consider null values by using "Is DBNull.Value" check.

    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·


  5. #5

    Re: Unable to get string Value from DataReader

    Quote Originally Posted past sapator View Post

    dr("RP") , this volition return the actual value in they actual type.

    Information technology will return an Object reference, and so you'd demand to cast it as its bodily type to utilize it as that type. That's why methods like GetString are used, i.e. they don't require a cast.

    Quote Originally Posted by sapator View Post

    You must besides consider null values by using "Is DBNull.Value" check.

    It'south worth noting that the data reader classes accept their own IsDBNull method. Like GetString though, it merely accepts an ordinal. You can telephone call GetOrdinal again though, and my extension library includes an corresponding method:

    vb.cyberspace Lawmaking:

                                    
    1. ''' <summary>

    2. ''' Gets a value that indicates whether the column contains nonexistent Or missing values.

    3. ''' </summary>

    4. ''' <param name="source">

    5. ''' The input <run across cref="DbDataReader"/>, which acts as the <b>this</b> instance for the extension method.

    6. ''' </param>

    7. ''' <param name="name">

    8. ''' The name of the cavalcade.

    9. ''' </param>

    10. ''' <returns>

    11. ''' <b>truthful</b> if the specified column Is equivalent to <see cref="DBNull"/>; otherwise <b>simulated</b>.

    12. ''' </returns>

    13. <Extension>

    14. Public Function IsDBNull(source As DbDataReader, proper name As String) As Boolean

    15.     Render source.IsDBNull(source.GetOrdinal(name))

    16. Finish Function


  6. #six

    Re: Unable to go string Value from DataReader

    No problem.
    I was merely expanding the alternatives. I usually utilize the simple dr() method as I wrap this in a "getsqldata" function that I know exactly what type I need to bandage to just that is but me.

    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·


  7. #7

    Penfound is offline

    Thread Starter

    Lively Member


    Re: Unable to go string Value from DataReader

    Many thanks to you all and specially to jmcilhinney for the perfect respond.

  8. #viii

    Re: [RESOLVED] Unable to get string Value from DataReader

    There'southward another characteristic of my extension library that you lot may find useful. As part of LINQ to DataSet, in that location is a Field method that extends the DataRow grade and enables y'all to retrieve a field value past ordinal or name as its native blazon or Nothing if the field is NULL. I've also included a similar Field method that extends the data reader classes in the same manner:

    vb.net Lawmaking:

                                      
    1. ''' <summary>

    2. ''' Synchronously gets the value of the specified column equally a type.

    3. ''' </summary>

    4. ''' <typeparam proper name="T">

    5. ''' The type of the value to be returned.

    6. ''' </typeparam>

    7. ''' <param name="source">

    8. ''' The input <see cref="DbDataReader"/>, which acts as the <b>this</b> instance for the extension method.

    9. ''' </param>

    10. ''' <param proper name="ordinal">

    11. ''' The zippo-based column ordinal.

    12. ''' </param>

    13. ''' <returns>

    14. ''' The value of the specified cavalcade.

    15. ''' </returns>

    16. ''' <remarks>

    17. ''' This method also supports nullable value types, which <run across cref="DbDataReader.GetFieldValue(Of T)">GetFieldValue</see> does non.

    18. ''' </remarks>

    19. <Extension>

    20. Public Part Field(Of T)(source As DbDataReader, ordinal As Integer) Every bit T

    21.     Return If(source.IsDBNull(ordinal),

    22.               Null,

    23.               source.GetFieldValue(Of T)(ordinal))

    24. Finish Function

    25. ''' <summary>

    26. ''' Synchronously gets the value of the specified column every bit a type.

    27. ''' </summary>

    28. ''' <typeparam proper noun="T">

    29. ''' The type of the value to be returned.

    30. ''' </typeparam>

    31. ''' <param name="source">

    32. ''' The input <see cref="DbDataReader"/>, which acts as the <b>this</b> instance for the extension method.

    33. ''' </param>

    34. ''' <param name="proper name">

    35. ''' The name of the column.

    36. ''' </param>

    37. ''' <returns>

    38. ''' The value of the specified column.

    39. ''' </returns>

    40. ''' <remarks>

    41. ''' This method also supports nullable value types, which <see cref="GetFieldValue(Of T)">GetFieldValue</encounter> does not.

    42. ''' </remarks>

    43. <Extension>

    44. Public Function Field(Of T)(source Every bit DbDataReader, proper name Every bit String) As T

    45.     Return source.Field(Of T)(source.GetOrdinal(proper name))

    46. Terminate Function

    That means that, in this instance, you could practise this:

    vb.net Code:

                                      
    1. tmpW = doctorField(Of String)("RP")

    and y'all'd go your desired String value or, if the field was NULL, you'd get Nothing. If your column is not nullable so there'due south no issue using GetString only, if it is nullable, y'all need to check whether it is NULL, just like sapator suggested. You tin do that separately using the IsDBNull extension I provided just this Field extension rolls it all into a unmarried call.
    Last edited past jmcilhinney; May 11th, 2018 at 10:52 AM.

Posting Permissions

  • You may not postal service new threads
  • You lot may non post replies
  • You may not mail attachments
  • You may not edit your posts
  • BB code is On
  • Smilies are On
  • [IMG] lawmaking is On
  • [VIDEO] code is On
  • HTML code is Off

Click Here to Expand Forum to Full Width

filerdoeste.blogspot.com

Source: https://www.vbforums.com/showthread.php?862439-RESOLVED-Unable-to-get-string-Value-from-DataReader

0 Response to "Visual Studio 2017 Datareader Read Again"

ارسال یک نظر

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel