Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two decimal values, and a have to subtract them...
When I read the first one form a dataset (the dataset returns the correct value ex.0.012) then when I gave the value to a variable it converts it to a 12D) How can I avoid this problem, I want the variable to get the same value 0.012... Here is my code
VB
Private _Value As Decimal 
    Public Property Value As Decimal
        Get
            Return _Value
        End Get
        Set(ByVal value As Decimal)
            _Value = value
        End Set
    End Property
'-------------- The method---------------
 Value = EX.GetValue(a, b, c).Tables(0).Rows(0)(0)



The method GetValue It returns a dataset with the value:

VB
Public Function GetValue(ByVal a As String, ByVal bAs String, ByVal s As String) As DataSet

        Dim ds As DataSet
        ds = ExecDataSet(Source.sp, "MyProcedure", "Expertiza", a, b, c)
        Return ds

    End Function

AND the ExecDataset looks like:
VB
Public Function ExecDataSet _
       (ByVal source As Source, ByVal strStoredProc As String, _
       ByVal tableName As String, _
       ByVal ParamArray objValues() As Object) _
       As DataSet

       Dim objCommand As SqlCommand
       Dim objDataSet As DataSet
       Dim objAdapter As New SqlClient.SqlDataAdapter

       objCommand = New SqlCommand

       objCommand.CommandText = strStoredProc
       objCommand.CommandTimeout = 1800 ' 30 min.

       objCommand.CommandType = source 'cmdType
       If dbConnection.State <> ConnectionState.Closed Then dbConnection.Close()
       objCommand.Connection = dbConnection

       Try
           If objCommand.Connection.State = ConnectionState.Closed Then
               objCommand.Connection.Open()
           End If
           If source = source.sp Then
               If (objValues.Length = 0) Then
                   'AddFieldParameters(objCommand)
               Else
                   AddParameters(objCommand, objValues)
               End If
           End If
           objAdapter = New SqlDataAdapter(objCommand)
           objDataSet = New DataSet

           objAdapter.Fill(objDataSet, tableName)
       Catch ex As Exception
           If ConnectionState.Open Then
               objCommand.Connection.Close()
           End If
           Throw ex
       Finally
           objAdapter.Dispose()
           objCommand.Dispose()
           If ConnectionState.Open Then
               objCommand.Connection.Close()
           End If

       End Try

       Return objDataSet
   End Function
Posted
Updated 13-Jan-14 0:23am
v4
Comments
phil.o 13-Jan-14 5:16am    
Could you detail the content of the GetValue() method, as well as any class field that it could use? What is the EX variable?
Voley 13-Jan-14 5:48am    
Public Function GetValue(ByVal a As String, ByVal bAs String, ByVal s As String) As DataSet

Dim ds As DataSet
ds = ExecDataSet(Source.sp, "MyProcedure", "Expertiza", a, b, c)
Return ds

End Function
phil.o 13-Jan-14 6:19am    
Don't post it as a reply to a comment; better improve your question and put relevant code in it.

1 solution

So EX.GetValue(a, b, c) returns a Dataset object. You are taking the first table, looking at the first row, and returning the value in the first column.

Problem is, that is an Object, and your property expects a Decimal. The Framework can handle this by making a best guess, but sometimes it guesses wrong. This is why I find it best to write and compile my apps with Option Explicit = True, which tells the compiler not to make ANY assumptions and just flag it as an error.

Try explicitly converting the result before assigning it, and see if it makes any difference:
VB.NET
Value = CDbl(EX.GetValue(a, b, c).Tables(0).Rows(0)(0))
The other thing to check is to verify that the value in the database is the value you are expecting: it may actually be 12 rather than 0.012.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900