Solved

read a file and get a field

Posted on 2011-09-15
4
199 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Hi,

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

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now