?
Solved

read a file and get a field

Posted on 2011-09-15
4
Medium Priority
?
205 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 252 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 248 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
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.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

770 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