IT Community - Software Programming, Web Development and Technical Support

How to import CSV formatted file using asp.net/c# ?

This is a discussion on How to import CSV formatted file using asp.net/c# ? within the ASP and ASP.NET Programming forums, part of the Web Development category; Hi how to import CSV formatted file using asp.net/c# ? Thnx...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > ASP and ASP.NET Programming

Register FAQ Members List Calendar Mark Forums Read
  3 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 08-07-2007, 12:45 AM
KiruthikaSambandam KiruthikaSambandam is offline
D-Web Analyst
 
Join Date: Aug 2007
Posts: 332
KiruthikaSambandam is on a distinguished road
Default How to import CSV formatted file using asp.net/c# ?

Hi
how to import CSV formatted file using asp.net/c# ?

Thnx
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-08-2007, 04:05 AM
vadivelanvaidyanathan vadivelanvaidyanathan is offline
D-Web Genius
 
Join Date: Feb 2007
Posts: 803
vadivelanvaidyanathan is on a distinguished road
Default Re: How to import CSV formatted file using asp.net/c# ?

See here you get solutions
__________________
V.Vadivelan

There never a wrong time to do the right thing.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 08-08-2007, 05:24 AM
krishnakumar krishnakumar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 206
krishnakumar is on a distinguished road
Default Re: How to import CSV formatted file using asp.net/c# ?

