Reputation: 23
i am trying to import multiple csv files from a folder using textfieldparser . each csv file contains header and 1 record , but when my import method runs it gives error of "a column named "PTNAME" is already belongs to datatable". i couldnt find the way where to change code . plz help me .
here is my import method,
public void ImportAllFilesOfFolder()
{
try
{
DataTable dt = new DataTable();
string sourceDir = txtsend.Text;
var Icsv = Directory.EnumerateFiles(sourceDir, "*.csv");
foreach (string currentfile in Icsv)
{
using (TextFieldParser parser = new TextFieldParser(currentfile))
{
parser.TextFieldType = FieldType.Delimited;
parser.Delimiters = new string[] { "," };
parser.HasFieldsEnclosedInQuotes = true;
string[] columns = parser.ReadFields();
for (int i = 0; i < columns.Length; i++)
{
dt.Columns.Add(columns[i], typeof(string));
}
while (!parser.EndOfData)
{
string[] fields = parser.ReadFields();
DataRow newrow = dt.NewRow();
for (int i = 0; i < fields.Length; i++)
{
newrow[i] = fields[i];
}
dt.Rows.Add(newrow);
}
}
}
con.Open();
SqlBulkCopy bc = new SqlBulkCopy(con.ConnectionString, SqlBulkCopyOptions.TableLock);
bc.DestinationTableName = "SaiRamH";
bc.BatchSize = dt.Rows.Count;
bc.WriteToServer(dt);
bc.Close();
}
catch (SystemException e) { MessageBox.Show(e.Message); }
finally { con.Close(); }
}
and here is my file structure,
PTNAME,REGNO/ID,BLOOD GRP,WARD NAME,DOC NAME,XRAY,PATHO,MEDICATION,BLOOD GIVEN
Mrs.BHAGWAT SUGHANDA RAGHUNTH,SH1401/00457,,GENERAL WARD (FEMALE),SHELKE SAMEER,MRI LS SPINE-PID L3-4 L5S1 MRI BRAIN --- NAD,BSL (R), IV DICLOGESIC RR DRIP 1-0-1 TAB FLEXURA D 1-0-1 TAB ARCOPAN D 1-0-1 TAB BIO D3 STRONG 0-1-0 TAB ALPRAX 0.5 MG 0-0-1 XYNOSURE GEL L/A 1-1-1 TAB NERVSHINE ER 75 0-1-0 CERVICAL TRACTION AND LUMBAR TRACT ALTER WITH IFT 1-0-1,NOT GIVEN.
Upvotes: 0
Views: 603
Reputation: 34421
Made simple change by adding new variable firstFile. See code below
public void ImportAllFilesOfFolder()
{
try
{
DataTable dt = new DataTable();
string sourceDir = txtsend.Text;
var Icsv = Directory.EnumerateFiles(sourceDir, "*.csv");
bool firstFile = true;
foreach (string currentfile in Icsv)
{
using (TextFieldParser parser = new TextFieldParser(currentfile))
{
parser.TextFieldType = FieldType.Delimited;
parser.Delimiters = new string[] { "," };
parser.HasFieldsEnclosedInQuotes = true;
string[] columns = parser.ReadFields();
if (firstFile == true)
{
for (int i = 0; i < columns.Length; i++)
{
dt.Columns.Add(columns[i], typeof(string));
}
}
while (!parser.EndOfData)
{
string[] fields = parser.ReadFields();
DataRow newrow = dt.NewRow();
for (int i = 0; i < fields.Length; i++)
{
newrow[i] = fields[i];
}
dt.Rows.Add(newrow);
}
}
firstFile = false;
}
}
catch (Exception e)
{
}
}
Upvotes: 0
Reputation: 3808
The code is attempting to add new columns each iteration of the foreach loop. Adding a conditional before the loop will prevent the issue. In the code below we are checking to see if the datatable's column has been populated.
If (dt.Columns.Count == 0)
{
for (int i = 0; i < columns.Length; i++)
{
dt.Columns.Add(columns[i], typeof(string));
}
}
Upvotes: 1