Solved

OleDbDataReader is skipping some values in my CSV file

Posted on 2010-08-20
7
868 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Connection String 16 55
Error in script 11 56
open System.IO.Compression.ZipArchiveEntry as ZipFile without storing to file 2 56
List<PaisEntity> - show some 1 24
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

776 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