Click here to Skip to main content
15,889,834 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I'm using rdlc report with vs 2008. I have been tried to generate amount in words in the body section of rdlc report template. For this, I used custom code.[Report Properties-> Code..].
But I got #Error while generating the report. Please comment.
Posted
Comments
Schatak 9-Apr-14 2:50am    
What error you are getting ? and can you show your code?
normalsoft 9-Apr-14 3:19am    
="Amount in words: " & Code.changeToWords(CStr(FormatNumber(Sum(Fields!dTurnover.Value),2)))

Actually my custom code accepts string value as parameter.
Schatak 9-Apr-14 3:25am    
Definition of ChangeToWords function is?
normalsoft 9-Apr-14 3:29am    
Public Function changeToWords(ByVal numb As [String]) As [String]
Dim numb as string = Convert.toString(val)
Dim val As [String] = ""
Dim wholeNo As [String] = numb
Dim points As [String] = ""
Dim andStr As [String] = ""
Dim pointStr As [String] = ""
Dim endStr As [String] = "Only"
Dim decimalPlace As Integer = numb.IndexOf(".")
If decimalPlace > 0 Then
wholeNo = numb.Substring(0, decimalPlace)
points = numb.Substring(decimalPlace + 1)
If Convert.ToInt32(points) > 0 Then
andStr = "and"
pointStr = translateCents(points)
pointStr= pointStr & " Paise "
End If
End If
val = [String].Format("{0} {1}{2} {3}", translateWholeNumber(wholeNo).Trim(), "Rupees " & andStr, pointStr , endStr)
Return val
End Function

Private Shared Function translateWholeNumber(ByVal number As [String]) As [String]
Dim word As String = ""
Dim beginsZero As Boolean = False
Dim isDone As Boolean = False
Dim dblAmt As Double = (Convert.ToDouble(number))
If dblAmt > 0 Then
beginsZero = number.StartsWith("0")
Dim numDigits As Integer = number.Length
Dim pos As Integer = 0
Dim place As [String] = ""
Select Case numDigits
Case 1

word = ones(number)
isDone = True
Exit Select
Case 2

word = tens(number)
isDone = True
Exit Select
Case 3

pos = (numDigits Mod 3) + 1
place = " Hundred "
Exit Select

Case 4, 5, 6
pos = (numDigits Mod 4) + 1
place = " Thousand "
Exit Select

Case 7, 8, 9
pos = (numDigits Mod 7) + 1
place = " Million "
Exit Select
Case 10

pos = (numDigits Mod 10) + 1
place = " Billion "
Exit Select
Case Else

isDone = True
Exit Select
End Select

If Not isDone Then
word = translateWholeNumber(number.Substring(0, pos)) + place + translateWholeNumber(number.Substring(pos))

If beginsZero Then
word = " and " & word.Trim()
End If
End If

If word.Trim().Equals(place.Trim()) Then
word = ""
End If
End If

Return word.Trim()
End Function

Private Shared Function tens(ByVal digit As [String]) As [String]
Dim digt As Integer = Convert.ToInt32(digit)
Dim name As [String] = Nothing
Select Case digt
Case 10
name = "Ten"
Exit Select
Case 11
name = "Eleven"
Exit Select
Case 12
name = "Twelve"
Exit Select
Case 13
name = "Thirteen"
Exit Select
Case 14
name = "Fourteen"
Exit Select
Case 15
name = "Fifteen"
Exit Select
Case 16
name = "Sixteen"
Exit Select
Case 17
name = "Seventeen"
Exit Select
Case 18
name = "Eighteen"
Exit Select
Case 19
name = "Nineteen"
Exit Select
Case 20
name = "Twenty"
Exit Select
Case 30
name = "Thirty"
Exit Select
Case 40
name = "Fourty"
Exit Select
Case 50
name = "Fifty"
Exit Select
Case 60
name = "Sixty"
Exit Select
Case 70
name = "Seventy"
Exit Select
Case 80
name = "Eighty"
Exit Select
Case 90
name = "Ninety"
Exit Select
Case Else
If digt > 0 Then
name = (tens(digit.Substring(0, 1) & "0") & " ") + ones(digit.Substring(1))
End If
Exit Select
End Select
Return name
End Function

