How to load data from SQL to datetable that show in treeview












-1















I have a chartTable with 2 columns:



ChildPersonID | ParentPersonID
--------------+-----------------
1 | 2
1 | 3
2 | 4


That is joined to personTable with 2 columns:



ID | PersonName
---+-----------------
1 | a
2 | b
3 | c
4 | d


I want a select query that fill datatable with PersonName that show in treeview



Result:



parentname | parentid | childname | childid
-----------+----------+-----------+---------
a | 1 | b | 2
a | 1 | c | 3
b | 2 | d | 4









share|improve this question




















  • 2





    Did you try writing any code for this? What issue you are facing in that?

    – Chetan Ranpariya
    Jan 2 at 10:15






  • 1





    Join twice! One time for parentname and one time for childname.

    – jarlh
    Jan 2 at 10:16
















-1















I have a chartTable with 2 columns:



ChildPersonID | ParentPersonID
--------------+-----------------
1 | 2
1 | 3
2 | 4


That is joined to personTable with 2 columns:



ID | PersonName
---+-----------------
1 | a
2 | b
3 | c
4 | d


I want a select query that fill datatable with PersonName that show in treeview



Result:



parentname | parentid | childname | childid
-----------+----------+-----------+---------
a | 1 | b | 2
a | 1 | c | 3
b | 2 | d | 4









share|improve this question




















  • 2





    Did you try writing any code for this? What issue you are facing in that?

    – Chetan Ranpariya
    Jan 2 at 10:15






  • 1





    Join twice! One time for parentname and one time for childname.

    – jarlh
    Jan 2 at 10:16














-1












-1








-1








I have a chartTable with 2 columns:



ChildPersonID | ParentPersonID
--------------+-----------------
1 | 2
1 | 3
2 | 4


That is joined to personTable with 2 columns:



ID | PersonName
---+-----------------
1 | a
2 | b
3 | c
4 | d


I want a select query that fill datatable with PersonName that show in treeview



Result:



parentname | parentid | childname | childid
-----------+----------+-----------+---------
a | 1 | b | 2
a | 1 | c | 3
b | 2 | d | 4









share|improve this question
















I have a chartTable with 2 columns:



ChildPersonID | ParentPersonID
--------------+-----------------
1 | 2
1 | 3
2 | 4


That is joined to personTable with 2 columns:



ID | PersonName
---+-----------------
1 | a
2 | b
3 | c
4 | d


I want a select query that fill datatable with PersonName that show in treeview



Result:



parentname | parentid | childname | childid
-----------+----------+-----------+---------
a | 1 | b | 2
a | 1 | c | 3
b | 2 | d | 4






c# sql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 10:34







xxxsenatorxxx

















asked Jan 2 at 10:12









xxxsenatorxxxxxxsenatorxxx

629




629








  • 2





    Did you try writing any code for this? What issue you are facing in that?

    – Chetan Ranpariya
    Jan 2 at 10:15






  • 1





    Join twice! One time for parentname and one time for childname.

    – jarlh
    Jan 2 at 10:16














  • 2





    Did you try writing any code for this? What issue you are facing in that?

    – Chetan Ranpariya
    Jan 2 at 10:15






  • 1





    Join twice! One time for parentname and one time for childname.

    – jarlh
    Jan 2 at 10:16








2




2





Did you try writing any code for this? What issue you are facing in that?

– Chetan Ranpariya
Jan 2 at 10:15





Did you try writing any code for this? What issue you are facing in that?

– Chetan Ranpariya
Jan 2 at 10:15




1




1





Join twice! One time for parentname and one time for childname.

– jarlh
Jan 2 at 10:16





Join twice! One time for parentname and one time for childname.

– jarlh
Jan 2 at 10:16












2 Answers
2






active

oldest

votes


















1














This is the query which you are looking for (though the column name which you have mentioned are confusing, I think it should be reversed)



DECLARE @Table1 TABLE (ChildPersonID  INT,ParentPersonID INT)
DECLARE @Table2 TABLE (ID INT, PersonName VARCHAR(10))

