469,903 Members | 2,287 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

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 3452
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Leonardo Hyppolito | last post: by
13 posts views Thread by Bob Day | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.