Attempt to write the DataRow content to Text file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • madankarmukta
    Contributor
    • Apr 2008
    • 308

    #1

    Attempt to write the DataRow content to Text file

    Hi,

    Objectives is to write the DataTable content to the text file in the format
    Code:
    Col1  |  Col2 | Col3
    ------|-------|---------
    Val11 | Val21 | Val31
    Val12 | Val22 | Val32
    Val13 | Val23 | Val33
    (Same way as they appear in DataTable)

    I tried this by iterating through each Row in the table.While writing the DataRow content to the text file, the code I wrote is as -
    Code:
    strArray = new string[row.ItemArray.Length];
    row.ItemArray.CopyTo(strArray,0); //<---- error occurs here
    string rowForTextFile = string.Join("\t", (string[])row.ItemArray);
    //functionality to write the the rowForTextFile  to text file
    But the error incurred @ above italicized line.The error was
    "The error was At least one element in the source array could not be cast down to the destination array type"

    Later it is occured to me that of the columns Col1,Col2,Col3 - Col3 is integer.Hence the error.

    In order to join separate strings (Call to string.Join() ) I need to copy the object array to string array (Call to row.ItemArray.C opyTo() ) .Is there any solution over this..?

    I could have achived the result by iterating through Datacolumn of the datarow.. But I believe that will consume time if the rows count to a lacs or two.Hence I tried implementing it this way.

    Please provide inputs.

    Thanks!
    Last edited by Frinavale; Nov 23 '09, 04:41 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Why are you jumping through all these hoops?
    Just loop through your data printing it out (use the .ToString() function)

    Comment

    • madankarmukta
      Contributor
      • Apr 2008
      • 308

      #3
      Originally posted by Plater
      Why are jumping through all these hoops?
      Just loop through your data printing it out (use the .ToString() function)
      Hi,
      Thanks for the inputs!

      Just to make sure, I am jumping through the necessary rows only.I baffled as you directed to use ToString() function.

      I believe use of ToString() will not skip iterations through 49-50 fields of a Datarow.Skiping these iterations may improve performance.

      Thanks!

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        You could just loop through the columns in the DataTable that the DataRow belongs to. For each column, grab the item in the DataRow and use the toString() method for each of them to output the value to a TextFile...

        Code:
        DataTable table = theDataRow.Table;
        
        foreach(DataColumn column in table.Columns)
        {        
        //  theDataRow.Item[column.ColumnName] .......
        }

        Comment

        Working...