Monday, February 20, 2012

Money format error

I use asp.net 2.0 and sql server 2005 for a web site (and Microsoft enterprise library).When I run aplication at local there is no problem but at the server I take this error:

Disallowed implicit conversion from data type varchar to data type money, table 'dbname.dbo.shopProducts', column 'productPrice'. Use the CONVERT function to run this query.

productPrice coloumn format is money. It was working correctly my old server but now it crashed.How can I solve this problem? And why it isn't work same configuration?

My code:

decimal productPrice;

Database db =DatabaseFactory.CreateDatabase("connection");

string sqlCommand ="update shopProducts set .................,productPrice='" + productPrice.ToString().Replace(",",".") +"',................where productID=" + productID +" ";

db.ExecuteNonQuery(CommandType.Text, sqlCommand)

Decimal should be able to covert to SqlMoney implicitly, why you convert the price to string before update it to the table? However, it's better to useParameterized Queries as possible as you can.

No comments:

Post a Comment