Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to Get Data From Data Set

How to Get Data From Data Set

Scheduled Pinned Locked Moved C#
tutorialquestion
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Hello Everybody, I have two tables Emp and Dept in DataSet. In which have followind fields.

    Emp Dept


    Emp_No Emp_No
    Emp_Name Dept_No
    Emp_Sal Dep_Name

    Now i want the result in datagridview from both dataset. Result will be look like this.....

    Result_In_dataGridview

    Emp_no Emp_Name Emp_Sal Dept_no Dept_Name

    So please describe me how can i do that by using Code. Thanks

    If you can think then I Can.

    OriginalGriffO G 2 Replies Last reply
    0
    • L Lost User

      Hello Everybody, I have two tables Emp and Dept in DataSet. In which have followind fields.

      Emp Dept


      Emp_No Emp_No
      Emp_Name Dept_No
      Emp_Sal Dep_Name

      Now i want the result in datagridview from both dataset. Result will be look like this.....

      Result_In_dataGridview

      Emp_no Emp_Name Emp_Sal Dept_no Dept_Name

      So please describe me how can i do that by using Code. Thanks

      If you can think then I Can.

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      You need to construct a SQL JOIN between the two tables. Here is a simple example which combines the two into a new table:

          static void Main(string\[\] args)
              {
              Console.WriteLine("Started");
              SqlConnection con = new SqlConnection(strCon);
              con.Open();
              SqlDataAdapter daMyTable = new SqlDataAdapter("SELECT \* FROM myTable", con);
              SqlDataAdapter daMyOtherTable = new SqlDataAdapter("SELECT \* FROM myOtherTable", con);
              SqlDataAdapter daJoined = new SqlDataAdapter("SELECT \* FROM myTable INNER JOIN myOtherTable ON myTable.iD=myOtherTable.iD", con);
              DataSet ds = new DataSet();
              daMyTable.Fill(ds, "My Table");
              daMyOtherTable.Fill(ds, "My Other Table");
              daJoined.Fill(ds, "My Joined Table");
              ShowDataSet(ds);
              con.Close();
              Console.WriteLine("Stopped");
              Console.ReadLine();
              }
      
          private static void ShowDataSet(DataSet ds)
              {
              foreach (DataTable dt in ds.Tables)
                  {
                  Console.WriteLine(dt.TableName);
                  ShowDataTable("   ", dt);
                  }
              }
      
          private static void ShowDataTable(string p, DataTable dt)
              {
              int cols = dt.Columns.Count;
              foreach (DataRow dr in dt.Rows)
                  {
                  Console.Write(p);
                  string sep = "";
                  for (int i = 0; i < cols; i++)
                      {
                      Console.Write(sep + dr\[i\].ToString());
                      sep = ", ";
                      }
                  Console.WriteLine();
                  }
              }
      

      Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      L O 2 Replies Last reply
      0
      • L Lost User

        Hello Everybody, I have two tables Emp and Dept in DataSet. In which have followind fields.

        Emp Dept


        Emp_No Emp_No
        Emp_Name Dept_No
        Emp_Sal Dep_Name

        Now i want the result in datagridview from both dataset. Result will be look like this.....

        Result_In_dataGridview

        Emp_no Emp_Name Emp_Sal Dept_no Dept_Name

        So please describe me how can i do that by using Code. Thanks

        If you can think then I Can.

        G Offline
        G Offline
        GauravKP
        wrote on last edited by
        #3

        just use innerjoin .. select Emp.*, Dept.* from Emp inner join Dept on Emp.Emp_No = Dept.Emp_No

        L 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          You need to construct a SQL JOIN between the two tables. Here is a simple example which combines the two into a new table:

              static void Main(string\[\] args)
                  {
                  Console.WriteLine("Started");
                  SqlConnection con = new SqlConnection(strCon);
                  con.Open();
                  SqlDataAdapter daMyTable = new SqlDataAdapter("SELECT \* FROM myTable", con);
                  SqlDataAdapter daMyOtherTable = new SqlDataAdapter("SELECT \* FROM myOtherTable", con);
                  SqlDataAdapter daJoined = new SqlDataAdapter("SELECT \* FROM myTable INNER JOIN myOtherTable ON myTable.iD=myOtherTable.iD", con);
                  DataSet ds = new DataSet();
                  daMyTable.Fill(ds, "My Table");
                  daMyOtherTable.Fill(ds, "My Other Table");
                  daJoined.Fill(ds, "My Joined Table");
                  ShowDataSet(ds);
                  con.Close();
                  Console.WriteLine("Stopped");
                  Console.ReadLine();
                  }
          
              private static void ShowDataSet(DataSet ds)
                  {
                  foreach (DataTable dt in ds.Tables)
                      {
                      Console.WriteLine(dt.TableName);
                      ShowDataTable("   ", dt);
                      }
                  }
          
              private static void ShowDataTable(string p, DataTable dt)
                  {
                  int cols = dt.Columns.Count;
                  foreach (DataRow dr in dt.Rows)
                      {
                      Console.Write(p);
                      string sep = "";
                      for (int i = 0; i < cols; i++)
                          {
                          Console.Write(sep + dr\[i\].ToString());
                          sep = ", ";
                          }
                      Console.WriteLine();
                      }
                  }
          

          Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          I Do't have any Connection. I have data in dataset.

          If you can think then I Can.

          1 Reply Last reply
          0
          • G GauravKP

            just use innerjoin .. select Emp.*, Dept.* from Emp inner join Dept on Emp.Emp_No = Dept.Emp_No

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            How can i use join in data set.

            If you can think then I Can.

            1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              You need to construct a SQL JOIN between the two tables. Here is a simple example which combines the two into a new table:

                  static void Main(string\[\] args)
                      {
                      Console.WriteLine("Started");
                      SqlConnection con = new SqlConnection(strCon);
                      con.Open();
                      SqlDataAdapter daMyTable = new SqlDataAdapter("SELECT \* FROM myTable", con);
                      SqlDataAdapter daMyOtherTable = new SqlDataAdapter("SELECT \* FROM myOtherTable", con);
                      SqlDataAdapter daJoined = new SqlDataAdapter("SELECT \* FROM myTable INNER JOIN myOtherTable ON myTable.iD=myOtherTable.iD", con);
                      DataSet ds = new DataSet();
                      daMyTable.Fill(ds, "My Table");
                      daMyOtherTable.Fill(ds, "My Other Table");
                      daJoined.Fill(ds, "My Joined Table");
                      ShowDataSet(ds);
                      con.Close();
                      Console.WriteLine("Stopped");
                      Console.ReadLine();
                      }
              
                  private static void ShowDataSet(DataSet ds)
                      {
                      foreach (DataTable dt in ds.Tables)
                          {
                          Console.WriteLine(dt.TableName);
                          ShowDataTable("   ", dt);
                          }
                      }
              
                  private static void ShowDataTable(string p, DataTable dt)
                      {
                      int cols = dt.Columns.Count;
                      foreach (DataRow dr in dt.Rows)
                          {
                          Console.Write(p);
                          string sep = "";
                          for (int i = 0; i < cols; i++)
                              {
                              Console.Write(sep + dr\[i\].ToString());
                              sep = ", ";
                              }
                          Console.WriteLine();
                          }
                      }
              

              Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

              O Offline
              O Offline
              Orcun Iyigun
              wrote on last edited by
              #6

              SqlDataAdapter daMyTable = new SqlDataAdapter("SELECT * FROM myTable", con); SqlDataAdapter daMyOtherTable = new SqlDataAdapter("SELECT * FROM myOtherTable", con); SqlDataAdapter daJoined = new SqlDataAdapter("SELECT * FROM myTable INNER JOIN myOtherTable ON myTable.iD=myOtherTable.iD", con); I am curious why did you get the data seperately instead of using a one query with a "join", getting all the data all once and bing it to gridview?

              OriginalGriffO 1 Reply Last reply
              0
              • O Orcun Iyigun

                SqlDataAdapter daMyTable = new SqlDataAdapter("SELECT * FROM myTable", con); SqlDataAdapter daMyOtherTable = new SqlDataAdapter("SELECT * FROM myOtherTable", con); SqlDataAdapter daJoined = new SqlDataAdapter("SELECT * FROM myTable INNER JOIN myOtherTable ON myTable.iD=myOtherTable.iD", con); I am curious why did you get the data seperately instead of using a one query with a "join", getting all the data all once and bing it to gridview?

                OriginalGriffO Offline
                OriginalGriffO Offline
                OriginalGriff
                wrote on last edited by
                #7

                Purely to show what I was doing, and that I had two tables in the database. Seemed worth doing for a beginner in this stuff... :laugh:

                Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups