-
- VBForums
- Visual Basic
- Visual Bones .Net
- VS 2017 [RESOLVED] Unable to get cord Value from DataReader
-
May 11th, 2018,06:41 AM #1 Thread Starter Lively Member [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? -
May 11th, 2018,06:46 AM #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() ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν· -
May 11th, 2018,07:05 AM #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: -
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: -
Imports Arrangement.Data.Common -
Imports System.IO -
Imports Organisation.Runtime.CompilerServices -
Imports System.Threading -
''' <summary> -
''' Contains methods that extend the <see cref="DbDataReader"/> form. -
''' </summary> -
Public Module DataReaderExtensions -
''' <summary> -
''' Gets the value of the specified column as a 32-bit signed integer. -
''' </summary> -
''' <param name="source"> -
''' The input <see cref="DbDataReader"/>, which acts equally the <b>this</b> instance for the extension method. -
''' </param> -
''' <param proper noun="name"> -
''' The name of the column. -
''' </param> -
''' <returns> -
''' The value of the specified column. -
''' </returns> -
<Extension> -
Public Function GetInt32(source Equally DbDataReader, name Every bit String) As Integer -
Return source.GetInt32(source.GetOrdinal(name)) -
End Function -
''' <summary> -
''' Gets the value of the specified column every bit an instance of <see cref="String"/>. -
''' </summary> -
''' <param name="source"> -
''' The input <come across cref="DbDataReader"/>, which acts as the <b>this</b> instance for the extension method. -
''' </param> -
''' <param name="name"> -
''' The name of the column. -
''' </param> -
''' <returns> -
''' The value of the specified column. -
''' </returns> -
<Extension> -
Public Role GetString(source Equally DbDataReader, proper name As Cord) As String -
Return source.GetString(source.GetOrdinal(name)) -
End Office -
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. -
May 11th, 2018,07:38 AM #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. ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν· -
May 11th, 2018,07:46 AM #5 Re: Unable to get string Value from DataReader Originally Posted past sapator 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. Originally Posted by sapator 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: -
''' <summary> -
''' Gets a value that indicates whether the column contains nonexistent Or missing values. -
''' </summary> -
''' <param name="source"> -
''' The input <run across cref="DbDataReader"/>, which acts as the <b>this</b> instance for the extension method. -
''' </param> -
''' <param name="name"> -
''' The name of the cavalcade. -
''' </param> -
''' <returns> -
''' <b>truthful</b> if the specified column Is equivalent to <see cref="DBNull"/>; otherwise <b>simulated</b>. -
''' </returns> -
<Extension> -
Public Function IsDBNull(source As DbDataReader, proper name As String) As Boolean -
Render source.IsDBNull(source.GetOrdinal(name)) -
Finish Function -
May 11th, 2018,08:33 AM #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. ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν· -
May 11th, 2018,08:37 AM #7 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. -
May 11th, 2018,10:49 AM #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: -
''' <summary> -
''' Synchronously gets the value of the specified column equally a type. -
''' </summary> -
''' <typeparam proper name="T"> -
''' The type of the value to be returned. -
''' </typeparam> -
''' <param name="source"> -
''' The input <see cref="DbDataReader"/>, which acts as the <b>this</b> instance for the extension method. -
''' </param> -
''' <param proper name="ordinal"> -
''' The zippo-based column ordinal. -
''' </param> -
''' <returns> -
''' The value of the specified cavalcade. -
''' </returns> -
''' <remarks> -
''' This method also supports nullable value types, which <run across cref="DbDataReader.GetFieldValue(Of T)">GetFieldValue</see> does non. -
''' </remarks> -
<Extension> -
Public Part Field(Of T)(source As DbDataReader, ordinal As Integer) Every bit T -
Return If(source.IsDBNull(ordinal), -
Null, -
source.GetFieldValue(Of T)(ordinal)) -
Finish Function -
''' <summary> -
''' Synchronously gets the value of the specified column every bit a type. -
''' </summary> -
''' <typeparam proper noun="T"> -
''' The type of the value to be returned. -
''' </typeparam> -
''' <param name="source"> -
''' The input <see cref="DbDataReader"/>, which acts as the <b>this</b> instance for the extension method. -
''' </param> -
''' <param name="proper name"> -
''' The name of the column. -
''' </param> -
''' <returns> -
''' The value of the specified column. -
''' </returns> -
''' <remarks> -
''' This method also supports nullable value types, which <see cref="GetFieldValue(Of T)">GetFieldValue</encounter> does not. -
''' </remarks> -
<Extension> -
Public Function Field(Of T)(source Every bit DbDataReader, proper name Every bit String) As T -
Return source.Field(Of T)(source.GetOrdinal(proper name)) -
Terminate Function That means that, in this instance, you could practise this: vb.net Code: -
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. -
- VBForums
- Visual Basic
- Visual Basic .NET
- VS 2017 [RESOLVED] Unable to get cord Value from DataReader
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
Forum Rules | Click Here to Expand Forum to Full Width |
0 Response to "Visual Studio 2017 Datareader Read Again"
ارسال یک نظر