INSERT INTO @Table1 VALUES (1,2),(1,3),(2,4)
INSERT INTO @Table2 VALUES (1,'a'),(2,'b'),(3,'c'),(4,'d')


SELECT T3.PersonName AS parentName, T1.ChildPersonID AS ParentId,
T2.PersonName AS childname, T1.ParentPersonID AS childid
FROM @Table1 T1
INNER JOIN @Table2 T2 ON T1.ParentPersonID = T2.Id
INNER JOIN @Table2 T3 ON T1.ChildPersonID = T3.id





share|improve this answer
























  • Chose table aliases that make sense. E.g. p for parent and c for child.

    – jarlh
    Jan 2 at 10:43











  • how to load this table in radtreeview (telerik treeview) winform?

    – xxxsenatorxxx
    Jan 2 at 11:50



















1














See code below :



using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Data;

namespace ConsoleApplication94
{
class Program
{
static void Main(string args)
{

DataTable dt = new DataTable();
dt.Columns.Add("parentname", typeof(string));
dt.Columns.Add("parentid", typeof(int));
dt.Columns.Add("childname", typeof(string));
dt.Columns.Add("childid", typeof(int));

DataTable dtChildPerson = new DataTable();
dtChildPerson.Columns.Add("ChildPersonID", typeof(int));
dtChildPerson.Columns.Add("ParentPersonID", typeof(int));
dtChildPerson.Rows.Add(new object { 1, 1 });
dtChildPerson.Rows.Add(new object { 1, 3 });
dtChildPerson.Rows.Add(new object { 2, 4 });

DataTable personName = new DataTable();
personName.Columns.Add("ID", typeof(int));
personName.Columns.Add("PersonName", typeof(string));
personName.Rows.Add(new object { 1, "a" });
personName.Rows.Add(new object { 2, "b" });
personName.Rows.Add(new object { 3, "c" });
personName.Rows.Add(new object { 4, "d" });


foreach (DataRow row in dtChildPerson.AsEnumerable())
{
int parentID = row.Field<int>("ParentPersonID");
string parentName = personName.AsEnumerable().Where(x => x.Field<int>("ID") == parentID).Select(x => x.Field<string>("PersonName")).FirstOrDefault();
int childID = row.Field<int>("ChildPersonID");

foreach(DataRow childRow in personName.AsEnumerable().Where(x => x.Field<int>("ID") == childID))
{
string childName = childRow.Field<string>("PersonName");

dt.Rows.Add(new object { parentName, parentID, childName, childID });
}

}
}
}
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54004470%2fhow-to-load-data-from-sql-to-datetable-that-show-in-treeview%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    This is the query which you are looking for (though the column name which you have mentioned are confusing, I think it should be reversed)



    DECLARE @Table1 TABLE (ChildPersonID  INT,ParentPersonID INT)
    DECLARE @Table2 TABLE (ID INT, PersonName VARCHAR(10))

    INSERT INTO @Table1 VALUES (1,2),(1,3),(2,4)
    INSERT INTO @Table2 VALUES (1,'a'),(2,'b'),(3,'c'),(4,'d')


    SELECT T3.PersonName AS parentName, T1.ChildPersonID AS ParentId,
    T2.PersonName AS childname, T1.ParentPersonID AS childid
    FROM @Table1 T1
    INNER JOIN @Table2 T2 ON T1.ParentPersonID = T2.Id
    INNER JOIN @Table2 T3 ON T1.ChildPersonID = T3.id





    share|improve this answer
























    • Chose table aliases that make sense. E.g. p for parent and c for child.

      – jarlh
      Jan 2 at 10:43











    • how to load this table in radtreeview (telerik treeview) winform?

      – xxxsenatorxxx
      Jan 2 at 11:50
















    1














    This is the query which you are looking for (though the column name which you have mentioned are confusing, I think it should be reversed)



