Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hey friends,

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,

C#

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.
Posted
Updated 3-May-15 0:26am
v2
Comments
OriginalGriff 3-May-15 6:11am    
BTW: I was going to correct your formatting - but the code you pasted was not indented. Indent your code in future and use pre tags to preserve the formatting - it makes your code a lot easier to read.

1 solution

We can't tell - you need to use the debugger and start looking at what is happening.

So start by putting a breakpoint on this line:
C#
dt.Columns.Add(columns[i], typeof(string));
And run your app in the debugger.
When it hits the breakpoint, it will stop, and you should look at the content of the columns array to check your column names.
It they contain no duplicates, then start stepping through the code to see exactly what columns are being added to your DataTable.

BUt we can't do that for you - we don't have access to your code or your data - so you need to do this bit at least yourself!
 
Share this answer
 
Comments
Member 11543226 3-May-15 6:27am    
YES i know the problem is in this line but if i remove this loop and add all columns mannualy it again gives same error.
OriginalGriff 3-May-15 6:35am    
The problem isn't the code, most likely - so you need to look at the data. At the moment, you don't know what columns it is trying to add - so you need to use the debugger and find out.

Without that information, you can't investigate why you have a problem, which means you can't be sure to solve it.
Member 11543226 3-May-15 6:45am    
how to use debugger to find out problem i am very new to .net.
OriginalGriff 3-May-15 6:51am    
OK - you know how to put a breakpoint in? And how to run the app in the debugger?
Member 11543226 3-May-15 7:01am    
ya i when i put breakpoint on that line it doesnt gives error but not given output too,
and run the app in debugger means simple debug program or anything else.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900