-1

I am new at C#.net. Could somebody help me with the following issue? Thank you.

I need to read the content from a file, then check each row of the file for data that are separated by ":" or ",". Then get the data that is between ":" and ",". Finally add it to the datatable.

How do I do this? Any help is highly appreciated.

Maraduarz
  • 59
  • 1
  • 5
  • Have you seen [this post](http://stackoverflow.com/a/736647/1370166)? It is in the visual basic namespace, but don't let that deter you. It can be used in C#. – TylerOhlsen Oct 15 '12 at 16:46
  • i just went through it, I am having hard time doing it in c#. thank you though – Maraduarz Oct 15 '12 at 16:48

2 Answers2

0

Given the generality and overall scope of your question (ie, you should break it up into the parts you don't understand and ask them individually), this is best I could come up with that could do what you want.

var data = File.ReadLines() // read the content from a file
               .Where(line => line.Contains(":") && line.Contains(",") // data separated by ":" & ","
               .Select(line => line.Split(":,".ToArray())[1])  // data between ":" & "," -- could yield data between "," and ":"
               .Select(data => new object[] {data}); // for DataTable.Rows.Add

// I can only assume you have a DataTable with one column
foreach(var rowData in data)
   yourTable.Rows.Add(rowData);

Hopefully this inspires you.

Austin Salonen
  • 49,173
  • 15
  • 109
  • 139
  • thank you for suggestion. I am still new at this, just started learning and working from this week. I already have a datatable with multiple columns but i guess your step to add rows will works. I will try the rest. Thank you. – Maraduarz Oct 15 '12 at 17:04
0
String linestring = streamreader.ReadLine();
String[] linetokens = linestring.Split(new String[]{":",","}, StringSplitOptions.None);

After that linetokens array will be filled with the segments you need.

Wanabrutbeer
  • 676
  • 6
  • 11
  • do i use this inside the loop when i read individual lines/rows? – Maraduarz Oct 15 '12 at 16:57
  • yes, perhaps a while loop that will end at EOF, or first call streamReader.ReadLines() to get an array to step through, then you could use foreach loop, something like this – Wanabrutbeer Oct 15 '12 at 16:59
  • thank you, with some tweak, your suggestion helped me get done what i was doing. – Maraduarz Oct 18 '12 at 17:11
  • are you familiar or have used POP3 to connect to gmail?? If you are could you help me? I am at a point where i have connected to the gmail and logged-in. Now, I am not sure how to check for new email and read the body of that email. (My plan is to delete every email after i have read the body so that I am checking only for one email/first email). – Maraduarz Oct 19 '12 at 15:24