Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

read a file and get a field

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

604 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