There's not enough information to be certain of what's going on, but if I had to guess I'd bet the Data
variable is an old-school ADO RecordSet
object. If this is true, the type of the Data("discount").Value
expression is Object
, with an actual underlying type determined based on the SQL command. Moreover, I'd bet the specific type here is Integer
.
Pop quiz: what happens in programming when you divide an Integer by another Integer?
The result is still an Integer! You get integer division, where any fractional part of the result is completely discarded. Therefore, if you have a table full of integer discount percentages, where of course no one ever has discounts that are negative or more than 100, the result of this statement will always be 0
.
To fix it, again we'd need to see more of what's going on. But if this really is VB.Net, rather than VBA or old-school VB6, I hate to say this but I'd start by rewriting all data access from scratch (really!) to get rid of these old classic ADO APIs. The older API library exists only for backwards compatibility with old code, and should not be used anymore for new development! Look for the newer ADO.Net API in the System.Data
namespace. Additionally, for this rewrite you should turn on Option Strict
, which would have forced you to deal with this mistake much earlier.
You want to end up with code more like this:
Dim sql As String = "SELECT discount, ... WHERE SomeKeyColum = @KeyField"
Using cn As New SqlConnection("connection string here"), _
cmd As New SqlCommand(sql, cn)
cmd.Parameter.Add("@KeyField", SqlDbType.NVarChar, 50).Value = "KeyValue"
cn.Open()
Using rdr As SqlDataReader = cmd.ExecuteReader()
While rdr.Read()
Dim discount As Decimal = DirectCast(rdr("discount"), Integer) / 100.0D
'...
End While
End Using
End Using
One more thing. Microsoft updated their Visual Basic programming guide in between the last VB6/vbscript/VBA release and VB.Net, to explicitly forbid hungarian type prefixes like lstr
, bln
, etc. This is not binding or mandatory, but it is the published recommendation of the vendor. In modern code today, these prefixes tend to only survive for WinForms control variables.