furyfish
furyfish

Reputation: 2135

Error with decimal

My webservice code

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
    SqlConnection con = new SqlConnection(
        "Server=NOVA-PC;database=totev006;User ID=***;Password =***;");

    public Service () { }

    [WebMethod(Description = "Update return in Bet")]
    public string setReturn(int EventID)
    {
        string sql = "";
        decimal dcm = (decimal)100.5 + (decimal)45.055;
        int BetTypeWinID = 1;
        int BettorID = 1;
        int ins = 0;

        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;

        sql = "update bet";
        sql += " set [return] = " + dcm;
        sql += " where event_id = " + EventID;
        sql += " and bet_type_id = " + BetTypeWinID;
        sql += " and bettor_id = " + BettorID;

        cmd.CommandText = sql;
        ins += cmd.ExecuteNonQuery();
        con.Close();
        if (ins > 0)
            return "Ok";
        else
            return "not Ok";
    }
}

I get exception and this is the callstack:

System.Data.SqlClient.SqlException: Incorrect syntax near '555'.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at Service.setReturn(Int32 EventID) in e:\Learning\Webservice\demo\webservice\App_Code\Service.cs:line 49

How to fix this?

Upvotes: 1

Views: 576

Answers (1)

RobIII
RobIII

Reputation: 8821

Your system is probably using a locale that formats 123.45 as 123,45.

Use parameterized queries (see SqlParameter) or be specific in the formatting.

Whatever you do, DO NOT "just" change the locale setting to "fix" this problem.

Upvotes: 3

Related Questions