Solved

OleDbDataReader is skipping some values in my CSV file

Posted on 2010-08-20
7
851 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

22 Experts available now in Live!

Get 1:1 Help Now