Asp.Net News
Free Tech Magazines
Asp.Net Web Hosting
Suggest Us
Link to Us
Feeds Subscription
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;

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();
dataGridView1.DataSource = ds;
dataGridView1.DataMember = ds.Tables[0].TableName;

Post Your Reply Post New Question

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

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
Many .NET teams are underperforming, is yours?
When Good HTML Goes Bad: How You Can Protect Your Web Applications
Web Application Security — How to Minimize Prevalent Risk of Attacks