Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Importing CSV into DataTable datatype problem  VB 2010

Posted on 2010-08-20
7
Medium Priority
?
1,571 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
Independent Software Vendors: 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 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

Independent Software Vendors: 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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

824 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