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. Web Development
  3. ASP.NET
  4. datatable

datatable

Scheduled Pinned Locked Moved ASP.NET
help
16 Posts 5 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.
  • M Malik1122

    i want to fill one datatable by another datatable but its not working. so plz help in solving this.

    V Offline
    V Offline
    Vimalsoft Pty Ltd
    wrote on last edited by
    #3

    Good Mornig Malik1122 We cant know what is the Problem until we see the code, another thing that can be easly done on the SQL. e.g

    INSERT INTO TABLE2
    SELECT COL1,COL2 FROM TABLE1
    WHERE COL1 IS NOT NULL

    Always try to keep the sql side in SQl , because these things can be achieve at ease that side than trying to loop through a datatable. Please explain clearly what is your problem.

    Vuyiswa Maseko, Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers." C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.somee.com http://www.vuyiswamaseko.tiyaneProperties.co.za vuyiswa@its.co.za http://www.itsabacus.co.za/itsabacus/

    M 1 Reply Last reply
    0
    • B Brij

      can you post your code?How are you doing this.

      Cheers!! Brij

      M Offline
      M Offline
      Malik1122
      wrote on last edited by
      #4

      int colm = 0; if (objBUS.busDataSet.Tables["tblSwapedColumns"].Columns.Count > 0) { for (int X = 0; X < objBUS.busDataSet.Tables["tblSwapedColumns"].Columns.Count; X++) { if (objBUS.busDataSet.Tables["tblSwapedColumns"].Columns[X].ToString() == objBUS.busDataSet.Tables["tbl"].Columns[colm].ToString()) { if (colm == 0) { for (int Q = 0; Q < objBUS.busDataSet.Tables["tbl"].Rows.Count; Q++) { objBUS.busDataSet.Tables["tblSwapedColumns"].Rows.Clear(); DataRow DRow = objBUS.tblSwapedColumns.NewRow(); DRow[objBUS.busDataSet.Tables["tblSwapedColumns"].Columns[X]] = objBUS.busDataSet.Tables["tbl"].Rows[Q][colm].ToString();//here code //give exception "Column 'FIRST NAME' does not belong to table tblSwapedColumns" //'FIRST NAME' is the column name which is previously present in the table. objBUS.tblSwapedColumns.Rows.Add(DRow); } colm++; } else { for (int Q = 0; Q < objBUS.busDataSet.Tables["tbl"].Rows.Count; Q++) { objBUS.busDataSet.Tables["tblSwapedColumns"].Rows[X][Q] = objBUS.busDataSet.Tables["tbl"].Rows[X][colm].ToString(); objBUS.busDataSet.Tables["tblSwapedColumns"].AcceptChanges(); } colm++; } } } }

      B 1 Reply Last reply
      0
      • B Brij

        can you post your code?How are you doing this.

        Cheers!! Brij

        M Offline
        M Offline
        Malik1122
        wrote on last edited by
        #5

        you got what i have done...?

        1 Reply Last reply
        0
        • V Vimalsoft Pty Ltd

          Good Mornig Malik1122 We cant know what is the Problem until we see the code, another thing that can be easly done on the SQL. e.g

          INSERT INTO TABLE2
          SELECT COL1,COL2 FROM TABLE1
          WHERE COL1 IS NOT NULL

          Always try to keep the sql side in SQl , because these things can be achieve at ease that side than trying to loop through a datatable. Please explain clearly what is your problem.

          Vuyiswa Maseko, Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers." C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.somee.com http://www.vuyiswamaseko.tiyaneProperties.co.za vuyiswa@its.co.za http://www.itsabacus.co.za/itsabacus/

          M Offline
          M Offline
          Malik1122
          wrote on last edited by
          #6

          Good Morning :) i have send the code so please check it and help me in doing this.

          V 1 Reply Last reply
          0
          • M Malik1122

            Good Morning :) i have send the code so please check it and help me in doing this.

            V Offline
            V Offline
            Vimalsoft Pty Ltd
            wrote on last edited by
            #7

            Can i see the Structure of tblSwapedColumns ?

            Vuyiswa Maseko, Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers." C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.somee.com http://www.vuyiswamaseko.tiyaneProperties.co.za vuyiswa@its.co.za http://www.itsabacus.co.za/itsabacus/

            M 1 Reply Last reply
            0
            • V Vimalsoft Pty Ltd

              Can i see the Structure of tblSwapedColumns ?

              Vuyiswa Maseko, Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers." C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.somee.com http://www.vuyiswamaseko.tiyaneProperties.co.za vuyiswa@its.co.za http://www.itsabacus.co.za/itsabacus/

              M Offline
              M Offline
              Malik1122
              wrote on last edited by
              #8

              The column of 'tbl' are 1. FIRST NAME 2. LAST NAME 3. DATE OF BIRTH 4. AGE 5. GENDER And that of 'tblSwapedColumns' are 1. GENDER 2. DATE OF BIRTH 3. AGE 4. LAST NAME 5. FIRST NAME

              V 1 Reply Last reply
              0
              • M Malik1122

                The column of 'tbl' are 1. FIRST NAME 2. LAST NAME 3. DATE OF BIRTH 4. AGE 5. GENDER And that of 'tblSwapedColumns' are 1. GENDER 2. DATE OF BIRTH 3. AGE 4. LAST NAME 5. FIRST NAME

                V Offline
                V Offline
                Vimalsoft Pty Ltd
                wrote on last edited by
                #9

                The Problem might be the names of your Columns in SQl it would be fine. Try not to name your Field names with Spaces , when Defining your Column names use something like th First_Name or Firstname Last_Name or LastName please Fix that and try to run your code again

                Vuyiswa Maseko, Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers." C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.somee.com http://www.vuyiswamaseko.tiyaneProperties.co.za vuyiswa@its.co.za http://www.itsabacus.co.za/itsabacus/

                M 2 Replies Last reply
                0
                • M Malik1122

                  i want to fill one datatable by another datatable but its not working. so plz help in solving this.

                  B Offline
                  B Offline
                  Baran M
                  wrote on last edited by
                  #10

                  Do you get any error ? whats the output of your code ?

                  Education is not a way to escape poverty — it is a way of fighting it.

                  1 Reply Last reply
                  0
                  • V Vimalsoft Pty Ltd

                    The Problem might be the names of your Columns in SQl it would be fine. Try not to name your Field names with Spaces , when Defining your Column names use something like th First_Name or Firstname Last_Name or LastName please Fix that and try to run your code again

                    Vuyiswa Maseko, Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers." C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.somee.com http://www.vuyiswamaseko.tiyaneProperties.co.za vuyiswa@its.co.za http://www.itsabacus.co.za/itsabacus/

                    M Offline
                    M Offline
                    Malik1122
                    wrote on last edited by
                    #11

                    I'm not using any SQL Query to fill datatable. I'm just coying data from one datatable to an other datatable without the use of any Query.

                    1 Reply Last reply
                    0
                    • M Malik1122

                      i want to fill one datatable by another datatable but its not working. so plz help in solving this.

                      Z Offline
                      Z Offline
                      Zafar A khan
                      wrote on last edited by
                      #12

                      Hi Malik. Just Assign one table to other like DataTable dt=datatable; if one table already contains some records and you don't want to loss that then use the following code.

                      //copy tbl1 to tbl2
                      public DataTable MergTables(DataTable tbl1, DataTable tbl2)
                      {
                      if (tbl1.Rows.Count > 0)
                      {
                      foreach (DataRow row in tbl1.Rows)
                      {
                      DataRow newrow = tbl2.NewRow();
                      newrow["Col1"] = row["Col1"].ToString();
                      newrow["Col2"] = row["Col2"].ToString();
                      newrow["Col3"] = row["Col3"].ToString();
                      tbl2.Rows.Add(newrow);
                      tbl2.AcceptChanges();

                                  }
                              }
                              return tbl2;
                          }
                      
                      M 1 Reply Last reply
                      0
                      • M Malik1122

                        int colm = 0; if (objBUS.busDataSet.Tables["tblSwapedColumns"].Columns.Count > 0) { for (int X = 0; X < objBUS.busDataSet.Tables["tblSwapedColumns"].Columns.Count; X++) { if (objBUS.busDataSet.Tables["tblSwapedColumns"].Columns[X].ToString() == objBUS.busDataSet.Tables["tbl"].Columns[colm].ToString()) { if (colm == 0) { for (int Q = 0; Q < objBUS.busDataSet.Tables["tbl"].Rows.Count; Q++) { objBUS.busDataSet.Tables["tblSwapedColumns"].Rows.Clear(); DataRow DRow = objBUS.tblSwapedColumns.NewRow(); DRow[objBUS.busDataSet.Tables["tblSwapedColumns"].Columns[X]] = objBUS.busDataSet.Tables["tbl"].Rows[Q][colm].ToString();//here code //give exception "Column 'FIRST NAME' does not belong to table tblSwapedColumns" //'FIRST NAME' is the column name which is previously present in the table. objBUS.tblSwapedColumns.Rows.Add(DRow); } colm++; } else { for (int Q = 0; Q < objBUS.busDataSet.Tables["tbl"].Rows.Count; Q++) { objBUS.busDataSet.Tables["tblSwapedColumns"].Rows[X][Q] = objBUS.busDataSet.Tables["tbl"].Rows[X][colm].ToString(); objBUS.busDataSet.Tables["tblSwapedColumns"].AcceptChanges(); } colm++; } } } }

                        B Offline
                        B Offline
                        Brij
                        wrote on last edited by
                        #13

                        Sorry for late reply.But as your exception you are trying to access some invalid column name.Please check the schema..

                        Cheers!! Brij

                        1 Reply Last reply
                        0
                        • Z Zafar A khan

                          Hi Malik. Just Assign one table to other like DataTable dt=datatable; if one table already contains some records and you don't want to loss that then use the following code.

                          //copy tbl1 to tbl2
                          public DataTable MergTables(DataTable tbl1, DataTable tbl2)
                          {
                          if (tbl1.Rows.Count > 0)
                          {
                          foreach (DataRow row in tbl1.Rows)
                          {
                          DataRow newrow = tbl2.NewRow();
                          newrow["Col1"] = row["Col1"].ToString();
                          newrow["Col2"] = row["Col2"].ToString();
                          newrow["Col3"] = row["Col3"].ToString();
                          tbl2.Rows.Add(newrow);
                          tbl2.AcceptChanges();

                                      }
                                  }
                                  return tbl2;
                              }
                          
                          M Offline
                          M Offline
                          Malik1122
                          wrote on last edited by
                          #14

                          Thanks for helping. Thats not what I want to do but now finally I resolved the problem. Anyway Thanks.

                          1 Reply Last reply
                          0
                          • V Vimalsoft Pty Ltd

                            The Problem might be the names of your Columns in SQl it would be fine. Try not to name your Field names with Spaces , when Defining your Column names use something like th First_Name or Firstname Last_Name or LastName please Fix that and try to run your code again

                            Vuyiswa Maseko, Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers." C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.somee.com http://www.vuyiswamaseko.tiyaneProperties.co.za vuyiswa@its.co.za http://www.itsabacus.co.za/itsabacus/

                            M Offline
                            M Offline
                            Malik1122
                            wrote on last edited by
                            #15

                            Thanks For Help.

                            V 1 Reply Last reply
                            0
                            • M Malik1122

                              Thanks For Help.

                              V Offline
                              V Offline
                              Vimalsoft Pty Ltd
                              wrote on last edited by
                              #16

                              You welcome.

                              Vuyiswa Maseko, Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers." C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.somee.com http://www.vuyiswamaseko.tiyaneProperties.co.za vuyiswa@its.co.za http://www.itsabacus.co.za/itsabacus/

                              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