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

x
?
Solved

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

Posted on 2014-09-04
4
Medium Priority
?
910 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
3 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 46

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 64

Accepted Solution

by:
Fernando Soto earned 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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 article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Starting up a Project

876 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