473,387 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

DataTable.Select - Thread.CurrentThread.CurrentCulture - String.Format

Hi NG,

in my application a <myDataTable>.Select fails with "Syntax error in the
expression."
It took me a while, isolate the cause to this:

To select some datarows from a data table I am using a select sting like
"selectStr = String.Format("x >= {0}", aDoubleVar)".
The Thread.CurrentThread.CurrenCulture is set to "en-US" where the
Thread.CurrentThread.CurrentCulture.NumberFormat.C urrencyDecimalSeparator is
a '.' as expected. BUT.... debugging my selectStr it turned out to be "x>=
2,14" !

After a while I had the Idea to check ControlPanel->Regional and
LanguageOptions and YES, the user has changed the decimal seperator.

So I currently looks like
- String.Format uses the ControlPanel Settings ","
- The DataTable uses Thread.CurrentThread.CurrentCulture "."

Is this a BUG in the Framework, or in my HEAD? Is there something I missed?
Any help or hint on this subject is very appreciated.

Regards
Rainer Queck
Jan 5 '08 #1
4 3584
I don't know much about DataTable(I've always steered fairly clear of
DataSet), but if this was a regular SqlCommand (etc) I would be adding
a parameter with .Value = aDoubleVar - that way, you don't need to
worry about formatting, plus it is injection safe (think "Bobby
Tables"...).

However, if you /must/ use string concatenation, then you should
ensure that the format is in the known, fixed, expected format of
whatever you are talking to. It is *entirely reasonable* for the user
to use their local (or /locale/) formats and separators, and your code
shouldn't rely on this. This should be used for displaying data to the
user, not exchanging data between systems. You could try passing
CultureInfo.InvariantCulture as the first argument to string.Format
(although you might need a more specific format...).

But I stress; the most reliable option is using parameters.

This is a /similar/ (but unrelated) problem to using the default
encoding / endianness when exchanging text as binary; when talking
between systems it is best to agree in advance what formats to use.

Marc
Jan 5 '08 #2
Hi Marc,
>You could try passing
CultureInfo.InvariantCulture as the first argument to string.Format
(although you might need a more specific format...).
What kind of a Culture is a DataTable using for example on a select? Is it
possible, that it usese the "InvariantCulture" as well?
If that is the case, I could get rid of my problem by following your
suggestion. If not.... :-(

Regards
Rainer

Jan 6 '08 #3
I wouldn't set the culture the invariant - I'd (as indicated) *use*
the invariant culture when I am doing system-to-system calls - i.e.
string.Format(CultureInfo.InvariantCulture, "foo", bar).

Note that there can be issues messing with culture while forms are
displayed; they don't like that much... a simpler option might be to
save the new culture in a settings file, and restart the app? (I
believe that Application has a Restart method or similar).

Also; re supplying proper parameters - I don't really know (not having
done it myself), but is this not the GetFillParameters method on the
data-adapter?

Marc
Jan 6 '08 #4
Hi Mark,

"Marc Gravell" <ma**********@gmail.comschrieb im Newsbeitrag
news:3b**********************************@k39g2000 hsf.googlegroups.com...
>I wouldn't set the culture the invariant - I'd (as indicated) *use*
the invariant culture when I am doing system-to-system calls - i.e.
string.Format(CultureInfo.InvariantCulture, "foo", bar).
Yes, that is what I am currently doing (ref my other answer/question).
Note that there can be issues messing with culture while forms are
displayed; they don't like that much... a simpler option might be to
save the new culture in a settings file, and restart the app? (I
believe that Application has a Restart method or similar).
It surely would be better, to set the culture and the stop/restart the
application. Unfortunately it is a request from my customer, that the app
must be able to switch rultures at runtime. Acutally this is working quite
good. I used the method pointed out in
http://dzaebel.net/LocalizeRuntime.htm by Frank Dzaeble.
Also; re supplying proper parameters - I don't really know (not having
done it myself), but is this not the GetFillParameters method on the
data-adapter?
You are right, but I dont have a data-adapter, since I am working with a
"standalone" typed DataSet.

As mentioned above, I am already following your suggestion, and am using
CultureInfo.InvariantCulture to do my String.Format on selectStr generation.
It seems to be working fine, which leads me to the thought, that DataSets
are using CultureInfo.InvariantCulture internaly as well.
If someone could state this true, I can replace "seems to" with "is" ;-)

Regards
Rainer
Jan 6 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Rahul Agarwal | last post by:
Hi I am trying to write data back to the client using dataset.WriteXML and I have set the Thread.CurrentThread.CurrentCulture to "no" (norwegian) just before writing the data using WriteXML....
6
by: Willie wjb | last post by:
Hi, i have a client program that sends a filter expression to the server PC. On that server PC this filter is put over a datatable and the result is send back. the server can be located on a...
4
by: Leonardo Hyppolito | last post by:
Hello, I am trying to write a multithread program that simulates producers and consumers. My program can have many producers and many consumers (each in a separate thread). It has a storage...
6
by: Peter Rilling | last post by:
Okay, I have the main thread which does all the work. This main thread spawns a worker thread that just periodically poles the environment looking for a certain condition. This second thread is...
1
by: benmorganpowell | last post by:
I have a small windows service which connects to a POP3 server at defined intervals, scans the available messages, extracts the required information and inserts the data into a SQL database. I am...
1
by: Manu | last post by:
I have created a new culture : Dim objCulture As New CultureInfo("he") //hebrew When I tried to assign it to the current thread. Thread.CurrentThread.CurrentCulture = objCulture...
13
by: Bob Day | last post by:
Using vs2003, vb.net I start a thread, giving it a name before start. Code snippet: 'give each thread a unique name (for later identification) Trunk_Thread.Name = "Trunk_0_Thread" ' allow...
0
by: hazz | last post by:
I have introduced the following code into a business object to convert SQL Server datetime into a date format required for my client application....
5
by: Tsair | last post by:
I set the thread culture in MAIN() as below in order to show the date in format DAY/MONTH/YEAR, but the datagridview alway show the date in M/d/yyyy. How to set the default Date format from...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.