    DECLARE @Table1 TABLE (ChildPersonID  INT,ParentPersonID INT)
    DECLARE @Table2 TABLE (ID INT, PersonName VARCHAR(10))

    INSERT INTO @Table1 VALUES (1,2),(1,3),(2,4)
    INSERT INTO @Table2 VALUES (1,'a'),(2,'b'),(3,'c'),(4,'d')


    SELECT T3.PersonName AS parentName, T1.ChildPersonID AS ParentId,
    T2.PersonName AS childname, T1.ParentPersonID AS childid
    FROM @Table1 T1
    INNER JOIN @Table2 T2 ON T1.ParentPersonID = T2.Id
    INNER JOIN @Table2 T3 ON T1.ChildPersonID = T3.id





    share|improve this answer
























    • Chose table aliases that make sense. E.g. p for parent and c for child.

      – jarlh
      Jan 2 at 10:43











    • how to load this table in radtreeview (telerik treeview) winform?

      – xxxsenatorxxx
      Jan 2 at 11:50














    1












    1








    1







    This is the query which you are looking for (though the column name which you have mentioned are confusing, I think it should be reversed)



    DECLARE @Table1 TABLE (ChildPersonID  INT,ParentPersonID INT)
    DECLARE @Table2 TABLE (ID INT, PersonName VARCHAR(10))

    INSERT INTO @Table1 VALUES (1,2),(1,3),(2,4)
    INSERT INTO @Table2 VALUES (1,'a'),(2,'b'),(3,'c'),(4,'d')


    SELECT T3.PersonName AS parentName, T1.ChildPersonID AS ParentId,
    T2.PersonName AS childname, T1.ParentPersonID AS childid
    FROM @Table1 T1
    INNER JOIN @Table2 T2 ON T1.ParentPersonID = T2.Id
    INNER JOIN @Table2 T3 ON T1.ChildPersonID = T3.id





    share|improve this answer













    This is the query which you are looking for (though the column name which you have mentioned are confusing, I think it should be reversed)



    DECLARE @Table1 TABLE (ChildPersonID  INT,ParentPersonID INT)
    DECLARE @Table2 TABLE (ID INT, PersonName VARCHAR(10))

    INSERT INTO @Table1 VALUES (1,2),(1,3),(2,4)
    INSERT INTO @Table2 VALUES (1,'a'),(2,'b'),(3,'c'),(4,'d')


    SELECT T3.PersonName AS parentName, T1.ChildPersonID AS ParentId,
    T2.PersonName AS childname, T1.ParentPersonID AS childid
    FROM @Table1 T1
    INNER JOIN @Table2 T2 ON T1.ParentPersonID = T2.Id
    INNER JOIN @Table2 T3 ON T1.ChildPersonID = T3.id






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 2 at 10:22









    Anuj TripathiAnuj Tripathi

    1,791916




    1,791916













    • Chose table aliases that make sense. E.g. p for parent and c for child.

      – jarlh
      Jan 2 at 10:43











    • how to load this table in radtreeview (telerik treeview) winform?

      – xxxsenatorxxx
      Jan 2 at 11:50



















    • Chose table aliases that make sense. E.g. p for parent and c for child.

      – jarlh
      Jan 2 at 10:43











    • how to load this table in radtreeview (telerik treeview) winform?

      – xxxsenatorxxx
      Jan 2 at 11:50

















    Chose table aliases that make sense. E.g. p for parent and c for child.

    – jarlh
    Jan 2 at 10:43





    Chose table aliases that make sense. E.g. p for parent and c for child.

    – jarlh
    Jan 2 at 10:43













    how to load this table in radtreeview (telerik treeview) winform?

    – xxxsenatorxxx
    Jan 2 at 11:50





    how to load this table in radtreeview (telerik treeview) winform?

    – xxxsenatorxxx
    Jan 2 at 11:50













    1














    See code below :



    using System;
    using System.Collections.Generic;
    using System.Collections;
    using System.Linq;
    using System.Text;
    using System.Data;

