Begging you for help
I m coding a little project that read inside a text files
The file can be query to display a record and answer accordingly (found display it ,not found display a error message) also display some information regarding the Balance of the customer
(withdraw,deposit)
Presently i m having a trouble with my business class that instead of displaying the first record display the following error message (function GetData)
" invalidCastException was unhandled
conversion from string "500.00" to type decimal is not valid"
conversion from string "500.00" to type decimal is not valid"
We read the whole line of the file and as information is separed by comma we decide to split according to the comma and save all the information inside an array of strings.Now when we proceed with the search..First element will be ID, second : name, third : balance.
At this stage my program does not display the record
following is the code of the business class. I have highlight in Blue and BOLD the trouble line( See function GetData )
Imports System.IO
Imports System.IO.File
Public Class Account
Private maccountId As String
Private maccountname As String
Private mbalance As Decimal
Private mFilepath As String
Private mLastError As String
Private mTotalDeposits As Decimal
Private mTotalwithdrawls As Decimal
#Region "Public properties"
Public Property AccountID() As String
Get
Return mAccountId
End Get
Set(ByVal value As String)
maccountId = value
End Set
End Property
Public Property Accountname() As String
Get
Return mAccountname
End Get
Set(ByVal value As String)
mAccountname = value
End Set
End Property
Public ReadOnly Property balance() As Decimal
Get
Return mbalance
End Get
End Property
Public Property Filepath() As String
Get
Return mFilePath
End Get
Set(ByVal value As String)
mFilepath = value
End Set
End Property
Public Property LastError() As String
Get
Return mLastError
End Get
Set(ByVal value As String)
mLastError = value
End Set
End Property
Public ReadOnly Property TotalDeposits() As Decimal
Get
Return mTotalDeposits
End Get
End Property
Public ReadOnly Property Totalwithdrawals() As Decimal
Get
Return mTotalwithdrawls
End Get
End Property
#End Region
#Region "Public methods"
Public Sub New(ByVal paccountId As String)
maccountId = paccountId
maccountname = String.empty
mbalance = 0D
End Sub
Public Function Getdata() As Boolean
' read the data file ,try to find the account ID
'if found read the account name and balance and
'return true .If not found ,return false
Dim infile As StreamReader = Nothing
mLastError = String.empty
Try
infile = openText(mFilepath)
While Not infile.endofstream
Dim entireline As String = infile.readline()
Dim fields() As String = entireline.split(","c)
If fields(0) = maccountId Then
mAccountname = fields(1)
mbalance = CDec(fields(2))
mTotalDeposits = 0D
mTotalwithdrawls = 0D
End If
End While
mLastError = "account " & mAccountId & " not found"
Return False
Finally
If infile IsNot Nothing Then infile.close()
End Try
Return False
End Function
Public Sub Deposit(ByVal amount As Decimal)
' deposit the amount in the account by adding it
'to the balance
mbalance += amount
mTotalDeposits += amount
End Sub
Public Function withdraw(ByVal amount As Decimal) As Boolean
'withdraw amount from the account if the existing balance is
' at least as large as the amount, and return true
' return false if balance is less than amount
If amount <= mbalance Then
mbalance -= amount
mTotalwithdrawls += amount
Return True
Else
mLastError = "balance is too low to withdraw the requested amount"
Return False
End If
End Function
#End Region
End Class
Imports System.IO.File
Public Class Account
Private maccountId As String
Private maccountname As String
Private mbalance As Decimal
Private mFilepath As String
Private mLastError As String
Private mTotalDeposits As Decimal
Private mTotalwithdrawls As Decimal
#Region "Public properties"
Public Property AccountID() As String
Get
Return mAccountId
End Get
Set(ByVal value As String)
maccountId = value
End Set
End Property
Public Property Accountname() As String
Get
Return mAccountname
End Get
Set(ByVal value As String)
mAccountname = value
End Set
End Property
Public ReadOnly Property balance() As Decimal
Get
Return mbalance
End Get
End Property
Public Property Filepath() As String
Get
Return mFilePath
End Get
Set(ByVal value As String)
mFilepath = value
End Set
End Property
Public Property LastError() As String
Get
Return mLastError
End Get
Set(ByVal value As String)
mLastError = value
End Set
End Property
Public ReadOnly Property TotalDeposits() As Decimal
Get
Return mTotalDeposits
End Get
End Property
Public ReadOnly Property Totalwithdrawals() As Decimal
Get
Return mTotalwithdrawls
End Get
End Property
#End Region
#Region "Public methods"
Public Sub New(ByVal paccountId As String)
maccountId = paccountId
maccountname = String.empty
mbalance = 0D
End Sub
Public Function Getdata() As Boolean
' read the data file ,try to find the account ID
'if found read the account name and balance and
'return true .If not found ,return false
Dim infile As StreamReader = Nothing
mLastError = String.empty
Try
infile = openText(mFilepath)
While Not infile.endofstream
Dim entireline As String = infile.readline()
Dim fields() As String = entireline.split(","c)
If fields(0) = maccountId Then
mAccountname = fields(1)
mbalance = CDec(fields(2))
mTotalDeposits = 0D
mTotalwithdrawls = 0D
End If
End While
mLastError = "account " & mAccountId & " not found"
Return False
Finally
If infile IsNot Nothing Then infile.close()
End Try
Return False
End Function
Public Sub Deposit(ByVal amount As Decimal)
' deposit the amount in the account by adding it
'to the balance
mbalance += amount
mTotalDeposits += amount
End Sub
Public Function withdraw(ByVal amount As Decimal) As Boolean
'withdraw amount from the account if the existing balance is
' at least as large as the amount, and return true
' return false if balance is less than amount
If amount <= mbalance Then
mbalance -= amount
mTotalwithdrawls += amount
Return True
Else
mLastError = "balance is too low to withdraw the requested amount"
Return False
End If
End Function
#End Region
End Class
PS
I have tried to replace the line i m having the same error message
The file location is correct
Thanks in advance for your kind help
Regards
Armand
VulpesPosted Feb 26, 2012, 9:56 AM
I'd try replacing the offending line with this one:
mbalance = Convert.ToDecimal(fields(2), CultureInfo.GetCultureInfo("en-US"))