[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 69
  • Last Modified:

How to fill a datatable from a datatable

I want to create a datatable (ResultDataTable) in vb.net with a subset of columns and a subset of rows from another datatable (SourceDataTable) so I could pass ResultDataTable to a SQL Server stored procedure.  If  SourceDataTable has columns C1, C2, and C3, how can I create a ResultDataTable that only has the column C1 and only has rows where C3=1?  The SQL Statement from ResultDataTable would be "SELECT C1 from SourceDataTable WHERE C3 = 1".
0
Declan_Basile
Asked:
Declan_Basile
3 Solutions
 
Shaun KlineLead Software EngineerCommented:
You can create a DataView from your existing table that filters the rows based on your criteria. Once the DataView object is created, you can use the ToTable() methods to create a new table. One of the overloaded methods for ToTable takes a string array of column names you want in the table.

Below is a link to the ToTable method of the DataView object:
https://msdn.microsoft.com/en-us/library/wec2b2e6(v=vs.110).aspx
0
 
Dustin SaundersDirector of OperationsCommented:
Your selection result should have the .CopyToDataTable() method.  So for example

ResultDataTable = SelectionResult.CopyToDataTable()
0
 
ROMA CHAUHANProject LeadCommented:
Hi Declan_Basile,

I think solution provided in the link shared by Shaun Kline, will not give you the exact solution for which you are looking.

You can try out the below method. I referred the same link but made some changes as per your need.

Private Sub GetResultTableFrmSrcTable()

        Dim SourceDataTable As DataTable = New DataTable("SourceDataTable")

        Dim column As DataColumn = New DataColumn("C1", GetType(System.String))
        SourceDataTable.Columns.Add(column)

        column = New DataColumn("C2", GetType(System.String))
        SourceDataTable.Columns.Add(column)

        column = New DataColumn("C3", GetType(System.Int32))
        SourceDataTable.Columns.Add(column)


        ' Add some items.
        Dim row As DataRow = SourceDataTable.NewRow()
        row.ItemArray = New Object() {"Apple", "Fruit", 1}
        SourceDataTable.Rows.Add(row)

        row = SourceDataTable.NewRow()
        row.ItemArray = New Object() {"Orange", "Fruit", 1}
        SourceDataTable.Rows.Add(row)

        row = SourceDataTable.NewRow()
        row.ItemArray = New Object() {"Muffin", "Bread", 2}
        SourceDataTable.Rows.Add(row)

        row = SourceDataTable.NewRow()
        row.ItemArray = New Object() {"Salmon", "Fish", 3}
        SourceDataTable.Rows.Add(row)

        row = SourceDataTable.NewRow()
        row.ItemArray = New Object() {"Salmon", "Fish", 3}
        SourceDataTable.Rows.Add(row)

        row = SourceDataTable.NewRow()
        row.ItemArray = New Object() {"Croissant", "Bread", 2}
        SourceDataTable.Rows.Add(row)

        ' Filter source table where C3=1
        SourceDataTable.DefaultView.RowFilter = "C3=1"

        'Get C1 column from dataview
        Dim ResultDataTable As DataTable = SourceDataTable.DefaultView.ToTable(False, "C1")

    End Sub

Open in new window

0
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

 
Declan_BasileAuthor Commented:
Thanks everyone.  I tried both methods and both worked.  I went with the Dataview method because I found it easier.  Thank you Roma for making it even easier by providing the example.
0
 
Declan_BasileAuthor Commented:
I see a close request, but I thought that I already assigned points and closed this question.  Also, I don't see any "Accept as Answer" buttons.  Can someone please advise?
0
 
Dustin SaundersDirector of OperationsCommented:
Ah, the question was left open, so it notifies experts to suggest a closing to the question to keep the queue of open questions managed.  

The question will auto-close itself once an expert has made a suggestion for closing- but as the author if you want to change the distribution of points or select just one answer, open a new comment and instead of clicking 'Submit' choose 'Object'.  

I'm unsure if when the author objects it will automatically accept the results, but if not then the question will auto-close after a few days with whatever your new solution selection is.
0
 
Declan_BasileAuthor Commented:
OK.  I'll just let this auto-close then.  Thanks.
0
 
Dustin SaundersDirector of OperationsCommented:
Author suggested both approaches worked, cited the answer from Roma to be the most robust.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now