Solved

How to fill a datatable from a datatable

Posted on 2016-10-04
8
36 Views
Last Modified: 2016-10-25
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
Comment
Question by:Declan_Basile
8 Comments
 
LVL 26

Assisted Solution

by:Shaun Kline
Shaun Kline earned 125 total points (awarded by participants)
ID: 41828622
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
 
LVL 12

Assisted Solution

by:Dustin Saunders
Dustin Saunders earned 125 total points (awarded by participants)
ID: 41828628
Your selection result should have the .CopyToDataTable() method.  So for example

ResultDataTable = SelectionResult.CopyToDataTable()
0
 
LVL 11

Accepted Solution

by:
ROMA CHAUHAN earned 250 total points (awarded by participants)
ID: 41829658
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
 
LVL 1

Author Comment

by:Declan_Basile
ID: 41831752
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 1

Author Comment

by:Declan_Basile
ID: 41852367
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
 
LVL 12

Expert Comment

by:Dustin Saunders
ID: 41852457
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
 
LVL 1

Author Comment

by:Declan_Basile
ID: 41854457
OK.  I'll just let this auto-close then.  Thanks.
0
 
LVL 12

Expert Comment

by:Dustin Saunders
ID: 41858243
Author suggested both approaches worked, cited the answer from Roma to be the most robust.
0

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.

Join & Write a Comment

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

10 Experts available now in Live!

Get 1:1 Help Now