Creating an In Memory Dataset

Posted on 2009-02-17
Last Modified: 2012-05-06
I have two datasets:
ds2 ->Blank Dataset

I am looping through ds1 and want to place all the unchanged rows from ds1 into the new dataset ds2. ds2 resides only in memory and I am using it to validate data in a database table. Both datasets have the same columns. All i need is to get the rows from ds1 that are unchanged and put them in the new dataset. This seems like it would be easy but im pulling my hair out over here :-(

I am using the following code:

ds2.Tables("ORIGINAL").Columns.Add ("COL1")
ds2.Tables("ORIGINAL").Columns.Add ("COL2")
ds2.Tables("ORIGINAL").Columns.Add ("COL3")
ds2.Tables("ORIGINAL").Columns.Add ("COL4")

For intCount = 0 to ds1.Tables(0).Rows.Count - 1
     If ds1.Tables(0).Rows(intCount).RowState  = DataRowState.Unchanged Then
      End If

it seems simple enough but i am missing something here
Question by:imonfireDAMMIT
    LVL 1

    Expert Comment

    What is the result of this code when you run it?
    LVL 1

    Author Comment

    It returns blank rows without the data filled in
    LVL 69

    Expert Comment

    by:Éric Moreau
    You need to create a DataTable object first and then add it to your dataset. Here is an example I have:

        Private Sub CreateDataSet()
            'Creates a new dataset, 2 datatables and adds data to them
            mdsData = New DataSet("TestDataSet")
            Using dt As New DataTable("Countries")
                dt.Columns.Add("ID", GetType(Integer))
                dt.Columns.Add("Description", GetType(String))
                dt.LoadDataRow(New Object() {1, "Canada"}, True)
                dt.LoadDataRow(New Object() {2, "United States"}, True)
            End Using
            Using dt As New DataTable("States")
                dt.Columns.Add("ID", GetType(Integer))
                dt.Columns.Add("IDParent", GetType(Integer))
                dt.Columns.Add("Code", GetType(String))
                dt.Columns.Add("Description", GetType(String))
                dt.Columns.Add("Population", GetType(Integer))
                dt.LoadDataRow(New Object() {1, 1, "AB", "Alberta", 3153700}, True)
                dt.LoadDataRow(New Object() {2, 1, "BC", "British Columbia", 4146600}, True)
                dt.LoadDataRow(New Object() {3, 1, "MB", "Manitoba", 1162800}, True)
                dt.LoadDataRow(New Object() {4, 1, "NB", "New Brunswick", 750600}, True)
                dt.LoadDataRow(New Object() {5, 1, "NF", "Newfoundland", 519600}, True)
                dt.LoadDataRow(New Object() {6, 1, "NT", "Northwest Territories", 41900}, True)
                dt.LoadDataRow(New Object() {7, 1, "NS", "Nova Scotia", 936000}, True)
                dt.LoadDataRow(New Object() {8, 1, "NU", "Nunavut", 29400}, True)
                dt.LoadDataRow(New Object() {9, 1, "IPE", "Prince Edward Island", 137800}, True)
                dt.LoadDataRow(New Object() {10, 1, "ON", "Ontario", 12238300}, True)
                dt.LoadDataRow(New Object() {11, 1, "QC", "Québec", 7487200}, True)
                dt.LoadDataRow(New Object() {12, 1, "SK", "Saskatchewan", 994800}, True)
                dt.LoadDataRow(New Object() {13, 1, "YK", "Yukon", 31100}, True)
                dt.LoadDataRow(New Object() {14, 2, "CA", "California", 33872000}, True)
                dt.LoadDataRow(New Object() {15, 2, "TX", "Texas", 20852000}, True)
                dt.LoadDataRow(New Object() {16, 2, "NY", "New York", 18976000}, True)
                dt.LoadDataRow(New Object() {17, 2, "FL", "Florida", 15982000}, True)
                dt.LoadDataRow(New Object() {18, 2, "MA", "Maine", 1275000}, True)
                dt.LoadDataRow(New Object() {19, 2, "CO", "Colorado", 4301000}, True)
                dt.LoadDataRow(New Object() {20, 2, "NH", "New Hampshire", 1236000}, True)
            End Using
          End Sub

    Open in new window

    LVL 69

    Accepted Solution

    Your Add method is not adding any data!

    You should try the LoadDataRow passing ds1.Tables(0).Rows(intCount) in parameters.
    LVL 1

    Author Closing Comment

    I actually saw your comment about my add method not adding the data and it raised a flag. I realized my error and changed my code as follows:

    For each dr1 in ds1.Tables(0).Rows
         If dr1.RowState = DataRowState.Unchanged Then
        End If

    Thank you for your time

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now