Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

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
?
888 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Make the most of your online learning experience.
Simple Linear Regression
Progress

719 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