By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,680 Members | 1,745 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,680 IT Pros & Developers. It's quick & easy.

Not all CSV column data being read into DataTable

P: n/a
I read the CSV file into a DataTable. This is so I can fix invalid dates
and other data I don't want in the database. Then I use SqlBulkCopy to
insert the data into the SQL database. All the rows are being read and
inserted into the table. The problem is that one of the columns in the CSV
contains the value of 1 thru 6 or C, BUT only the numbers seem to be read
into the DataTable not the letter. I tried single quotes and double quotes
around the character. I even changed the data in another (a number) to a
letter and it did the same thing.

This is the code I use fill the DataTable with the CSV file.

OleDbConnection con = new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + filePath +
";Extended Properties='text;HDR=Yes;Format=Delimited';");
OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM " + sTable +
".csv", con);
DataTable dt = new DataTable("ImportedCSV");
da.Fill(dt);

I then loop over each row to to check the date fields for invalid dates and
other errors prior to performing a SqlBulkCopy this is how I know it is not
reading into the DataTable. I just changed the dt.Rows[i]["colName"] to show
me what is in that column and everytime it should have "C" it has "" whereas
"1" shows "1".

Any ideas? Any Solutions?

--
Regards,

Mike D
Coding in C# since Feb 2007
Sep 19 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Wed, 19 Sep 2007 09:12:07 -0700, Mike D
<Mi***@discussions.microsoft.comwrote:
>I read the CSV file into a DataTable. This is so I can fix invalid dates
and other data I don't want in the database. Then I use SqlBulkCopy to
insert the data into the SQL database. All the rows are being read and
inserted into the table. The problem is that one of the columns in the CSV
contains the value of 1 thru 6 or C, BUT only the numbers seem to be read
into the DataTable not the letter. I tried single quotes and double quotes
around the character. I even changed the data in another (a number) to a
letter and it did the same thing.

This is the code I use fill the DataTable with the CSV file.

OleDbConnection con = new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=" + filePath +
";Extended Properties='text;HDR=Yes;Format=Delimited';");
OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM " + sTable +
".csv", con);
DataTable dt = new DataTable("ImportedCSV");
da.Fill(dt);

I then loop over each row to to check the date fields for invalid dates and
other errors prior to performing a SqlBulkCopy this is how I know it is not
reading into the DataTable. I just changed the dt.Rows[i]["colName"] to show
me what is in that column and everytime it should have "C" it has "" whereas
"1" shows "1".

Any ideas? Any Solutions?
From the symptoms you describe if may be that the datatable does not
like a 'C' in that column - perhaps it is expecting all digits? How
is that column defined in the datatable? What are the valid
characters for the type the column is defined as?

What does the 'C' in that column mean? Is there a different way of
coding for that same value to be input?

rossum

Sep 19 '07 #2

P: n/a
Thanks for the reply.
I agree, it does sound as if the DataTable doesn't like "C" (or any letter)
however the DataTable doesn't have any columns datatyped. The values for the
column are set in stone by SAP and I can't change them ('C' stands for
cancelled). In the CSV the columns are not datatyped either since it is a
text file and therefore contains no formatting/datatyping data. I tried
wrapping the letter in both single and double quotes and nothing worked.

--
Regards,

Mike D
Coding in C# since Feb 2007
"rossum" wrote:
On Wed, 19 Sep 2007 09:12:07 -0700, Mike D
<Mi***@discussions.microsoft.comwrote:
I read the CSV file into a DataTable. This is so I can fix invalid dates
and other data I don't want in the database. Then I use SqlBulkCopy to
insert the data into the SQL database. All the rows are being read and
inserted into the table. The problem is that one of the columns in the CSV
contains the value of 1 thru 6 or C, BUT only the numbers seem to be read
into the DataTable not the letter. I tried single quotes and double quotes
around the character. I even changed the data in another (a number) to a
letter and it did the same thing.

This is the code I use fill the DataTable with the CSV file.

OleDbConnection con = new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + filePath +
";Extended Properties='text;HDR=Yes;Format=Delimited';");
OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM " + sTable +
".csv", con);
DataTable dt = new DataTable("ImportedCSV");
da.Fill(dt);

I then loop over each row to to check the date fields for invalid dates and
other errors prior to performing a SqlBulkCopy this is how I know it is not
reading into the DataTable. I just changed the dt.Rows[i]["colName"] to show
me what is in that column and everytime it should have "C" it has "" whereas
"1" shows "1".

Any ideas? Any Solutions?
From the symptoms you describe if may be that the datatable does not
like a 'C' in that column - perhaps it is expecting all digits? How
is that column defined in the datatable? What are the valid
characters for the type the column is defined as?

What does the 'C' in that column mean? Is there a different way of
coding for that same value to be input?

rossum

Sep 19 '07 #3

P: n/a
I'd highly recommend this CSV reader. Has it's own API and also
implements IDataReader.

http://www.codeproject.com/cs/database/CsvReader.asp

HTH,

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

Sep 19 '07 #4

P: n/a
Well I figured it out! All I needed was a schema.ini file in the same folder
as the CSV files. The schema.ini file set the datatype of each column to
'text'. Now all the data is imported into the DataTable and eventually into
the SQL server.

Thanks to all who assisted me.

--
Regards,

Mike D
Coding in C# since Feb 2007
Sep 20 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.