Private Shared Function ones(ByVal digit As [String]) As [String]
Dim digt As Integer = Convert.ToInt32(digit)
Dim name As [String] = ""
Select Case digt
Case 1
name = "One"
Exit Select
Case 2
name = "Two"
Exit Select
Case 3
name = "Three"
Exit Select
Case 4
name = "Four"
Exit Select
Case 5
name = "Five"
Exit Select
Case 6
name = "Six"
Exit Select
Case 7
name = "Seven"
Exit Select
Case 8
name = "Eight"
Exit Select
Case 9
name = "Nine"
Exit Select
End Select
Return name
End Function


Private Shared Function translateCents(ByVal cents As [String]) As [String]
Dim cts As [String] = ""
Dim digit As [String] = ""
Dim engOne As [String] = ""
Dim i As Integer = 0
For i = 0 To Len(cents) - 1
digit = cents(i).ToString()
If digit.Equals("0") Then
engOne = "Zero"
Else
engOne = ones(digit)
End If
cts = cts & " " & engOne
Next
Return cts
End Function

Error is in second line of your code.
VB
Dim numb as string = Convert.toString(val)

just remove this line and it will work fine.
 
Share this answer
 
Comments
normalsoft 9-Apr-14 3:45am    
Sorry. this line was not in code. I forgot to uncomment. It works when remove FormatNumber keyword. But i needs decimal part figures into words.
Schatak 9-Apr-14 4:03am    
="Amount in words: " & Code.ChangeToWords(FormatNumber(Sum(Fields!dTurnover.Value),2))
normalsoft 9-Apr-14 4:22am    
It is not working for me. Comes #Error as mentioned earlier. Works only when remove "FormatNumber" from the code.
Schatak 9-Apr-14 4:24am    
can you please tell me value of Fields!dTurnover.Value individually? what value it is having?
normalsoft 9-Apr-14 4:36am    
it is something like total amount. e.g. 135.60. I think the formatting of decimal and passing as string is not properly executed.
fixed problem of decimal part
VB
Public Function changeToWords(ByVal numb As [String]) As [String]
        Dim val As [String] = ""
        Dim wholeNo As [String] = numb
        Dim points As [String] = ""
        Dim andStr As [String] = ""
        Dim pointStr As [String] = ""
        Dim endStr As [String] = "Only"
        Dim decimalPlace As Integer = numb.IndexOf(".")
        If decimalPlace > 0 Then
            wholeNo = numb.Substring(0, decimalPlace)
            points = numb.Substring(decimalPlace + 1)
            If Convert.ToInt32(points) > 0 Then
                andStr = "and"
                'Change Start here in this block
                If (Len(points) > 1) Then
                    pointStr = translateWholeNumber(points).Trim()

                Else
                    pointStr = translateCents(points)
                End If
                'chnage end
                pointStr = pointStr & " Paise "
            End If
            End If
        val = [String].Format("{0} {1} {2} {3}", translateWholeNumber(wholeNo).Trim(), "Rupees " & andStr, pointStr, endStr)
            Return val
    End Function
 
Share this answer
 
Comments
normalsoft 9-Apr-14 6:58am    
Thanks. It is working in local machine. but not working in production. ???
Schatak 9-Apr-14 6:59am    
same error you are getting?
normalsoft 9-Apr-14 7:15am    
Yes. Same #Error.
Schatak 9-Apr-14 7:19am    
you need to check the data for "dTurnover" might be there is some issue in data
normalsoft 9-Apr-14 7:29am    
I think the problem coming when values comes something like comma(,) separated.

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