Link to home
Start Free TrialLog in
Avatar of jfsedlar3rd
jfsedlar3rdFlag for United States of America

asked on

VB.NET Dataset datarow output.

Please help I am having a memory issue at the moment. I am writing a vb.net program using .NET 1.1 VS 2003. I have a dataset that contains multiple rows and about 166 columns. This information needs to be written to a text file. My question is how do I write each entire data row to the text file? For some reason i cannot get my code to return me the entire row.
Avatar of Darren
Darren
Flag of Ireland image

Hi,

Could you just save the dataset as XML?
Avatar of jfsedlar3rd

ASKER

No, the file is a fixed length delimitation that I will be providing to an outside company.
SOLUTION
Avatar of Darren
Darren
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Dim sb As New StringBuilder()
For Each dr As DataRow In dt.Rows
   For Each dc As DataColumn In dt.Columns
      sb.Append(dr(dc).ToString)
   Next dc
   ' Write string builder to file.
Next dr

Bob
Good point, I think that is what i have done in the past: Example

For each dr in ds.tables(0).rows

          For Each DSCoulumn In ds.Tables(0).Columns
                   stringBuilder.add(ds.Tables(0).Rows(i).Item(j).ToString())
                    j += 1
                Next DSCoulumn
i+=1
Next

Am I on the right track?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I was able to resolve this by using VBRocks code and making a few minor modifications.

  Dim dt As New DataTable
            Dim j As Integer
            Dim dr As DataRow

            Dim col As DataColumn
            For i = 0 To ds.Tables(0).Columns.Count - 1
                col = New DataColumn(ds.Tables(0).Columns(i).ToString)
                dt.Columns.Add(col)
            Next

            'Add 50 rows of data to the DataTable, and populate every column
            Dim row As DataRow
            For i = 0 To ds.Tables(0).Rows.Count - 1
                row = dt.NewRow()
                For Each col In dt.Columns
                    'Insert row number and column number (Ordinal)
                    row.Item(col.Ordinal) = ds.Tables(0).Rows(i).Item(col.Ordinal)
                Next
                dt.Rows.Add(row)
            Next

            'Write each row out to a file
            For Each row In dt.Rows
                oWrite.WriteLine(Join(row.ItemArray))
            Next

            oWrite.Close()
Awesome!  I'm glad I could help.