0

I'm doing import and export using LINQ. Import works properly. But I am getting some problems when exporting the same files.

There are 13 tables so i import 13 csv files.Now i import same files to database but one tables has comma seperated data.

like(a,b,c).

when i import data of this table then in excel file it creates new column.And when i import this file i get error:

Input array is longer than the number of columns in this table.

Here is my code:

Export

string fileName = mt.TableName.Replace("dbo.", "") + "_" + TenantID + ".csv";
StreamWriter writer = new StreamWriter(filePath + fileName);

for (int j = 0; j < gd.HeaderRow.Cells.Count; j++)
{
    if (gd.HeaderRow.Cells[j].Text == "&nbsp;")
        gd.HeaderRow.Cells[j].Text = "";
    writer.Write(gd.HeaderRow.Cells[j].Text);
    if (j != gd.HeaderRow.Cells.Count)
    {
        writer.Write(",");
    }
}
writer.Write(sw.NewLine);

for (int i = 0; i < gd.Rows.Count; i++)
{
    for (int j = 0; j < gd.Rows[i].Cells.Count; j++)
    { 
        if (gd.Rows[i].Cells[j].Text == "&nbsp;")
            gd.Rows[i].Cells[j].Text = "";

        writer.Write(Convert.ToString(gd.Rows[i].Cells[j].Text));

        if (j != gd.Rows[i].Cells.Count)
        {
            writer.Write(",");
        }
    }
    writer.Write(sw.NewLine);
}
writer.Flush();
writer.Close();

Import

// Prepare for the data to be processed into a DataTable
// We don't know how many records there are in the .csv, so we
// use a List<string> to store the records in it temporarily.
// We also prepare a DataTable;
List<string> rows = new List<string>();

// Then we read in the raw data
StreamReader reader = new StreamReader(path, true);
string record = reader.ReadLine();
string[] column = record.Split(',');
List<string> c = column.ToList<string>();
c.RemoveAll(a => string.IsNullOrEmpty(a));
column = c.ToArray();
while (record != null)
{
    rows.Add(record);
    record = reader.ReadLine();
}

// And we split it into chunks.
// Note that we keep track of the number of columns
// in case the file has been tampered with, or has been made
// in a weird kind of way (believe me: this does happen)

// Here we will store the converted rows
List<string[]> rowObjects = new List<string[]>();

int maxColsCount = 0;
foreach (string s in rows)
{
    string[] convertedRow = s.Split(new char[] { separator });
    List<string> y = convertedRow.ToList<string>();
    y.RemoveAll(p => string.IsNullOrEmpty(p));
    convertedRow = y.ToArray();

    if (convertedRow.Length > maxColsCount)
    maxColsCount = convertedRow.Length;
    rowObjects.Add(convertedRow);
}

// Then we build the table
table = new DataTable(tableName);
foreach (string col in column)
{
    table.Columns.Add(new DataColumn(col));
}

int j = 0;
foreach (string[] rowArray in rowObjects)
{
    if (j != 0)
        table.Rows.Add(rowArray);
    j = j + 1;
}
table.AcceptChanges();
sq33G
  • 3,320
  • 1
  • 23
  • 38
aman
  • 1
  • 1
  • 4
  • Please show small peace of code, which reproduces your problem – Sergey Berezovskiy Jul 07 '13 at 08:29
  • your problem is dealing with comma in data thus the need to escape the commas, check [this link for the answer](http://stackoverflow.com/questions/769621/dealing-with-commas-in-a-csv-file) one way is to enclose the data in double-quotes – Mahmoud Darwish Jul 07 '13 at 08:36
  • Tried to edit post to represent the code in question. But as @MEYWD said - escaping the commas seems to be the solution. – sq33G Jul 07 '13 at 09:42
  • Also: I think that the OP and I are using the terms "export" and "import" in the opposite directions. I usually would say "export to csv" and "import from csv". – sq33G Jul 07 '13 at 09:43
  • possible duplicate of [Parsing delimited data for specific instance of repeated line](http://stackoverflow.com/questions/15770421/parsing-delimited-data-for-specific-instance-of-repeated-line) – Paul Sweatte Jan 29 '14 at 18:46

0 Answers0