473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Error while reading CSV file

Hi Guys,

I have a weird problem.
While reading my csv file some data a returned as null.

When the routine reads values in the column "Size_Code" (like "S",, "M"...) it returns "NULL".
When routine reads the other values in the same column "Size_Code" (like 2, 4, 6 ...) it returns the right value.

Any clue ? Any help?
Shall i have to force a string conversion ? If yes how can i do it the sql statement ?

Thank you
Paul


-----------------------------------------------------------------------------
Here's some data from my CSV file:
-----------------------------------------------------------------------------
Style_code,Color_Code,SIZE_code,SKU,UPC,Descriptio n,Customer_Code,CUSTOMER_SKU
46400,Snow,S ,,776512634247,Cardigan/Snow,CUSTOMER_CODE,252675
46400,Snow,M,,776512634254,Cardigan/Snow,CUSTOMER_CODE,253965
46400,Snow,L,,776512634261,Cardigan/Snow,CUSTOMER_CODE,253969
46500,sand,2,,776512634360,Blouse/sand,CUSTOMER_CODE,252679
46500,sand,4,,776512634377,Blouse/sand,CUSTOMER_CODE,253979
46500,sand,6,,776512634384,Blouse/sand,CUSTOMER_CODE,253980

46400,Snow,XL,,776512634278,Cardigan/Snow,CUSTOMER_CODE,253970
46400,Mint,S ,,776512634285,Cardigan/Mint,CUSTOMER_CODE,253971
46400,Mint,M,,776512634292,Cardigan/Mint,CUSTOMER_CODE,253972
46400,Mint,L,,776512634308,Cardigan/Mint,CUSTOMER_CODE,253973




-----------------------------------------------------------------------------
Here's my VB.Net code
-----------------------------------------------------------------------------
Dim v_sql_connection As SqlConnection
Dim v_connection_csv As System.Data.OleDb.OleDbConnection
Dim v_sql_command As OleDbCommand
Dim v_reader As OleDbDataReader

Dim v_file_name As String = "test.csv"

