AspdotnetCodes.com
Search
Articles
Books
Resources
Asp.Net News
Downloads
Free Tech Magazines
Asp.Net Web Hosting
Archives
Softwares
Newsletter
Suggest Us
Link to Us
Feeds Subscription
Articles
Questions & Answers
Tips & Tricks
 trying to read .txt file,getting unspecified error
Posted by jyothi bunny on  6/22/2010 1:42:46 PM Category: C#
Add to my favorites
Email to friend
Hi , i am trying to read .txt file in c# using oledb connection .
my file name is emp.txt and i wrote schema.ini file also .after debuging this code i am getting unspecified error in ad.fill(ds); i included namespace using system.data.oledb;

Plz anybody help me to correct it or is there any other code in c#?

OleDbConnection con;

//OleDbCommand cmd;
OleDbDataAdapter ad;
con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\text files folder\;Extended Properties='text;HDR=Yes;FMT=Fixed'");
ad = new OleDbDataAdapter("select * from emp.txt", con);
DataSet ds = new DataSet();
ad.Fill(ds);
dataGridView1.DataSource = ds;
dataGridView1.DataMember = ds.Tables[0].TableName;

Post Your Reply Post New Question

 Viewer's Reply
Reading text file in asp.net and Show in GridView control
Answered By Moderator1 on 6/22/2010 10:16:43 PM
Hi,

To read a text file, you no need to use OLEDB connection. You can read through StreamReader object as below,

StreamReader sr = File.OpenText("C:\\text files folder\\emp.txt");
string content = sr.ReadToEnd();

Then you need to split the content based on the delimiter, then fill the values in a datatable, then you can bind with GridView control.
 
Post Your Reply If you still have problem, post it again under this reply for better response.

 
Sponsored by
Backup and Recovery Best Practices for Microsoft SQL Server 2005
Sexy Web Design - Free 71 Page Preview!
The PHP Anthology: 101 Essential Tips, Tricks & Hacks, 2nd Edition - Free 207 Page Preview!