1

I am using the below function to import a csv file into a datatable object csvData, however i face an oject reference issue that i'd like to understand more while using Regex.Replace to remove quote marks from the data:

private static DataTable Gettabledata(string cpath)
    {
        DataTable csvData = new DataTable();

        try
        {
            using (TextFieldParser csvReader = new TextFieldParser(cpath))
            {
                csvReader.SetDelimiters(new string[] { "," });
                csvReader.HasFieldsEnclosedInQuotes = true;
                string[] colFields = csvReader.ReadFields();
                foreach (string column in colFields)
                {
                    DataColumn datecolumn = new DataColumn(column);
                    datecolumn.AllowDBNull = true;
                    csvData.Columns.Add(datecolumn);
                }
                while (!csvReader.EndOfData)
                {

                    string[] fieldData = csvReader.ReadFields();
                    string pattern="\""; % remove quotation mark "
                    string replacement=""; % replace by empty.(eg "a", a) 
                    Regex rgx = new Regex(pattern);
                    for (int i = 0; i < fieldData.Length; i++)
                        fieldData[i] = Regex.Replace(fieldData[i],pattern, replacement); %object reference issue

                    {
                        if (fieldData[i] == "")
                        {
                            fieldData[i] = null;
                        }
                    }
                    csvData.Rows.Add(fieldData);
                }
            }
        }
        catch (Exception ex)
        {
        }
        return csvData;
    }
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
dark.vador
  • 619
  • 1
  • 6
  • 25
  • What is the contents in `fieldData`? – Wiktor Stribiżew Apr 12 '16 at 10:25
  • @WiktorStribiżew: missing `pattern` has been added to the post. the matter `index` `[i]` used in loop is no longer recognized for some reasons... – dark.vador Apr 12 '16 at 10:29
  • `fieldData` contains several data of several types (string, float, int...) – dark.vador Apr 12 '16 at 10:31
  • sorry my mistake, `fieldData` contains 645 data of `string type` using this `function` – dark.vador Apr 12 '16 at 10:35
  • Well, no idea what is going on but removing quotes is as easy as `fieldData[i] = fieldData[i].Replace("\"", "")`. BTW, look, `fieldData[i] = Regex.Replace(fieldData[i],pattern, replacement);` is outside the `{...}` block. I think it should be inside it. I think the exception is thrown at `if (fieldData[i] == "")` line. – Wiktor Stribiżew Apr 12 '16 at 10:36
  • thanks. `fieldData[i]` was indeed out of scope. – dark.vador Apr 12 '16 at 10:42

1 Answers1

0

The fieldData[i] = Regex.Replace(fieldData[i],pattern, replacement); is outside the {...} block. It should be inside it.

for (int i = 0; i < fieldData.Length; i++)
{
    if (fieldData[i] == "")
    {
         fieldData[i] = null;
    }
    else 
    {
         fieldData[i] = Regex.Replace(fieldData[i],pattern, replacement); 
    }
}
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Glad it worked for you. Please also consider upvoting if my answer proved helpful to you (see [How to upvote on Stack Overflow?](http://meta.stackexchange.com/questions/173399/how-to-upvote-on-stack-overflow)). – Wiktor Stribiżew Apr 13 '16 at 06:55