    namespace ConsoleApplication94
    {
    class Program
    {
    static void Main(string args)
    {

    DataTable dt = new DataTable();
    dt.Columns.Add("parentname", typeof(string));
    dt.Columns.Add("parentid", typeof(int));
    dt.Columns.Add("childname", typeof(string));
    dt.Columns.Add("childid", typeof(int));

    DataTable dtChildPerson = new DataTable();
    dtChildPerson.Columns.Add("ChildPersonID", typeof(int));
    dtChildPerson.Columns.Add("ParentPersonID", typeof(int));
    dtChildPerson.Rows.Add(new object { 1, 1 });
    dtChildPerson.Rows.Add(new object { 1, 3 });
    dtChildPerson.Rows.Add(new object { 2, 4 });

    DataTable personName = new DataTable();
    personName.Columns.Add("ID", typeof(int));
    personName.Columns.Add("PersonName", typeof(string));
    personName.Rows.Add(new object { 1, "a" });
    personName.Rows.Add(new object { 2, "b" });
    personName.Rows.Add(new object { 3, "c" });
    personName.Rows.Add(new object { 4, "d" });


    foreach (DataRow row in dtChildPerson.AsEnumerable())
    {
    int parentID = row.Field<int>("ParentPersonID");
    string parentName = personName.AsEnumerable().Where(x => x.Field<int>("ID") == parentID).Select(x => x.Field<string>("PersonName")).FirstOrDefault();
    int childID = row.Field<int>("ChildPersonID");

    foreach(DataRow childRow in personName.AsEnumerable().Where(x => x.Field<int>("ID") == childID))
    {
    string childName = childRow.Field<string>("PersonName");

    dt.Rows.Add(new object { parentName, parentID, childName, childID });
    }

    }
    }
    }
    }





    share|improve this answer




























      1














      See code below :



      using System;
      using System.Collections.Generic;
      using System.Collections;
      using System.Linq;
      using System.Text;
      using System.Data;

      namespace ConsoleApplication94
      {
      class Program
      {
      static void Main(string args)
      {

      DataTable dt = new DataTable();
      dt.Columns.Add("parentname", typeof(string));
      dt.Columns.Add("parentid", typeof(int));
      dt.Columns.Add("childname", typeof(string));
      dt.Columns.Add("childid", typeof(int));

      DataTable dtChildPerson = new DataTable();
      dtChildPerson.Columns.Add("ChildPersonID", typeof(int));
      dtChildPerson.Columns.Add("ParentPersonID", typeof(int));
      dtChildPerson.Rows.Add(new object { 1, 1 });
      dtChildPerson.Rows.Add(new object { 1, 3 });
      dtChildPerson.Rows.Add(new object { 2, 4 });

      DataTable personName = new DataTable();
      personName.Columns.Add("ID", typeof(int));
      personName.Columns.Add("PersonName", typeof(string));
      personName.Rows.Add(new object { 1, "a" });
      personName.Rows.Add(new object { 2, "b" });
      personName.Rows.Add(new object { 3, "c" });
      personName.Rows.Add(new object { 4, "d" });


      foreach (DataRow row in dtChildPerson.AsEnumerable())
      {
      int parentID = row.Field<int>("ParentPersonID");
      string parentName = personName.AsEnumerable().Where(x => x.Field<int>("ID") == parentID).Select(x => x.Field<string>("PersonName")).FirstOrDefault();
      int childID = row.Field<int>("ChildPersonID");

      foreach(DataRow childRow in personName.AsEnumerable().Where(x => x.Field<int>("ID") == childID))
      {
      string childName = childRow.Field<string>("PersonName");

      dt.Rows.Add(new object { parentName, parentID, childName, childID });
      }

      }
      }
      }
      }





      share|improve this answer


























        1












        1








        1







        See code below :



        using System;
        using System.Collections.Generic;
        using System.Collections;
        using System.Linq;
        using System.Text;
        using System.Data;

