I'm relatively new to programming.

Problem:
I'm creating an ASP.NET app the needs to read a CSV file and filter the data.

I searched the net and only found classic ASP examples or all DB driven examples.

Does anyone have sample code. Or is there any other suggestions on how to read and filter. Thanks

c# code

using System.Data;

//in some function
System.Data.Odbc.OdbcConnection conn;
DataTable dt;
System.Data.Odbc.OdbcDataAdapter da;
string connectionString;
string importFolder;
string fileName;

importFolder = @"c:\importfile" //or pass it in -this is the folder in which the csv file is in

fileName = "csvimport.csv" //or pass it in -this is the csv file to be imported

connectionString= @"Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + importFolder + ";";
conn = new Odbc.OdbcConnection(connectionString)

//we only pass it the folder.  The csv file import is in the query which follows

da = new System.data.Odbc.OdbcDataAdapter("select * from [" + strFileName + "]", conn);
da.Fill(dt);

//Your table is filled and uses the first row of the csv file as the column headings.

Hope it helps :)

Thanks for the code f1_fan.

I'll try to work with the code.

Does anyone have a VB example as well.

I will change it to vb but bear with me as my vb is rusty to say the least.

imports System.Data;

//in some function
Dim conn As System.Data.Odbc.OdbcConnection 
Dim dt As DataTable
Dim da As System.Data.Odbc.OdbcDataAdapter;
Dim connectionString As string
Dim importFolder As string
Dim fileName As string

importFolder = "c:\importfile" //or pass it in -this is the folder in which the csv file is in

fileName = "csvimport.csv" //or pass it in -this is the csv file to be imported

connectionString= "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + importFolder + ";"
conn = new Odbc.OdbcConnection(connectionString)

//we only pass it the folder.  The csv file import is in the query which follows

da = new System.data.Odbc.OdbcDataAdapter("select * from [" + strFileName + "]", conn);
da.Fill(dt);

//Your table is filled and uses the first row of the csv file as the column headings.

Thank you for the code, it really helped me
:)

c# code

using System.Data;

//in some function
System.Data.Odbc.OdbcConnection conn;
DataTable dt;
System.Data.Odbc.OdbcDataAdapter da;
string connectionString;
string importFolder;
string fileName;

importFolder = @"c:\importfile" //or pass it in -this is the folder in which the csv file is in

fileName = "csvimport.csv" //or pass it in -this is the csv file to be imported

connectionString= @"Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + importFolder + ";";
conn = new Odbc.OdbcConnection(connectionString)

//we only pass it the folder.  The csv file import is in the query which follows

da = new System.data.Odbc.OdbcDataAdapter("select * from [" + strFileName + "]", conn);
da.Fill(dt);

//Your table is filled and uses the first row of the csv file as the column headings.

Hope it helps :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.