Solved

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

Posted on 2014-09-04
4
764 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 62

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

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.

Join & Write a Comment

This is about my first experience with programming Arduino.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

707 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

13 Experts available now in Live!

Get 1:1 Help Now