title: difference between two datatables
body: <p>I've a problem here. If any of you can please help me in this regard it would be geat.</p>
  
  <p>As I'm new user it doesn't allow me to post new images.</p>
  
  <p>Table1 has two rows with similar data as shown below (Please try to look it as a DataView).</p>
  
  <pre><code>RID      | Release_Point_ID   | short_desc
  -------------------------------------------
  1007     | SV006              | Boiler1
  1008     | SV006              | Boiler2
  </code></pre>
  
  <p>Table2 has one row of same data. </p>
  
  <pre><code>RID      | Release_Point_ID   | short_desc
  -------------------------------------------
  1017     | SV006              | Boiler
  </code></pre>
  
  <p>My code snippet below scans and breaks out of the loop once it finds the data in Table2.</p>
  
  <p>If you can see two attached snap shots along with this mail, those are the DataSet views for two different datatables. In those snap shots I've highlighted a line for the column Process_rid with row "100100164". And the same this for table2 but here we have two rows.</p>
  
  <p>I need to get the difference between these two datatables. I've given example snap shots for understanding the scenario, the code I've written is very basic one but will skip the row if it has two common/similar rows. </p>
  
  <pre><code>#region get Difference Table
      public static bool isEqual;
      public static DataTable table;
      public static string table2PrvRow, tablePrvRow;
      public static int tblRow = 0, tableRCount = 0;
      // This function fetches the row which is different in both the tables.
      public DataTable getDifferenceTable(DataTable table1, DataTable table2)
      {
          //Create Empty Table
          DataTable table = new DataTable("Difference");
          try
          {
              if (table1.Rows.Count &gt; table2.Rows.Count)
              {
                  resultTable = table1.Clone();
                  DataRow resultRow = resultTable.NewRow();
                  if (tblName == "INV_SOURCES")
                  {
                      string sourceId = "27" + string.Format("{0:00000000}", getPreferredID(facId));
                      table = _DB.ReadDataTable(string.Format("SELECT SOURCE_NAME FROM RAPIDS.INV_SOURCES WHERE INVENTORY_RID = {0} AND SOURCE_ID = {1}", glblVars.invRid, sourceId));
                  }
                  else if (tblName == "INV_PROCESS_RELEASE_POINTS")
                      table = _DB.ReadDataTable(string.Format("SELECT IPRP.PROCESS_RID FROM RAPIDS.INV_PROCESS_RELEASE_POINTS IPRP WHERE IPRP.SOURCE_RID = {0}", glblVars.RapidsSourceRID));
                  for (int row = 0; row &lt; table.Rows.Count; row++)
                  {
                      foreach (DataRow rpIDRow in table2.Rows)
                      {
                          if (tblName == "INV_SOURCES" &amp;&amp; rpIDRow["SOURCE_NAME"].ToString() == table1.Rows[row]["SOURCE_NAME"].ToString())
                          { copy = "N"; break; }
                          else if (tblName == "INV_PROCESS_RELEASE_POINTS" &amp;&amp; rpIDRow["PROCESS_RID"].ToString() == table1.Rows[row]["PROCESS_RID"].ToString())
                          { copy = "N"; break; }
                          else
                          { copy = "Y"; }
                      }
  
                      if (copy == "Y")
                      { resultRow = resultTable.NewRow(); resultRow.ItemArray = table1.Rows[row].ItemArray; resultTable.Rows.Add(resultRow); }
                  }
              }
              else if (table1.Rows.Count &lt; table2.Rows.Count)
              {
                  resultTable = table2.Clone();
                  DataRow resultRow = resultTable.NewRow();
                  if (tblName == "INV_SOURCES")
                  {
                      string sourceId = "27" + string.Format("{0:00000000}", getPreferredID(facId));
                      table = _DB.ReadDataTable(string.Format("SELECT SOURCE_NAME FROM RAPIDS.INV_SOURCES WHERE INVENTORY_RID = {0} AND SOURCE_ID = {1}", glblVars.invRid, sourceId));
                  }
                  else if (tblName == "INV_PROCESS_RELEASE_POINTS")
                  {
                      //DataTable differenceOfTwoTables = Difference(table1, table2);
                      table = _DB.ReadDataTable(string.Format("SELECT IPRP.PROCESS_RID FROM RAPIDS.INV_PROCESS_RELEASE_POINTS IPRP WHERE IPRP.SOURCE_RID = {0}", glblVars.RapidsSourceRID));
                  }
                  for (int row = 0; row &lt; table2.Rows.Count; row++)
                  {
  
                      foreach (DataRow rpIDRow in table.Rows)
                      {
                          tableRCount = 0;
                          if (tblName == "INV_SOURCES")
                          {
                              string table1srcName = rpIDRow["SOURCE_NAME"].ToString();
                              string table2srcName = table2.Rows[row]["SOURCE_NAME"].ToString().Replace("'", "");
                              if (table1srcName == table2srcName)
                              { copy = "N"; break; }
                          }
                          else if (tblName == "INV_PROCESS_RELEASE_POINTS" &amp;&amp; rpIDRow["PROCESS_RID"].ToString() == table2.Rows[row]["PROCESS_RID"].ToString())
                          { copy = "N"; break; }
  
                          else
                          { copy = "Y"; } tableRCount++;
                      }
                      if (copy == "Y")
                      { resultRow = resultTable.NewRow(); resultRow.ItemArray = table2.Rows[row].ItemArray; resultTable.Rows.Add(resultRow); }
                  }
              }
              else
                  resultTable = table1.Clone();
          }
          catch (Exception ex)
          {
              throw new Exception("Get Difference Table: " + ex.Message);
          }
          return resultTable;
      }
  </code></pre>