Hi,
I came to know about CSV Formatted file in c# can import and saved it in database...
private void writeSchema()
{
try
{
FileStream fsOutput = new FileStream(txtCSVFolderPath.Text+"\\schema.ini",Fi leMode.Create, FileAccess.Write);
StreamWriter srOutput = new StreamWriter (fsOutput);
string s1,s2,s3,s4,s5;
s1="["+strCSVFile+"]";
s2="ColNameHeader="+bolColName.ToString();
s3="Format="+strFormat;
s4="MaxScanRows=25";
s5="CharacterSet=OEM";
srOutput.WriteLine(s1.ToString()+'\n'+s2.ToString( )+'\n'+s3.ToString()+'\n'+s4.ToString()+'\n'+s5.To String());
srOutput.Close ();
fsOutput.Close ();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{}
}


//Function For Importing Data From CSV File
public DataSet ConnectCSV(string filetable)
{
DataSet ds = new DataSet();
try
{
// You can get connected to driver either by using DSN or connection string

// Create a connection string as below, if you want to use DSN less connection. The DBQ attribute sets the path of directory which contains CSV files
string strConnString="Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq="+txtCSVFolderPath.Text.Trim()+";Exten sions=asc,csv,tab,txt;Persist Security Info=False";
string sql_select;
System.Data.Odbc.OdbcConnection conn;

//Create connection to CSV file
conn = new System.Data.Odbc.OdbcConnection(strConnString.Trim ());

// For creating a connection using DSN, use following line
//conn = new System.Data.Odbc.OdbcConnection(DSN="MyDSN");

//Open the connection
conn.Open();
//Fetch records from CSV
sql_select = "select * from ["+ filetable +"]";

obj_oledb_da = new System.Data.Odbc.OdbcDataAdapter(sql_select,conn);
//Fill dataset with the records from CSV file
obj_oledb_da.Fill(ds,"Stocks");

//Set the datagrid properties

dGridCSVdata.DataSource=ds;
dGridCSVdata.DataMember="Stocks";
//Close Connection to CSV file
conn.Close();
}
catch(Exception e) //Error
{
MessageBox.Show(e.Message);
}
return ds;
}


//Button Import CSV Data
private void btnImport_Click(object sender, System.EventArgs e)
{
try
{
if(txtCSVFolderPath.Text=="")
{
MessageBox.Show("The Folder Path TextBox cannot be empty.","Warning");
return;
}
else if(txtCSVFilePath.Text=="")
{
MessageBox.Show("The File Path TextBox cannot be empty.","Warning");
return;
}
else if(txtDelimiter.Text=="\"")
{
MessageBox.Show("You cannot specify (\") as a delimeter.","Warning");
return;
}
else
{
int intLengthOfFileName=txtCSVFilePath.Text.Trim().Len gth;
int intLastIndex=txtCSVFilePath.Text.Trim().LastIndexO f("\\");
strCSVFile=txtCSVFilePath.Text.Trim().Substring(in tLastIndex,intLengthOfFileName-intLastIndex);
strCSVFile=strCSVFile.Remove(0,1).Trim();
Format();
writeSchema();
ConnectCSV(strCSVFile);
btnUpload.Enabled=true;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{}
}


It may usefull for u.....
Hope u get satisfied..
__________________
Krishnakumar.S
Beware of Everything -that is un true; stick to the Truth shall succeed slowly but steadily
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 08-08-2007, 05:27 AM
krishnakumar krishnakumar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 206
krishnakumar is on a distinguished road
Wink Re: How to import CSV formatted file using asp.net/c# ?

Hi,


Refer this siteCSV

I hope that it may useful for u... U can get some idea about CSV...
__________________
Krishnakumar.S
Beware of Everything -that is un true; stick to the Truth shall succeed slowly but steadily
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 08-09-2007, 07:48 AM
GDevakii GDevakii is offline
D-Web Sr.Programmer
 
Join Date: Aug 2007
Posts: 138
GDevakii is on a distinguished road
Default Re: How to import CSV formatted file using asp.net/c# ?

private void OnExportGridToCSV(object sender, System.EventArgs e)
{
// Create the CSV file to which grid data will be exported.
StreamWriter sw = new StreamWriter(Server.MapPath("~/GridData.txt"), false);

DataTable dt = m_dsProducts.Tables[0];
int iColCount = dt.Columns.Count;
for(int i = 0; i < iColCount; i++)
{
sw.Write(dt.Columns[i]);
if (i < iColCount - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
// Now write all the rows.
foreach (DataRow dr in dt.Rows)
{
for (int i = 0; i < iColCount; i++)
{
if (!Convert.IsDBNull(dr[i]))
{
sw.Write(dr[i].ToString());
}
if ( i < iColCount - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
}
sw.Close();
}
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 08-16-2007, 10:03 AM
H2o H2o is offline
D-Web Analyst
 
Join Date: Jul 2007
Posts: 246
H2o is on a distinguished road
Thumbs up Re: How to import CSV formatted file using asp.net/c# ?

Hi

I got error for import csv, that csv included for one empty cell, How can i import csv, pls gimme any idea.




thanks
__________________
H2O

Without us, no one can survive..
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

LinkBacks (?)
LinkBack to this Thread: http://www.discussweb.com/asp-asp-net-programming/3186-how-import-csv-formatted-file-using-asp-net-c.html
Posted By For Type Date
import csv asp.net - Dot Net Search Engine swicki - powered by eurekster This thread Refback 01-14-2008 12:37 AM
datatable to csv in C# - Dot Net Search Engine swicki - powered by eurekster This thread Refback 01-09-2008 01:59 PM
datatable to csv in C# - Dot Net Search Engine swicki - powered by eurekster This thread Refback 12-12-2007 07:27 AM

Similar Threads
Thread Thread Starter Forum Replies Last Post
import http CSV or TXT to database ??? muller Database Support 0 03-25-2008 04:50 AM
File extension of the code file & object repository file in QTP vigneshgets Testing Tools 1 01-16-2008 11:43 PM
How do we import or export data in MySql Jeyaseelansarc Database Support 8 08-17-2007 07:25 AM
How can we import and export using BCP utility in SQL? oxygen Database Support 1 07-26-2007 04:33 AM
How to import MySQL Dump files? kingmaker Database Support 2 07-24-2007 04:47 AM


All times are GMT -7. The time now is 08:32 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0