Or you may get away passing integers only ...
/gustav
Main Topics
Browse All TopicsHi,
I have problems with inserting correct values into my database. I have written som code that gets a value from an input-field. When saving the value into a variable (double or decimal) the value are saved like 10.3. Because I live in Denmark where we use comma (10,3) instead of dot (10.3) it gives me some problems when i try to insert the data into my Access-db. When it get values with dot instead of comma, it just removes the dot so the result in the db is 103 instead of 10,3.
I use parameters, but it doesn't help me.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
it-row, its all about regional settings.
double myValue = Convert.ToDouble(textBox1.
If RegionalSettings are set to English(UnitedStates), and user types 123.45 myValue will be 123.45.
If RegionalSettings are set to Danish, and user types 123,45 myValue will be 123,45.
So, in both cases result will be correct, its up to application user to select proper regional settings.
Goran
The result will be 3 and 4/10, which is a correct result. I dont understand what do you mean by: "The result in .net is always saved with dot instead of a comma". The result is not "saved in .NET", its saved in MS Access database, and it has nothing to do with .NET. MS Access will interpret decimal sign based on the regional settings. Example, set the regional settings in English (United States). OPen database MS Access, open this table that contains decimal valkues, enter 3.4 . Value will be saved as decimal number. Close MS Access database, Change REgionalSettings to Danish, reopen MS Accesss, go to rhe table you were before. Now you can see that record you have endeterd just before is displayed as 3,4 which is again a decimal number. If you now try to enter decimal number, you will need to enter it with comma.
I never had any problem with above approach (converting to decimal and save it to database using parameters). IF I dont understand your problem, please be more precise.
In the above example I have used MS Access database (and left out .NET from scenario), so you can clearly see what is going one. When you see that it behaves correctly, you can then do the same with .NET and parameterized query. Just remember that when you change regional settings, its important to close and reopen application, since it needs to pick up new settings.
> its up to application user to select proper regional settings.
It just has to build a correct SQL string. This involves converting decimal values to string expressions where dot is the decimal separator.
> its up to application user to select proper regional settings.
No, it uses the regional settings if no other locale is specified which is the typical case. Thus, as in this case, when comma is the decimal separator, a decimal value typed by the user is expected to have a comma as the decimal separator.
> MS Access will interpret decimal sign based on the regional settings
Nothing specific to Access here. It is an SQL string that is created and this must contain a dot as the decimal separator. If a numeric is passed as a parameter, it will be casted to a string and it is done according to the locale in use which - here - is read from the regional settings.
All this mess can be avoided by using DataTableAdapters avoiding all the low level SQL stuff, but that is another discussion.
Two secure solutions exist. One is the work-around described previously (passing integers only), the other is to convert a decimal to a string expression in SQL by using the native function Str which always converts to a decimal expression with a dot.
/gustav
this smells like a debate to me. :)
> t just has to build a correct SQL string. This involves converting decimal values to string expressions where dot is the decimal separator.
No, it doesnt. MS Access will accept comma as decimal sign IF comma is the decimal sign set in regional settings.
> Nothing specific to Access here. It is an SQL string that is created and this must contain a dot as the decimal separator.
This is again incorrent, sql can contain comma as decimal separator. Try it yourself. Set comma as decimal sign in regional settings, and crate a quick parametrized query that qill insert some double value in MS Access. It will work correclty with comma as decimal, and incorrectly if dot is used as decimal separator. Then open database in MS Access, see the inserted value, and see how it is displayed (with comma). The main
> All this mess can be avoided by using DataTableAdapters avoiding all the low level SQL stuff, but that is another discussion.
Please explain what does table adapter do so special, that command object with parameterized query cant do (which is what it-row is using in code)? They all do the same thing, table adapter uses internally command object to do CRUD operations.
The information I have provided here is based on the fact that my programs is working flawlessly for years, without any problems with decimal sign, and most of my users use comma as decimal sign.
Interpreting numbers as strings is a very bad idea, and leads to many problems in the future, when the application starts growing, so it is the last option to consider.
What I believe it_row wants here, when user presses dot key on numeric keypad, is to convert this sign to comma, Is this correct?
> MS Access will accept comma as decimal sign IF comma is the decimal sign set in regional settings.
Not when passed as an SQL string as here.
SQL strings like this example will fail:
INSERT INTO TableName (field1, field2, field3, field4) VALUES (2, 0,75, 5, 11)
while these will succeed:
INSERT INTO TableName (field1, field2, field3, field4) VALUES (2, 0.75, 5, 11)
INSERT INTO TableName (field1, field2, field3, field4) VALUES (2, 75/100, 5, 11)
However, I found out that my previous other suggestion:
INSERT INTO TableName (field1, field2, field3, field4) VALUES (?, STR(?), ?, ?)
doesn't work because the casting to a string takes place before STR can do anything.
If you don't like the method for using integers only, the parameter must be passed as a string as shown below. The OleDb parser seems to do the casting correctly using the code below even though the string in the locale in question will use comma as the decimal separator. This can be verified by appending the line:
Console.WriteLine(cmd.Para
This I would recommend as the solution because the code shows what is taken place.
The a2.ToString() could be reduced to just a2 if you prefer.
As for the DataTableAdapters, the use of these or even other strongly typed higher level code like the ADO.NET Entity Model framework is highly recommended as they automatically take care for cases like this. Just google for it to find numerous tutorials - far too much to go into here.
/gustav
> As for the DataTableAdapters, the use of these or even other strongly typed higher level code like the ADO.NET Entity Model framework is highly recommended as they automatically take care for cases like this. Just google for it to find numerous tutorials - far too much to go into here.
:) I still would like to hear from you what 'magic' do they do? I know what they do, and I have told you what they use internally, and nothing magic and unknown to be seen there. But, since you say that table adapters do better job than using command object with parameters, I would like to hear some facts about it. I personally don't use Data Tables for CRUID, and therefore do not use table Adapters, and I don't use Entity Framework (since its pretty new and green), but I use my own DAL, and never had a problem with comma as decimal sign being saved in database.
> Not when passed as an SQL string as here.
SQL strings like this example will fail:
If you have noticed, we are from the beginning talking about PARAMETERIZED queries, and what you post here as an example, and use as a starting base for your arguments, is a NON-PARAMETERIZED query. Big difference between the two, and if you have read numerous tutorials, you would find that parameterized query is preferred method for CRUD operations, and one of the easons for this is saving decimal numbers without any regard to their format (like we now have the case here).
1.
We all have our preferences, and if you have a home-built DAL that of course fits your purpose. I don't have other purposes than what fits perfectly to what the DataTableAdapters are intended for so I didn't see any reason to reinvent the wheel, indeed as I prefer strongly typed code too (some don't and that's fine with me).
There is nothing magic about DataTableAdapters, they are just very well thought out; and so is the ADO.NET Entity Model framework - it is still not perfect, that's true, but improving.
I think the question here demonstrates better than many words why strongly typed code is preferable. I would find it very frustrating if I had to deal with converting very common data types as decimal and currency to string or integer to be sure my code would work under differenct locales.
You are right about the parameterized queries. I used the SQL strings to illustrate the issue even though I can see that the source of the issue is not the SQL built but how the parameter values are interpreted.
2.
Did some further testing as I recall the JET engine being buggy when handling decimal data type.
A (third and better) solution seems to switch to data type Double or (if count of decimals is four or less) Currency.
/gustav
Business Accounts
Answer for Membership
by: ajitha75Posted on 2009-09-18 at 11:47:22ID: 25368752
Convert the values to en-US culture and then save it to the database. While retrieving it from the database, use the CurrentThread's culture to format the values to local culture.
Thanks
Ajitha