Solved

CONVERTING VALUE FROM DATAGRID TO CURRENCY 50pts

Posted on 2006-11-14
6
240 Views
Last Modified: 2008-02-26
ok so i have a crosstab query with a bunch of string numbre values that need to be converted to currency im my datagrid.

so i figured id loop through the dataset and change each value. then rebind to the grid when finished.
which works until i have to return the value back to the dataset. where it promptly craps out.


Public Function addDollarSign()
        Dim intCount As Integer
        Dim x As Integer
        Dim numColumns As Integer
        Dim strCellValue As String
        Dim intRow As Integer
        Dim intColumn As Integer
        Dim index As Integer

        Dim dateArray(10) As String
        dateArray(0) = "1/1/2000"
        dateArray(1) = "11/1/2006"
        dateArray(2) = "12/1/2006"
        dateArray(3) = "1/1/2007"
        dateArray(4) = "2/1/2007"
        dateArray(5) = "3/1/2007"
        dateArray(6) = "4/1/2007"
        dateArray(7) = "5/1/2007"
        dateArray(8) = "6/1/2007"
        dateArray(9) = "7/1/2007"

        index = 0
        intRow = 1
        intColumn = 3
        x = 1
        intCount = ds2.Tables("result").Rows.Count()

        'switch column after all rows finished
        Try
            Do Until intColumn = dateArray.Length
                Do Until x = intCount
                    'get value
                    strCellValue = ds2.Tables("result").Rows(x).Item(dateArray(index))
                    'add $ to value if not = 0
                    If strCellValue <> "0" Then
                        strCellValue = FormatCurrency(strCellValue, 2)
                        'return value
               ds2.Tables("result").Rows(x).Item(dateArray(index)) = strCellValue '<--errors out
                    End If
                Loop
                intRow = intRow + 1
                index = index + 1
            Loop
            intColumn = intColumn + 1
        Catch ex As Exception
            MsgBox(ex.Message)
            Debug.WriteLine(ex.Message)
            Exit Function
        End Try
    End Function

here is the error that is produced

System.FormatException: Input string was not in a correct format.
   at System.Number.ParseDouble(String s, NumberStyles style, NumberFormatInfo info)
   at System.Double.Parse(String s, NumberStyles style, IFormatProvider provider)
   at System.Convert.ToDouble(String value, IFormatProvider provider)
   at System.String.System.IConvertible.ToDouble(IFormatProvider provider)
   at System.Convert.ToDouble(Object value)
   at System.Data.Common.DoubleStorage.Set(Int32 record, Object value)
   at System.Data.DataColumn.set_Item(Int32 record, Object value)Couldn't store <$29,324.00> in 1/1/2000 Column.  Expected type is Double.

so basically it wants a double but if i used double i obviously lose the currency format. and a custom grid style/format is out of the question.

so what do i need to do to get thses values back into the dataset please help.


0
Comment
Question by:daveleblanc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 6

Expert Comment

by:riyazthad
ID: 17940238
In your code line

strCellValue = ds2.Tables("result").Rows(x).Item(dateArray(index))

dateArray always will return date value from your array

it should either field index number or field name.

strCellValue = ds2.Tables("result").Rows(x).Item(0)

OR

strCellValue = ds2.Tables("result").Rows(x).Item("MyDate")


Thad
0
 

Author Comment

by:daveleblanc
ID: 17940961
the reason i went with the array is bc its a cross table query with the "date value" is actually the column name. the code works properly and isnt the problem

im having a problem at this line
ds2.Tables("result").Rows(x).Item(dateArray(index)) = strCellValue

where strCellValue would contain "$29,565,123"
and its trying to insert it as a double when it needs to be string

i basically need to force the dataset to accept it as a string and not double and i dont know how to accomplish that.

0
 
LVL 6

Expert Comment

by:riyazthad
ID: 17941495
Your error saying it is expecting a double value , not string. Since your value contains both comma and $ sign, it wont allow you. So you are going to lose format also.

If you are just trying to show as format , then why dont take from database as formatted.

like

select '$' + convert(varchar(10),cast(100 as money))

then you can just need to set datasource as it is.

I dont know why you are going with this approach.

Thad
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:daveleblanc
ID: 17946799
ok for the win, can someone put that convert statement into this>?
i cant get it to work.

TRANSFORM Sum(dollars_import.DollarAmt) AS SumOfDollarAmt
SELECT dollars_import.projectName AS Project, dollars_import.InvType AS [Inventory Type], Sum(dollars_import.DollarAmt) AS Total
FROM dollars_import, tblProgram
GROUP BY dollars_import.projectName, dollars_import.InvType
ORDER BY dollars_import.Date
PIVOT dollars_import.Date;


dollarAmt is the field that needs conversion
0
 
LVL 6

Accepted Solution

by:
riyazthad earned 50 total points
ID: 17947058
yes


SELECT dollars_import.projectName AS Project, dollars_import.InvType AS [Inventory Type],
'$' + convert(varchar(20),cast(sum(dollars_import.DollarAmt) as money)) Total
FROM dollars_import, tblProgram
GROUP BY dollars_import.projectName, dollars_import.InvType
ORDER BY dollars_import.Date
0
 

Author Comment

by:daveleblanc
ID: 17947883
just used a custom table style and it worked

please close this as no one provided required answer

thanks to all the replied
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ASP/VB email question 4 62
vb.net cycle through results of a sql query and capture variables 3 26
"Emulate" TAB key when press Enter Key 3 72
Visual studio 2015 1 35
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question