I am getting an SQL error when I try to do this:
public static int GetOrderId(decimal totalprice, int userid)
{
string s = "SELECT * from orders where OrderUserId = " + userid + " and OrderTotalPrice = " + totalprice;
cmd = new SqlCommand(s, con);
int temporderid = Convert.ToInt32(cmd.ExecuteScalar());
return temporderid;
}
As far I can see its because it gets OrderTotalPrice back, the format is incompatible. But I cant figure out how to get it in the compatible format.
It’s probably formatting
totalpricewith a comma, like3,14, where SQL Server expects a dot, like3.14.One way to fix that would be to specify InvariantCulture, which uses a dot:
This formats the price as
3.1on any machine.By the way, it’s much nicer to pass the variables as parameters:
Then you don’t have to worry about formats because you send the database a double, not a double formatted as a string.