Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

VB.NET Dataset datarow output.

Posted on 2007-07-24
9
Medium Priority
?
891 Views
Last Modified: 2008-01-09
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.
0
Comment
Question by:jfsedlar3rd
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 18

Expert Comment

by:DarrenD
ID: 19556479
Hi,

Could you just save the dataset as XML?
0
 

Author Comment

by:jfsedlar3rd
ID: 19556501
No, the file is a fixed length delimitation that I will be providing to an outside company.
0
 
LVL 18

Assisted Solution

by:DarrenD
DarrenD earned 200 total points
ID: 19556541
Are you saying that the 166 columns are not available to you? That you can't access some of them?

If not then iterate through each row and write out each column seperately. If you need to write out all of the cols then you could just use the position instead of the column name.

Also use StringBuilder

Darren
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 96

Expert Comment

by:Bob Learned
ID: 19556601
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
0
 

Author Comment

by:jfsedlar3rd
ID: 19556621
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?
0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 600 total points
ID: 19556663
StringBuilder.Append not 'add'.  You are reference Item(j), with 'j' incrementing in the loop, but that is not necessary.  Also, you are looping through the rows with 'dr', so use that.

For each dr in ds.tables(0).rows
     For Each DSColumn In ds.Tables(0).Columns
          stringBuilder.add(dr(dSColumn).ToString())
     Next DSColumn
Next dr

Bob
0
 
LVL 27

Accepted Solution

by:
VBRocks earned 1200 total points
ID: 19556670
Here's an example of how you can do this:

        Dim dt As New DataTable()

        'Add 166 columns to DataTable
        Dim col As DataColumn
        For i As Int16 = 1 To 166
            col = New DataColumn(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 As Int16 = 1 To 50
            row = dt.NewRow()

            For Each col In dt.Columns
                'Insert row number and column number (Ordinal)
                row.Item(col.Ordinal) = "Row " & i.ToString & " & Column " & (col.Ordinal + 1)

            Next

            dt.Rows.Add(row)
        Next


        'Create a file to output to
        Dim filename As String
        filename = System.IO.Path.ChangeExtension(My.Computer.FileSystem.GetTempFileName(), "txt")

        'Open the file for output
        Dim sw As New IO.StreamWriter(filename)

        'Write each row out to a file
        For Each row In dt.Rows
            sw.WriteLine(Join(row.ItemArray, ","))

        Next

        sw.Close()


        'Open the file to view it
        System.Diagnostics.Process.Start(filename)

0
 

Author Comment

by:jfsedlar3rd
ID: 19556949
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()
0
 
LVL 27

Expert Comment

by:VBRocks
ID: 19557151
Awesome!  I'm glad I could help.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

580 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