Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Importing CSV into DataTable datatype problem  VB 2010

Posted on 2010-08-20
7
Medium Priority
?
1,561 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
[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
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 2000 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

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!

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

670 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