How to find element number in datarow.itemarray

Hi,
I am writting to a text file from a dataset. I am using nested for loops. One to go through the data table rows. the other objects in each row's itemarray. However I don't to write the first, second  or last object
I am not sure how to check for these and skip them in the loop
//C# code
StreamWriter sw; //create a writer and open the file
sw = File.CreateText("C://EDI//GeneratedInvoices//" + genInvoiceNo + ".txt");
sw.WriteLine("*BEG**CONOIIFVYY*"); //preset
foreach (DataRow dr in  objDS_Detail.Tables[0].Rows)
{
 StringBuilder builder = new StringBuilder( ); 	
 builder.Append('D');
 foreach(object o  in dr.ItemArray)
{
  if (??? not element  0 or 1 or last element)
  {
   builder.Append('"');
   builder.Append( o.ToString() );
   builder.Append('"');
   builder.Append( ',' );
  }
 }
 builder.Remove( builder.Length-1, 1);
 sw.WriteLine( builder.ToString() );
}
sw.WriteLine("*END*"); //preset 
sw.Close(); // close the stream
}

Open in new window

StrychturAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JimBrandleyCommented:
This should do it for you.

Jim

StreamWriter sw; //create a writer and open the file
sw = File.CreateText("C://EDI//GeneratedInvoices//" + genInvoiceNo + ".txt");
sw.WriteLine("*BEG**CONOIIFVYY*"); //preset
int columns = objDS_Detail.Tables[0].Columns.Count;
foreach (DataRow dr in  objDS_Detail.Tables[0].Rows)
{
 StringBuilder builder = new StringBuilder( );  
 builder.Append('D');
 for(int i = 2; i < columns - 1; i++)
 {
      builder.Append('"');
      builder.Append( dr[i].ToString() );
      builder.Append('"');
      builder.Append( ',' );
 }
 builder.Remove( builder.Length-1, 1);
 sw.WriteLine( builder.ToString() );
}
sw.WriteLine("*END*"); //preset 
sw.Close(); // close the stream
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
StrychturAuthor Commented:
Thank you very much.
0
JimBrandleyCommented:
My pleasure. Good luck.

Jim
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.