Solved

DataGridItemEventArgs dataitem field is decimal - it's rounding automatically, how to stop?

Posted on 2006-06-23
14
480 Views
Last Modified: 2012-08-14
I've got a SQL query that returns a dataset which includes several decimal fields (precision 3, scale 2). In the itemdatabound sub, I grab one of these decimal fields e.item(i).datafield("fieldname") whose actual value is 0.99. Debug watch shows the e.item.datafield("fieldname") value as 1D.  If I trace the value down using e.cells(i).text, the value correctly shows as "0.99". Anyone know why the datafield reference is rounding this value and how to stop it?

Query is very basic: select * from tablename....

tia,
Sue
0
Comment
Question by:slwojd
  • 8
  • 5
14 Comments
 
LVL 4

Accepted Solution

by:
gregg1ep00 earned 150 total points
ID: 16971958
I think that internally this is a global problem (not specific to the datafield, c#, or even .NET).  It's the way decimals and floating point numbers in general are handled in memory.  If you have a floating-point number in memory, its value is not guaranteed to be 100% accurate.

I'm not sure if changing the Sql syntax would help things (i.e., change the fields to precision 4, scale 3), but it would be worth a try, and definitely interesting to see what results you get.

If you use the value in a calculation, does it still show up as 1D?  In other words, is it just the Debug watch window that's doing the rounding, or is it the value itself?
0
 

Author Comment

by:slwojd
ID: 16972134
Fortunately, this particular field isn't part of any comparison or math function, but it does need to display correctly.  

I've tried changing the precision and scale previous to this, which correctly displays the number of digits, decimal place, etc, but still doesn't still doesn't bring up the correct value. e.g.: precision 4, scale 3 shows 1.000 (wrong) or 0.990 (right).

Added a label to test the calculation part (good idea!), and added 0.25 to the dataitem value (1.25D) and cell value after casting back to a decimal (1.24D).  

I'm sure this must be part of some UFO conspiracy, but that sure doesn't help me with this <g>...

Sue
0
 
LVL 4

Expert Comment

by:gregg1ep00
ID: 16972358
Yeah, these kind of issues are definitely the most annoying!  There is no reason that this shouldn't just work!  ;)

Can you run the SQL query against the database directly (using SQL query analyzer or the osql utility) and see what the database is returning?  That way we can at least narrow down the problem to the database or .NET.
0
 
LVL 4

Expert Comment

by:gregg1ep00
ID: 16972387
Another question...

If you do this in your ItemDataBound method:

Dim temp As Decimal = CType( e.Item(i).DataField("fieldname"), Decimal )


What is the value of temp?
0
 

Author Comment

by:slwojd
ID: 16972743
Identical query in the Query Analyzer gives the correct values, so it has to be something in the DataAdapter or how it's being passed as a DataGridItemEventArgs that's causing the problem. Which isn't a good thing if one is expecting consistent accurate data.

No change in results using the CType. I've resorted to a messy fix which I don't like doing without understanding why things are doing what they're doing. It always comes back to bite one in the arse:

If Not IsDBNull(e.Item.DataItem("RightValue")) Then
       e.Item.Cells(i).Text = CType(e.Item.DataItem("RightValue"), Decimal) / 100
End If

Since I suspect you're right with the idea that this is something to do with a system internal "feature", I'll be happy to grant you 150 points? If I have time, I'll keep poking around to see if I can narrow the source of the problem down further.

Sue, the still perplexed but gotta get this code out to the users this afternoon
0
 
LVL 4

Expert Comment

by:gregg1ep00
ID: 16972965
I'll leave the points up to you.  I'll continue to help you with this if you'd like, regardless of what you choose.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 4

Expert Comment

by:gregg1ep00
ID: 16972978
How are you doing your databinding?  As in, are you binding to a DataSet or a DataView?  Also, which version of the .NET runtime are you using?  If you're using .NET 2.0, are you binding to a SqlDataSource?
0
 
LVL 4

Expert Comment

by:gregg1ep00
ID: 16973047
I know that VB sometimes does a lot of type-casting for you.  Maybe if that's what's going on, then we can specifically cast everything and cut VB's casting out of the loop.

Try this inside your ItemDataBound handler:

Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView)
Dim value As Decimal = CType(drv("fieldname"), Decimal)

Or, if you're binding straight to a DataSet or DataTable:
Dim dr As DataRow = CType(e.Item.DataItem, DataRow)
Dim value As Decimal = CType(dr("fieldname"), Decimal)
0
 

Author Comment

by:slwojd
ID: 16973529
Binding to a datareader being fed from a stored procedure (SQLServer 2000). Still using 1.2 - will be upgrading if I can ever find the time.

I've tried various versions of grabbing the data using datarows and DataGridItemEventArgs with no success while poking around trying to figure out what was going on. I'll see if I can shake some time loose on Monday to do more testing...  

Thanks!
0
 
LVL 4

Expert Comment

by:gregg1ep00
ID: 16973707
If you're binding to a DataReader, then you'll need to do your cast like this:

Dim dr As System.Data.Common.DbDataRecord = CType(e.Item.DataItem, System.Data.Common.DbDataRecord)
Dim value As Decimal = dr.GetDecimal( dr.GetOrdinal( "fieldname" ) )


Greg
0
 

Author Comment

by:slwojd
ID: 17018644
Hi Greg - sorry it's taken so long to get back to this. I've tried various combos of grabbing the data, both from vb and from Query Analyzer without different results. Posted a question to MS's newsgroups without much of a response there either.

I'm on vacation next week and will be buried when I get back with finishing up a project, and after that I'm migrating to VB/ASP 2005 and SQL2005. Hoping that will fix the problem (or not <g>).  Don't want to leave this problem hanging so I'm going to close it (for now).  Have a happy 4th of July!

Sue
0
 

Author Comment

by:slwojd
ID: 17074448
Without more time to delve deeper into tracking down precisely what the cause of this problem, I'm going to chalk it up to internal system handling of floating point numbers.  Thanks for the ideas and suggestions, Greg!

Sue
0
 
LVL 4

Expert Comment

by:gregg1ep00
ID: 17075441
No problem, Sue.  Sorry we couldn't get it nailed down any deeper than that.  Good luck!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now