[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

How to fill a datatable from a datatable

Posted on 2016-10-04
8
Medium Priority
?
67 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 27

Assisted Solution

by:Shaun Kline
Shaun Kline earned 500 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 13

Assisted Solution

by:Dustin Saunders
Dustin Saunders earned 500 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 1000 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
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
 
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 13

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 13

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

656 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