Click here to Skip to main content
15,881,687 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I have a simple program with a specific culture, set to "ro-RO".

For example:

en-EN is 1,000.20
ro-RO is 1.000,20

those number ar the same

I have some math calculation with result is a DECIMAL, if my pc has set the regional to en-EN, the data is save oK in my database, if my pc has regional set to ro-RO, when i save the decimal in database is 100,020.00.

If i convert the number to Double the number is save proper.

Any suggestion what is the best solution?

Some code:

I retrive some data from database like this:

C#
DateTime[] UltimaInregistrareGr = new DateTime[CategorieProdus.Rows.Count];
                   decimal[] SoldGramaj = new decimal[CategorieProdus.Rows.Count];
                   int[] IdGramaj = new int[CategorieProdus.Rows.Count];

                   for (int i = 0; i < CategorieProdus.Rows.Count; ++i)
                   {
                       int NrCatCategorie = Convert.ToInt32(CategorieProdus.Rows[i]["id_categorie"].ToString());
                       Program.Connection.CommandText = "SELECT regGramaj.regGramaj_id, regGramaj.regGramaj_date, regGramaj.regGramaj_soldinitial, regGramaj.regGramaj_soldfinal, IIF(regGramaj.regGramaj_categorie Is Null, 0, regGramaj.regGramaj_categorie) AS RegRaportCategorie " +
                                                       "FROM regGramaj, [SELECT MAX(regGramaj.regGramaj_date)  FROM regGramaj]. AS maxdate " +
                                                       "WHERE regGramaj.regGramaj_categorie=@regGramaj_categorie";
                       Program.Connection.AddParameter("@regGramaj_categorie", NrCatCategorie);
                       DataTable TableSold = new DataTable();
                       Program.Connection.FillDataTable(TableSold, true);

                       if (TableSold.Rows.Count != 0)
                       {
                           for (int j = 0; j < TableSold.Rows.Count; ++j)
                           {
                               UltimaInregistrareGr[i] = Convert.ToDateTime(TableSold.Rows[j]["regGramaj_date"].ToString());
                               if (UltimaInregistrareGr[i] == DateTime.Today)
                                   SoldGramaj[i] = Convert.ToDecimal(TableSold.Rows[j]["regGramaj_soldinitial"].ToString());
                               else
                                   SoldGramaj[i] = Convert.ToDecimal(TableSold.Rows[j]["regGramaj_soldfinal"].ToString());
                               IdGramaj[i] = Convert.ToInt32(TableSold.Rows[j]["regGramaj_id"].ToString());
                           }
                       }
                       else
                       {
                           SoldGramaj[i] = 0;
                           IdGramaj[i] = 0;
                       }
                   }




and after sone math calculation i save the result like this:

C#
Program.Connection.CommandText = "INSERT INTO regGramaj (regGramaj_date, regGramaj_soldinitial, regGramaj_intrari, regGramaj_vanzare, regGramaj_soldfinal, regGramaj_categorie) values (@regGramaj_date, @regGramaj_soldinitial, @regGramaj_intrari, @regGramaj_vanzare, @regGramaj_soldfinal, @regGramaj_categorie)";
                                Program.Connection.AddParameter("@regGramaj_date", DateTime.Today);
                                Program.Connection.AddParameter("@regGramaj_soldinitial", SoldGramaj[i]);
                                Program.Connection.AddParameter("@regGramaj_intrari", IntrariGramaj[i]);
                                Program.Connection.AddParameter("@regGramaj_vanzare", IesireGramaj[i]);
                                Program.Connection.AddParameter("@regGramaj_soldfinal", GramajFinal);
                                Program.Connection.AddParameter("@regGramaj_categorie", CatProdusNr[i]);
                                Program.Connection.ExecuteNonQuery();
                                Program.Connection.CommitTranzaction();
Posted
Updated 29-Oct-14 6:22am
v2
Comments
Maciej Los 28-Oct-14 4:56am    
Show us the way you do that (post sample code).
aciobanita constantin 29-Oct-14 12:22pm    
i have add some code to my question.
Richard Deeming 29-Oct-14 13:11pm    
Where did the Program.Connection class come from? That doesn't look like any of the built-in ADO.NET objects.
aciobanita constantin 29-Oct-14 13:39pm    
no, i have created that class for retrive, update and save information in database.
Richard Deeming 29-Oct-14 13:41pm    
So where's the code for that class? We can't diagnose a problem with your code if you don't post it. :)

Use a parameterized query instead of a plain string query - it will handle those formatting quirks automatically (and also save you from SQL injection attacks).
 
Share this answer
 
Comments
Maciej Los 28-Oct-14 4:56am    
Bernard, it's access database with its limitations. How parameterized queries can help in this case? If input value will be in wrong format, the output (result set) have to be wrong.
1.There are two properties that are managing the current culture used in any .NET application:
i)Thread.CurrentThread.CurrentCulture - determine the default format for dates, times, numbers, currency values, the sorting order of text, casing conventions, and string comparisons.
ii)Thread.CurrentThread.CurrentUICulture - similar with the first one, but used in User Interface for displaying and getting the user inputs into a specific culture;

2.So you should use Thread.CurrentThread.CurrentCulture and set its value in your application to have the same value like the culture used by your database server. Better is to use an associated setting into your application config with default value "en-US".

3.You may find some ideas and details about this topic in my next article: MVC Basic Site: Step 1 – Multilingual Site Skeleton[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900