2

Basically I have a C# Desktop application that uploads data from txt/csv files into a database. I do something like:

OdbcConnection conn;
conn = new OdbcConnection(strConnString.Trim());
conn.Open();

sql_select = "select * from [" + this.csvFilePath.Trim() + "]";

The idea is to let the user decide what the delimiter will be, like ";" or "," or "#", or whatever..

So when the delimiter is anything besisdes ",", the application runs fine...but when I choose to use it, I get an error telling me that the field delimiter coincides with the decimal delimiter.

The thing is that in my location the delimiter for decimal is ",". I found out that changing the decimal delimiter on the Control Panel for "." instead of "," gets the application to work.

My question is, is there any way make the application set the default delimiter?

Carlos Yasuda
  • 121
  • 12

4 Answers4

0

Check this question out, it might lead you to your solution:

How to fix an application that has a problem with decimal separator

The question proper might be TL;DR, but the first answer has to do with your problem, and its solution. You'll basically use the CultureInfo class instance of your application's threads to make the app use a given culture. That way you can change the decimal separator you're using without having to change the separator used by the OS.

As far as I remember you can provide your own custom culture, so you can set the decimal separator to whatever you like.

Community
  • 1
  • 1
Geeky Guy
  • 9,229
  • 4
  • 42
  • 62
0

While you may be able to use an API call to set the default delimieter of the operating system, that would likely annoy users. A better option would be to run a find-and-replace on the delimeter in the file string, changing whatever delimiter the user inputs into some custom delimiter (ASCII character 30 was designed for this purpose). Then always parse the data into your file using that custom delimieter. Of course, don't save your changes to the file afterward.

This page contains a list of all characters defined by the ASCII standard.

IanPudney
  • 5,941
  • 1
  • 24
  • 39
0
var yourCulture = new CultureInfo("EN-US");
yourCulture.NumberFormat.NumberDecimalSeparator = ";"; // an arbitrary separator
Thread.CurrentThread.CurrentCulture = yourCulture;
Console.WriteLine(1.5);                          // -> 1;5
Console.WriteLine(Convert.ToDouble("1;5") + 1);  // -> 2;5
Zsolt
  • 3,263
  • 3
  • 33
  • 48
0

Neither of those solutions worked for me...even though I could change the CurrentCulture, I still got the error "field delimiter = decimal delimiter".

But because I'm stupid, I forgot to set the schema.ini properly. Here's what I got:

    private void writeSchema()
    {
        try
        {
            FileStream fsOutput = new FileStream(this.dirCSV + "\\schema.ini", FileMode.Create, FileAccess.Write);
            StreamWriter srOutput = new StreamWriter(fsOutput);
            string s1, s2, s3, s4, s5, s6, s7, s8;

            s1 = "[" + this.FileNevCSV + "]";
            s2 = "ColNameHeader=" + chkFirstRowColumnNames.Checked.ToString();
            s3 = "Format=" + this.strFormat;
            s4 = "MaxScanRows=25";
            s5 = "CharacterSet=" + this.strEncoding;
            s6 = "DecimalSymbol=.";
            s7 = "CurrencyDecimalSymbol=.";

            srOutput.WriteLine(s1.ToString() + "\r\n" + s2.ToString() + "\r\n" + s3.ToString() + "\r\n" + s4.ToString() + "\r\n" + s5.ToString() + "\r\n" + s6.ToString() + "\r\n" + s7.ToString());
            srOutput.Close();
            fsOutput.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "writeSchema");
        }
        finally
        { }
    }

That got the damn thing to work just fine. Thanks for all help!

Carlos Yasuda
  • 121
  • 12