Solved

OleDbDataReader is skipping some values in my CSV file

Posted on 2010-08-20
7
876 Views
Last Modified: 2012-05-10
I have some code that is parsing a CSV that a user uploads.  It's a way from the customer to keep their records up to date.  Anyways, everything seemed to be working fine but I noticed that none of the Canadian Zip Codes were coming over (going from CSV to SQL by the way).  When I parse through, the US zip codes pull fine from the CSV but the Canadian are blank.  I'll post some code that I am using, any help would be appreciated.

Thanks.
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strPath + ";Extended Properties='text;HDR=Yes;FMT=Delimited'";
        OleDbConnection conn = new OleDbConnection(strConn);
        conn.Open();
        try
        {
            OleDbCommand comm = new OleDbCommand("SELECT * from [" + strFilename + "]", conn);
            OleDbDataReader read = comm.ExecuteReader();

            while (read.Read())
            {
                xdlr = "HL" + read[0].ToString();
                xname = read[1].ToString();
                xaddr = read[3].ToString();
                xcity = read[4].ToString();
                xstate = read[5].ToString();
                xzip = read[6].ToString();
                xphone = read[7].ToString();
                xfax = read[8].ToString();
                xemail = read[9].ToString();
            }  
        }

Open in new window

0
Comment
Question by:drunk_irishman
  • 3
  • 2
  • 2
7 Comments
 
LVL 7

Expert Comment

by:jdavistx
ID: 33486949
What does one of your CSV lines, containing a Canadian zip, look like?
0
 
LVL 1

Author Comment

by:drunk_irishman
ID: 33487189
I took a couple lines out of the file, 1 for a US address/zip and 1 problem child.
test.csv
0
 
LVL 16

Expert Comment

by:kris_per
ID: 33487804

May be OLEDB driver takes the ZIPCODE column as integer type...try placing a canadian row as the first line in the csv file as OLEDB might check only the first row to find which type a column is...if the first row is non-integer, then it will treat that column as string and canadian post codes will come up....
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

by:drunk_irishman
ID: 33487886
I already moved one of the Canadian records to the top of the list with no luck.

I then changed the connection string to indicate there was no Header row, thinking that would trick it into treating the entire Zip Code column as text (assuming it bases off the first record it finds), which in this case would be the value "Zip Code".....still no luck.

string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strPath + ";Extended Properties='text;HDR=No;FMT=Delimited'";
0
 
LVL 7

Expert Comment

by:jdavistx
ID: 33487960
Nothing is jumping to mind.  This isn't related to your question, but I think you may want run your code like the attached.
OleDbConnection conn = new OleDbConnection();

try
{
	using(conn = new OleDbConnection(strConn))
	{
		conn.Open();
		//...
	}
}
catch(Exception e){ Console.WriteLine(e.ToString()); }
finally{ conn.Close(); }

Open in new window

0
 
LVL 16

Accepted Solution

by:
kris_per earned 500 total points
ID: 33492208

The issue is ado.net takes the ZIPCODE as int type column and canadian zipcodes which has text get skipped. You can use schema.ini file to tell ado.net what is the format and structure of the columns.

Schema.ini file will be like:
[test.csv]
ColNameHeader=True
Format=CSVDelimited
DateTimeFormat=dd-MMM-yyyy
Col1=A DateTime
Col2=B Text Width 100
Col3=C Text Width 100
Col4=D Long
Col5=E Double

Save Schema.ini file in the same folder of test.csv and run the code again....

You can check out this link on using schema.ini file => http://www.aspdotnetcodes.com/Importing_CSV_Database_Schema.ini.aspx

0
 
LVL 1

Author Comment

by:drunk_irishman
ID: 33492550
Thanks kris_per, I'll give it a shot, knew it was something with integer and text transition but wasn't sure of the resolution.  Thanks again.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
rest webservice call over https via c# 6 49
Keyboard 2 41
List<PaisEntity> - show some 1 28
Please explain the parts of these 2 LINQ expressions 3 53
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question