        namespace ConsoleApplication94
        {
        class Program
        {
        static void Main(string args)
        {

        DataTable dt = new DataTable();
        dt.Columns.Add("parentname", typeof(string));
        dt.Columns.Add("parentid", typeof(int));
        dt.Columns.Add("childname", typeof(string));
        dt.Columns.Add("childid", typeof(int));

        DataTable dtChildPerson = new DataTable();
        dtChildPerson.Columns.Add("ChildPersonID", typeof(int));
        dtChildPerson.Columns.Add("ParentPersonID", typeof(int));
        dtChildPerson.Rows.Add(new object { 1, 1 });
        dtChildPerson.Rows.Add(new object { 1, 3 });
        dtChildPerson.Rows.Add(new object { 2, 4 });

        DataTable personName = new DataTable();
        personName.Columns.Add("ID", typeof(int));
        personName.Columns.Add("PersonName", typeof(string));
        personName.Rows.Add(new object { 1, "a" });
        personName.Rows.Add(new object { 2, "b" });
        personName.Rows.Add(new object { 3, "c" });
        personName.Rows.Add(new object { 4, "d" });


        foreach (DataRow row in dtChildPerson.AsEnumerable())
        {
        int parentID = row.Field<int>("ParentPersonID");
        string parentName = personName.AsEnumerable().Where(x => x.Field<int>("ID") == parentID).Select(x => x.Field<string>("PersonName")).FirstOrDefault();
        int childID = row.Field<int>("ChildPersonID");

        foreach(DataRow childRow in personName.AsEnumerable().Where(x => x.Field<int>("ID") == childID))
        {
        string childName = childRow.Field<string>("PersonName");

        dt.Rows.Add(new object { parentName, parentID, childName, childID });
        }

        }
        }
        }
        }





        share|improve this answer













        See code below :



        using System;
        using System.Collections.Generic;
        using System.Collections;
        using System.Linq;
        using System.Text;
        using System.Data;

        namespace ConsoleApplication94
        {
        class Program
        {
        static void Main(string args)
        {

        DataTable dt = new DataTable();
        dt.Columns.Add("parentname", typeof(string));
        dt.Columns.Add("parentid", typeof(int));
        dt.Columns.Add("childname", typeof(string));
        dt.Columns.Add("childid", typeof(int));

        DataTable dtChildPerson = new DataTable();
        dtChildPerson.Columns.Add("ChildPersonID", typeof(int));
        dtChildPerson.Columns.Add("ParentPersonID", typeof(int));
        dtChildPerson.Rows.Add(new object { 1, 1 });
        dtChildPerson.Rows.Add(new object { 1, 3 });
        dtChildPerson.Rows.Add(new object { 2, 4 });

        DataTable personName = new DataTable();
        personName.Columns.Add("ID", typeof(int));
        personName.Columns.Add("PersonName", typeof(string));
        personName.Rows.Add(new object { 1, "a" });
        personName.Rows.Add(new object { 2, "b" });
        personName.Rows.Add(new object { 3, "c" });
        personName.Rows.Add(new object { 4, "d" });


        foreach (DataRow row in dtChildPerson.AsEnumerable())
        {
        int parentID = row.Field<int>("ParentPersonID");
        string parentName = personName.AsEnumerable().Where(x => x.Field<int>("ID") == parentID).Select(x => x.Field<string>("PersonName")).FirstOrDefault();
        int childID = row.Field<int>("ChildPersonID");

        foreach(DataRow childRow in personName.AsEnumerable().Where(x => x.Field<int>("ID") == childID))
        {
        string childName = childRow.Field<string>("PersonName");

        dt.Rows.Add(new object { parentName, parentID, childName, childID });
        }

        }
        }
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 2 at 10:38









        jdwengjdweng

        17.8k2817




        17.8k2817






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54004470%2fhow-to-load-data-from-sql-to-datetable-that-show-in-treeview%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            SQL update select statement

            'app-layout' is not a known element: how to share Component with different Modules