0

Please help with this matter. I did not find a solution.

I built a WebForm that allows entering some data (from multiple controls, one TextBox is to contain a number that means a quantity). The WebForm is in ASP.NET 4.0 (with C#).

The WebForm passes all the values from the Controls into a database hosted on a SQL Server 2014 version. Here comes the issue: the web application should run in a German Language environment. (my machine and the others that would access the application).

My issue is that even with the Regional Settings set to German, I still must enter the decimal values with "." decimal points, rather that with "," commas in the Textbox for quantity.

If I enter the decimal values with commas, they get glued in a single integer, rather than going to decimals. e.g. 11123,34 is recorded in the table as 1112324 1123.45 is recorded in the table as 1123,45

Note: I set the field in the database, where this TextBox addresses to "Money" type in the SQL table definition.

When accessing the contents of the table (as written) via a GridView control in another webform, I get all the values with "," commas , irrespective if the Regional Settings are set to German or English.

The format of the numeric values stored for Quantity changes its presentation from "," commas to "." points and viceversa only when running SELECTs on the table directly on the SQL Server (I mean in the SQL Server Management Studio), or when I "SET LANGUAGE TO GERMAN/BRITISH" on the SQL Server.

My question is:

  • What should I do that numeric values to be entered the same with "," in the textbox and be processed likewise with "," decimal point in the SQL table.

I do not want users to use the decimal separator of "." when entering the quantity, and use the decimal separator "," when reading the quantity out of the database.

Thank you very much for your help.

Broots Waymb
  • 4,713
  • 3
  • 28
  • 51
George
  • 653
  • 6
  • 18

1 Answers1

0

Proper solution: every operation that converts values to text and back must specify culture appropriately.

   // formatting/parsing 
   var userCuluture = new CultureInfo("de-de");
   textBox.Text = String.Format(userCuluture, "{0} for {1}", DateTime.Now, 1.99);
   var value = float.Parse(textBoxAmount.Text, userCulture);

   dbProvider.WriteValue(value); // use InvariantCulture inside for formating

Poor man version: change thread's culture so default culture is used for conversions and adjust it so operations on user's data done when user's culture set to be current and DB operations done when InvariantCulture:

   // Manipulation of user's data to use "userCulutre" rules
   Thread.CurrentThread.CurrentCulture = userCulture;
   textBox.Text = String.Format("{0} for {1}", DateTime.Now, 1.99);
   var value = float.Parse(textBoxAmount.Text);

   // DB/file manipulations to use Invariant rules
   Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
   dbProvider.WriteValue(value); // use current culture for formatting/parsing

There are many ways to manage current culture which you can find by https://www.bing.com/search?q=asp.net+culture, like Set Culture in an ASP.Net MVC app

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179