Search code examples
c#datatabledatacolumn

DataColumn in DataTable not found by name


I'm reading from a CSV file where the first line is the header row. All column names are read and put into a table like this:

foreach (var item in lines[0].Split(';').Where(s => !string.IsNullOrEmpty(s)))
        {
            table.Columns.Add(item.Trim(), typeof(string));
        }

Everything seams correct, except when I try to read from the table again. Most of the columns are read, except one.

I have tried to debug and write a message for this column, but it does NOT enter into that function (see image below). (The if statement return false)

enter image description here

I have tried about everything now, including checking the Locale on the table.

Header line:

document title;DocRef;DocRevNo;DocRevDt;RevisionObject;TransRef;Trans Status;DocOrigin;DocRefClient;RespActualDt;First TransDate;Last Update

EDIT:

I have found out that first byte is a strange one...

[0]: 65279 ''
[1]: 100 'd'
[2]: 111 'o'
[3]: 99 'c'
[4]: 117 'u'
[5]: 109 'm'
[6]: 101 'e'
[7]: 110 'n'
[8]: 116 't'
[9]: 32 ' '
[10]: 116 't'
[11]: 105 'i'
[12]: 116 't'
[13]: 108 'l'
[14]: 101 'e'

Anyone that knows how to remove that byte (If it exist)?


Solution

  • Here is the solution. The start of the line contained a hidden charakter (65279). By running the text through this function, it removed the problem:

        protected string CleanInput(string input)
        {
            var imp = (input ?? "");
            var removeChars = new Char[] { (Char)65279 };
            imp = new String(imp.ToCharArray().Where(c => !removeChars.Contains(c)).ToArray());
            return imp;
        }