Solved

read a file and get a field

Posted on 2011-09-15
4
201 Views
Last Modified: 2012-05-12
Hi Experts,

I am new to C#, any help would be appreciated.

I want to know how to get a field from a file. for example

my file has content:

col1,col2,col3,col4,col5
aaa,bbb,ccc,ddd,eee
111,222,333,444,555
AAA,BBB,CCC,DDD,EEE

How can I display col3?

Thanks


0
Comment
Question by:justinY
  • 2
4 Comments
 
LVL 22

Accepted Solution

by:
pivar earned 63 total points
ID: 36546186
Hi,

You could, for instance, use ReadLine and Split

System.IO.StreamReader file = new System.IO.StreamReader("c:\\file.txt");
string line;

while((line = file.ReadLine()) != null)
{
   string[] cols = line.Split(',');
   Console.WriteLine(cols[2]);
}

file.Close();
0
 
LVL 12

Assisted Solution

by:rajapandian_81
rajapandian_81 earned 62 total points
ID: 36547372
Hi,

Try below code. I have stored my text file in c drive with name "Test.txt". I will display the col3 in dataGridView.

string strFileName = "Test.txt";
string strDirName = "c:\\";
DataTable dt;

using (OleDbConnection con =
       new OleDbConnection(@"Provider=Microsoft.Jet.OleDb.4.0;" +
       "Data Source=" + strDirName + ";" +
       "Extended Properties=\"Text;HDR=Yes;FMT=Delimited\""))
{
	con.Open();

        using (OleDbDataAdapter adapter =
               new OleDbDataAdapter("SELECT * FROM " + strFileName, con))
        {
        	dt = new DataTable("Customer");
                adapter.Fill(dt);
        }
}
            
dataGridView1.DataSource = dt.DefaultView.ToTable(false, new string[] { "col3" });

Open in new window

0
 

Author Comment

by:justinY
ID: 36550354
rajapandian_81,

I got this error

Error1      The name 'dataGridView1' does not exist in the current context      
0
 
LVL 12

Expert Comment

by:rajapandian_81
ID: 36558261
Hi,

Add dataGridView to your form from toolbox and name it as dataGridView1.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

775 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