Solved

Importing CSV into DataTable datatype problem  VB 2010

Posted on 2010-08-20
7
1,526 Views
Last Modified: 2012-05-10
Hi

Im importing a CSV file into a datatable, it all goes well, except that I have a few columns that have rows which contain eaither a number or "????" . Unfortunatly the code that I have converst this column of data into double and my "????" are then turned into zero.

I would like to change the data columns datatype but I havent been able to do so even though I change the type prior to filling the table (see code). how do force the columns to be read as text ?

Michal
Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Path.GetDirectoryName(CSVName) & "\;Extended Properties=""text;HDR=YES;FMT=Delimited"""
            Dim objConn As New System.Data.OleDb.OleDbConnection(sConnectionString)
            Dim da As OleDb.OleDbDataAdapter



            'Open Data Adapter to Read from Text file
            Dim str As String = "SELECT * FROM [" & Path.GetFileName(CSVName) & "]"
            da = New System.Data.OleDb.OleDbDataAdapter(str, objConn)
            'Fill dataset using dataadapter
            da.FillSchema(SystemTable, SchemaType.Mapped)

            For Each column As DataColumn In SystemTable.Columns
                If column.ColumnName = "WINDIV" Then column.DataType = System.Type.GetType("System.String")
                If column.ColumnName = "PLCDIV" Then column.DataType = System.Type.GetType("System.String")
                If column.ColumnName = "SPDIV" Then column.DataType = System.Type.GetType("System.String")

            Next

            da.Fill(SystemTable)
            objConn.Close()

Open in new window

0
Comment
Question by:MichMat
  • 4
  • 3
7 Comments
 
LVL 38

Expert Comment

by:puppydogbuddy
ID: 33491354
Just an educated guess:
change this portion of your code:  column.DataType = System.Type.GetType("System.String")

                                          to this:   column.DataType = System.Type.GetType("String")        
0
 

Author Comment

by:MichMat
ID: 33491392
throws up an error Im sorry to say that the column data type is not recognised
0
 
LVL 38

Expert Comment

by:puppydogbuddy
ID: 33491540
see:  http://www.functionx.com/vbnet/oledb/Lesson02.htm
There are two primary pieces of information you must specify for each column: its name and its type. Therefore, the syntax of creating a column is:

ColumnName DataType Options

Change:
column.DataType = System.Type.GetType("System.String")

To this:
columnName.DataType = System.Type.GetType("System.String")
or
columnName.DataType = System.Type.GetType("System.Char")
or
columnName.DataType = System.Type.GetType("System.SqlString")
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 38

Expert Comment

by:puppydogbuddy
ID: 33491684
forgot to tell you to replace columnName above with your actual column name.
0
 

Author Comment

by:MichMat
ID: 33493043
Hi ,

Thanks for helping , so far all I can gathre that Microsoft will relentlesly 'help' me !

 System.ArgumentException: Invalid cast from 'Double' to 'Char'.Couldn't store <4> in WINDIV Column.

the SQLSTring get the same datatype error as mentioned in previous post.

It seam a crazy behaviour, I can load the table by using streemreader and get what I need but this process takes a minute   and a bit to load the table , splitting each line as opposed to less then 10 seconds with the way that I have here.
0
 
LVL 38

Accepted Solution

by:
puppydogbuddy earned 500 total points
ID: 33493135
At least you are making progress!  
1.  If column is alphanumeric, try replacing "Char" with "varChar"
2.  If column is numeric, try using Double,numeric and decimal, float and real, or money

refer to the link I gave you for questions about data types in .Net
0
 

Author Comment

by:MichMat
ID: 33493202
Hi ,

Thanks , that is a veru usefull link. I didnt have any luck with varchar, I guess that JET is very limited in what it will accept. I have been able to change the tables columns as we seen , but JET still converts the data to what ever it thinks it should be and then tries to fit a square peg into a round hole. ie Double into a char or even string.

My only option may be to force the columns to be cast when they are read ...... that would mean writing out a select statment with 162 columns !! But I havent figured out how to cast , I have used it in SQL but Jet just refuses to accept it , but I havent done much reaserch in that area yet
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…

766 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