473,387 Members | 1,590 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Not all CSV column data being read into DataTable

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
4 3491
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: John Latten | last post by:
Hi there, I have got a problem when adding extra (= dummy) columns to a datagrid. I have a datagrid that is bound to a datatable. The datatable is filled with data from a SQL Server 2000...
2
by: Joe | last post by:
Hi All, I am new to using the Access DB and I need some help if someone is able to give it to me. What I want to do is get the names of the columns of certain tables. Not the data in the table...
6
by: Hutty | last post by:
I've looked around and have yet to find anything that would answer my question regarding formating a column in a datagrid. My grid looks like this as far as data" AMHQCON|51300.01|-3147 The...
16
by: Geoff Jones | last post by:
Hi Can anybody help me with the following, hopefully simple, question? I have a table which I've connected to a dataset. I wish to add a new column to the beginning of the table and to fill...
1
by: Larry Bird | last post by:
I've created a AlertDataClass below within the class I have tables and column that I've create. In the AlertDataAccess class I'm trying to insert data into my tables. AlertDataAccess is a Module...
3
by: Gene Hubert | last post by:
I'm using DataTable.ImportRow to move data from one datatable to another... Dim dt, dtTarget As DataTable Dim dr As DataRow dt = DirectCast(Me.DataSource, DataTable) dtTarget = dt.Clone...
3
by: Oenone | last post by:
I have a project that creates a SqlDataAdapter and uses its Fill method to fill a DataTable with data from a user-provided query. From there I can obviously access details about the rows and...
1
by: Ryan Liu | last post by:
Hi, I read a csv file to a datatable. I don't know what are the column types at this moment, so I treat all columns as string type. After I read data from file, I use...
1
by: majidkorai | last post by:
Hey Guyz I am having problem when i read the data from a datareader into a data table. The whole scenario is this that I want to read data from two diffrent databases, the table strcutre is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.