I'm using c# 2.0.
I have a datareader that reads an invoice line item from a table in my SQL Server database. The UnitPrice field is a Money field in SQL server. At the same time I have an invoice class in my application that has a UnitPrice property which is a float.
How do I convert the SQLMoney to a float using my datareader?
Right now I have:
cm.CommandText =
"SELECT ItemID, Quantity, UnitPrice, Discount FROM tblInvoiceLineItems";using (SqlDataReader dr = cm.ExecuteReader()){
while (dr.Read()){
LineItem li =newLineItem();li.UnitPrice = (
float)(double)dr.GetFloat(3); // cast error here.lineItems.Add(li);
}
}
It will be easier to use DECIMAL in the database so your employer will not loose money and believe me that is a high probability with FLOAT. This makes it easy to use Visual Basic financial class to calculate your price in the application then use standard convert to convert your value to Decimal which is the same as money in SQL Server. Why because you can only set precision and scale with DECIMAL and NUMERIC in SQL Server. Just a thought the link below covers the FCL(framework class library) types, ADO.NET types and SQL Server types. Hope this helps.
http://msdn2.microsoft.com/en-us/library/ms131092.aspx
No comments:
Post a Comment