Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET Database and Reporting > CSV to datatable only showing integers


Reply
 
Thread Tools Display Modes
  #1  
Old 11-12-2007, 08:48 AM
itsover itsover is offline
Newcomer
 
Join Date: Nov 2007
Posts: 4
Default CSV to datatable only showing integers


Hey,

My app has a simple function that selects all the data on an csv file, puts the data onto a datatable, and displays that datatable on a datagridview.

The problem is that if a column in my csv file contains mostly numbers (50% or greater), the rest of the values are missing. For example, if my csv file contains:

Code:
Code,Description,Filter
N,None,FALSE
1,area flood or flash flood products,FALSE
2,Minor,FALSE
3,Moderate,FALSE
4,Major,FALSE
U,Unknown,FALSE
then my datatable contains:

Code:
Code,Description,Filter
[DbNull],None,FALSE
1,area flood or flash flood products,FALSE
2,Minor,FALSE
3,Moderate,FALSE
4,Major,FALSE
[DbNull],Unknown,FALSE
If my csv file contains:

Code:
Code,Description,Filter
N,None,FALSE
N,area flood or flash flood products,FALSE
N,Minor,FALSE
3,Moderate,FALSE
4,Major,FALSE
U,Unknown,FALSE
it would work fine.

Here is the function:

Code:
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

        Dim connStr As String = _
            "Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "Data Source=" & Environment.CurrentDirectory & ";" & _
            "Extended Properties=""text;HDR=YES\;FMT=Delimited;"""
        Dim dbConn As OleDbConnection = New OleDbConnection(connStr)
        Dim dbCmd As OleDbCommand = New OleDbCommand("SELECT * FROM " & csvFile, dbConn)
        Dim dbDataAdptr As OleDbDataAdapter = New OleDbDataAdapter()
        Dim csvDt As DataTable = New DataTable()

        ' Open connection to csv database
        dbConn.Open()

        ' Add columns to csv datatable, explicitly set type
        csvDt.Columns.Add("Code", GetType(String))
        csvDt.Columns.Add("Description", GetType(String))
        csvDt.Columns.Add("Filter", GetType(Boolean))

        ' Fill datatable from database
        dbDataAdptr.SelectCommand = dbCmd
        dbDataAdptr.Fill(csvDt)

        ' Display on datagridview
        DataGridView1.DataSource = csvDt

        ' Clean up
        dbConn.Close()
        dbCmd = Nothing
        dbConn = Nothing

    End Sub
At first I realized the datatable first column's value type was automatically being set to int32, so I explicitly add columns (with first column's value type as string). But that did not solve the problem.

Any advice is appreciated. Thank you in advance.
Reply With Quote
  #2  
Old 11-12-2007, 11:19 AM
Jayded Jayded is offline
Centurion
 
Join Date: Jul 2003
Posts: 144
Default

I dont mean to sound weird but why bother with the datatable? You would have a lot more control over the displayed elements if you wrote it straight to the datagridview.
The datatable is trying to be to intelligent when all you really need to do is display data on a datagridview. Unless you have strong reasons for using the datatable I would dump it altogether. The speed difference would be negligible for such small data amounts and even for a larger amount it would perform that poorly.
Reply With Quote
  #3  
Old 11-13-2007, 08:57 AM
itsover itsover is offline
Newcomer
 
Join Date: Nov 2007
Posts: 4
Default

I'd like to treat the csv file as a database rather than a text file. In order for me to put the results of a query onto a datagridview, I needed to use a datatable as a medium to the database. If you could suggest to me another way, which does not require the datatable, I'd be happy to try it.
Reply With Quote
  #4  
Old 11-13-2007, 10:17 AM
Jayded Jayded is offline
Centurion
 
Join Date: Jul 2003
Posts: 144
Default

It would be a fairly simple process of loading the csv file into a IO stream and then reading it line by line in a loop.
You would then load the datagridview using a string array.
That said on re reading the problem could I ask whether you have checked the data in the datatable? It would be interesting to see what has been loaded and it may also give a clue as to whats going on. As a suggestion run a quick loop through your datatable and either debug.print the results or load them into a msgbox().
If the problem is between the datatable and the datagridview then it would probably be easier to load the datagridview manually from the datatable.
Could you also attach a real sample of the CSV file?
Reply With Quote
  #5  
Old 11-13-2007, 01:15 PM
itsover itsover is offline
Newcomer
 
Join Date: Nov 2007
Posts: 4
Default

It is not the problem of the datatable to datagridview. The datatable is missing the data.

I am strongly trying to avoid using file IO to process the csv file as it should be treated as a database.
Attached Files
File Type: zip hvtec_severity.zip (241 Bytes, 1 views)
Reply With Quote
  #6  
Old 11-13-2007, 05:18 PM
IUnknown's Avatar
IUnknown IUnknown is offline
Senior Contributor

Forum Leader
* Expert *
 
Join Date: Oct 2004
Location: Montréal
Posts: 1,135
Default

You can use a schema.ini file to define the datatype of the columns in the csv file to be imported.

BTW, you can also put the connection string into the app.config file and retrieve it using MySettings.
__________________
win7 : vs 2008 : .Net 3.5

Last edited by IUnknown; 11-13-2007 at 05:28 PM.
Reply With Quote
  #7  
Old 11-13-2007, 10:07 PM
itsover itsover is offline
Newcomer
 
Join Date: Nov 2007
Posts: 4
Default

Quote:
You can use a schema.ini file to define the datatype of the columns in the csv file to be imported.

BTW, you can also put the connection string into the app.config file and retrieve it using MySettings.
This solved my problem! Thank you very much!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->