Solved

OleDbDataReader is skipping some values in my CSV file

Posted on 2010-08-20
7
898 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

705 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