Solved

read a file and get a field

Posted on 2011-09-15
4
204 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

690 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