Solved

Importing CSV into DataTable datatype problem  VB 2010

Posted on 2010-08-20
7
1,512 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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 …
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

10 Experts available now in Live!

Get 1:1 Help Now