SQL Insert syntax when float decimal is represented with a comma

Posted on 2009-02-20
Last Modified: 2012-08-14
Hello all, I need a little help.

I have written a C# application that receives data from a connected device.  The connected device will pass over a floating point number ie 1.8897.

I have no problems storing the data if the locale is US - English, but when I change the locale to something where a decimal is represented by a comma my insert query is failing.  

This query works if locale is US:

float tempfloat = 1.888; // Say this value was received from a device that represents decimals with periods

"INSERT INTO TABLE (PrimaryKey, isaFloatValue) VALUES(1, " + tempfloat + ")"

When the local is non US the query is transformed to look like this:
"INSERT INTO TABLE (PrimaryKey, isaFloatValue) VALUES(1, 1,888)" and generates the error below.

Basically the sql statement is failing with the following error "Number of query values and destination fields are not the same.  Microsoft JET Database Engine"

Any help or suggestions are greatly appreciated!
Question by:shawnpbrady
    LVL 39

    Accepted Solution

    Use parameterized SQL

    "INSERT INTO TABLE (PrimaryKey, isaFloatValue) VALUES(1,@FloatValue)"

    Then add @FloatValue as a parameter to the db command.

    Author Comment


    Never had to do that beautifully!

    Thanks for you help!

    Here is an article with syntax for anyone encountering this problem!

    Using parameterized SQL queries


    Author Closing Comment

    Thanks again!
    LVL 39

    Expert Comment

    You should ALWAYS use parameterized SQL.  It saves you from having to escape quotes and deal with stuff like this.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now