Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

920 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

17 Experts available now in Live!

Get 1:1 Help Now