Solved

Importing data into a table in Visual Studio Express 2013 from a comma delimited File

Posted on 2014-09-04
4
816 Views
Last Modified: 2014-09-10
I want to read the contents of a csv file........first line is headers,.......comma delimited, text has is in "" quotes. I found some help online, but it locks up my system and eventually returns an error:



Ultimately, once the data is in, I want to run some SQL on the contents. this is the header infor:

"End_Date","Entity","AcctNo",AJE_AMT



This is the code I am using. If there is a better way, that is fine:
Imports System.ComponentModel
Imports System.Text
Imports System.Data.OleDb
Imports System.IO

Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim folder = "C:\Users\rrudo_000\Documents"
        Dim CnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & folder & ";Extended Properties=""text;HDR=Yes;FMT=Delimited"";"
        Dim dt As New DataTable
        Using Adp As New OleDbDataAdapter("select * from [ajework.csv]", CnStr)
            Adp.Fill(dt)
        End Using

        Dim bs As New BindingSource
        Dim datagridview1 As New DataGridView()
        bs.DataSource = dt
        datagridview1.DataSource = bs
    End Sub


End Class

Open in new window

Error.JPG
0
Comment
Question by:rrudolph
4 Comments
 

Author Comment

by:rrudolph
ID: 40304744
The table I want to add the data to is just a temporary table and does not need to be kept afterwards.
0
 
LVL 45

Expert Comment

by:aikimark
ID: 40305557
It might help you find the bug if you broke up the CSV-to-DataTable process.
Imports System.ComponentModel
Imports System.Text
Imports System.Data.OleDb
Imports System.IO

Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim folder = "C:\Users\rrudo_000\Documents"
        Dim CnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & folder & ";Extended Properties=""text;HDR=Yes;FMT=Delimited"";"
        Dim conn As New OleDbConnection(CnStr)
        conn.Open()
        Dim dt As New DataTable
        Dim cmd As New OleDbCommand("select * from [ajework.csv]", conn)
        Using Adp As New OleDbDataAdapter()
            Adp.Selectcommand = cmd
            Dim ds As New Dataset()
            Adp.Fill(ds)
            dt = ds.Tables(0)
        End Using

        Dim bs As New BindingSource
        Dim datagridview1 As New DataGridView()
        bs.DataSource = dt
        datagridview1.DataSource = bs
    End Sub


End Class

Open in new window

0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40308603
Hi rrudolph;

The only issue I see with the code is that you create a new control called datagridview1 and set it up to be bound to a binding source component and assign that to the data source of the newly created DataGridView. But this new DataGridView will never be displayed on the form because you do not add it to the Form control collection. Now this may be what you are looking for I can not tell from the question. But if it is I would look at the csv file that you are reading in because it may have an issue. I created a test csv file and your code and had no issue.
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

730 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