I'm trying to validate that a decimal in c# will fit into a db column decimal. The SqlDecimal object allows you to pass in a precision and scale as well as the decimal bits to the constructor. I know the size of the column and so before we write the data I check each input so I can produce business required output.
In this case we are storing a percent, so the precision is 13 and the scale is 10. I have a testing harness that I've condensed below into a unti test for SO. This sample is throwing an Arithmetic Overflow error on the SqlDecimal constructor line:
[TestMethod]
public void TestDecimalFits()
{
decimal d = 10.3m;
SqlDecimal sqlDecimal = new SqlDecimal(13, 10, d >= 0, Decimal.GetBits(d));
Assert.AreEqual(d, sqlDecimal.Value);
}
Does anyone know why this blows up?
Thanks!
SqlDecimalconstructor with the decimal type directly. Like thisSqlDecimal sqlDecimal = new SqlDecimal(d);.