Solved

How to fill a datatable from a datatable

Posted on 2016-10-04
8
42 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How do ASP.NET and MVC work together? 4 25
Problem to error 4 43
how to read json value 2 29
Need a starter for ETL protocol? 4 38
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

919 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

18 Experts available now in Live!

Get 1:1 Help Now