Solved

OleDbDataReader is skipping some values in my CSV file

Posted on 2010-08-20
7
860 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

914 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now