' Copy data from CSV file to SQL table
v_connection_csv = New System.Data.OleDb.OleDbConnection("Provider=Micros oft.Jet.OLEDB.4.0; " + "Data Source=" + "C:\" + ";" + "Extended Properties=""text; HDR=Yes; FMT=Delimited""")

'Dim v_connection_str As String = ConfigurationManager.ConnectionStrings("LocalSqlSe rver").ConnectionString
'v_sql_connection = New SqlConnection(v_connection_str)

v_connection_csv.Open()


Dim v_sql As String

' --------------------------------------------------------------------------------------------------
' The engine should read the CSV file line by line
' We don't upload the line if we find it invalid, meaning the following are OK per line.
' - Warehouse code: Should NOT be null
' - Customer code: Should NOT be Null AND should exist in the Customers Table
' - Currency code: Should NOT be Null AND should exist in the Currencies table
' Only Rejected records are displayed in the grid to notify user about records that were not uploaded
' ----------------------------------------------------------------------------------------------------
v_sql = " SELECT Customer_Code AS CUSTOMER_CODE "
v_sql &= " , Customer_SKU AS CUSTOMER_SKU "
v_sql &= " , Style_Code AS STYLE_CODE "
v_sql &= " , Color_Code AS COLOR_CODE "
v_sql &= " , char(Size_Code) AS SIZE_CODE "
v_sql &= " , SKU AS SKU "
v_sql &= " , UPC AS UPC "
v_sql &= " FROM (" + v_file_name + ") "

v_sql_command = New OleDbCommand(v_sql, v_connection_csv)
v_reader = v_sql_command.ExecuteReader()

Dim v_message As String
Dim v_nb_error As Integer = 0

Dim v_customer_code As String
Dim v_customer_sku As String
Dim v_style_code As String
Dim v_color_code As String
Dim v_size_code As String
If v_reader.HasRows Then

While v_reader.Read

v_customer_code = v_reader("CUSTOMER_CODE").ToString
v_customer_sku = v_reader("CUSTOMER_SKU").ToString
v_style_code = v_reader("STYLE_CODE").ToString
v_color_code = v_reader("COLOR_CODE").ToString
v_size_code = v_reader("SIZE_CODE").ToString.Trim

Response.Write("Item = " & v_customer_code & " - " & v_customer_sku & " - " & v_style_code & " - " & v_color_code & " - " & v_size_code & "<BR>")

End While
End If
Jan 5 '08 #1
1 2898
Torgg
41
Here is code I used in a project where I needed to open a csv file and parse it. My code (which I've tested with your data) opens the csv file and populates a datatable. I simply run through the datatable and show all your data in the output window.

Here is the Method...

Expand|Select|Wrap|Line Numbers
  1.  
  2. Public Function ParserCSVData(ByVal sCSV_Path As String, ByVal sCSV_Name As String, Optional ByVal sCSV_Header As Boolean = True) As DataTable
  3.         'This function simply takes the raw .csv file and imports it to a DataTable
  4.         'sCSV_Path = "C:\Documents and Settings\user\Desktop\" 'without the file name and ext.
  5.         'sCSV_Name = "test.csv" just the file name and ext. 
  6.         'sCSV_Header = Does the .csv file have a header row
  7.  
  8.         Dim tmpDataTable As New DataTable
  9.         Try
  10.             Dim sHeader As String = "HDR=YES;"
  11.             If sCSV_Header = False Then
  12.                 sHeader = "HDR=NO;"
  13.             End If
  14.  
  15.             Dim sConnection As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sCSV_Path & ";Extended Properties=;text;" & sHeader & ""
  16.             Using oConnection As New System.Data.OleDb.OleDbConnection(sConnection)
  17.                 Dim tmpDataAdapter As New OleDb.OleDbDataAdapter
  18.                 tmpDataAdapter.SelectCommand = New OleDb.OleDbCommand("Select * From [" & sCSV_Name & "]", oConnection)
  19.  
  20.                 tmpDataAdapter.Fill(tmpDataTable)
  21.                 tmpDataTable.TableName = "CSVData"
  22.             End Using
  23.         Catch ex As Exception
  24.             MsgBox(ex.ToString)
  25.         End Try
  26.  
  27.         Return tmpDataTable
  28.     End Function

Here is the usage...

Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim dtTestDataTable = ParserCSVData("C:\Users\Torgg\Desktop", "test.csv", True)
  3.  
  4.         For Each row As DataRow In dtTestDataTable.Rows
  5.             Console.WriteLine("Item = " & row("CUSTOMER_CODE") & " - " & row("CUSTOMER_SKU") & " - " & row("STYLE_CODE") & " - " & row("COLOR_CODE") & " - " & row("SIZE_CODE"))
  6.             'Response.Write("Item = " & row("CUSTOMER_CODE") & " - " & row("CUSTOMER_SKU") & " - " & row("STYLE_CODE") & " - " & row("COLOR_CODE") & " - " & row("SIZE_CODE") & "<BR>")
  7.         Next
I hope this is helpful,
Torgg
Jan 5 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

20
by: TTroy | last post by:
Hello, I have found some peculiar behaviour in the fgets runtime library function for my compiler/OS/platform (Dev C++/XP/P4) - making a C console program (which runs in a CMD.exe shell). The...
2
by: GB | last post by:
Hi Everybody! I have 2 different processes/application. One is writing to a file and another is reading from it. For some reason the code doesnt seems to work and gives mscorlib.dll IOException...
0
by: Rhon Stewart via DotNetMonster.com | last post by:
Hi please visit this link : http://www.eggheadcafe.com/articles/pfc/selfupdater.asp I followed all the steps for listed on the link , when I execute the application it it gives me the following...
25
by: moondaddy | last post by:
I have an application where users need to upload images and in my web.config file I have a setting like this: <httpRuntime maxRequestLength="512" /> Which restricts image larger than 500k from...
0
by: Mart | last post by:
Hi, I have just written (my first) VB.net app using MS Visual Basic 2005 Express Edition Beta. It is fairly simple, it reads some configuration data from an XML file then opens a new window...
6
by: tgnelson85 | last post by:
Hello, C question here (running on Linux, though there should be no platform specific code). After reading through a few examples, and following one in a book, for linked lists i thought i would...
1
by: RADAR | last post by:
hi, the error code was taken from msdn and it occured in my code as follows; i am reading buffer from the file : #include <stdio.h> #include <stdlib.h> #define SIZE 10 #define DSIZE 5...
1
by: Steve Ametjan | last post by:
I've been trying to get MySQL-python to install on Leopard for the past couple of days, and I keep running into relatively the same error. I'm hoping that someone on this list will be able to...
4
by: radhikams | last post by:
hi I have written a code for writing the data of a column from database into a file and again reading that file and displaying. Im writing into an .html file....Now the problem is im...
0
Guido Geurs
by: Guido Geurs | last post by:
I'm writing a program that list the contents of a CDrom and also the contents of the ZIP files. When there is a bad Zip file on the CD, the program keeps traying to reed the file and after +- 50...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.