0

enter image description here

I have one Issue i don't know how to do this i have a dataset if it has duplicates rows then i want to print a message. can anybody suggest me how can i do this. Below is Code

bool ErrorOcc = false;
StringBuilder script = new StringBuilder();
string Filter = "";
string XmlCheck = "";
string[] IfExists = null;
for (int i = 0; i < ColumnsToks[0].Length; i++)
{
    if (ColumnsToks[0][i] != "")
    {
        xml += @"<" + objModule.TableName + ">";
        for (int j = 0; j < objModule.Cols.Count; j++)
        {
            MCol col = objModule.Cols[j];
            DataTable dt = EBusiness.GetModuleColsValidation(this.Data.MID, col.ID);
            if (dt.Select("ValidationOnClient = 'False' and ValidationType = 'Unique'").Length == 0)
            {

            }
            for (int m = 0; m < dt.Rows.Count; m++)
            {
                 if (!Convert.ToBoolean(dt.Rows[m]["ValidationOnClient"]) && dt.Rows[m]["ValidationType"].ToString() == "Unique")
                 {
                     //  Filter += col.ColName + "='" + ColumnsToks[j][i] + "' And ";
                     XmlCheck += col.ColName + "='" + ColumnsToks[j][i] + "',";
                  }
             }
             IfExists = XmlCheck.Split(",".ToCharArray());
             xml += "<" + col.ColName + ">" + ColumnsToks[j][i].Replace("<", "&lt;").Replace(">", "&gt;").Replace("&nbsp;", "") + "</" + col.ColName + ">";
         }
         bool IfDuplicate = false;
         foreach (string count in IfExists)
         {
             if (count != "")
             {
                 Filter += count + " and ";
             }
         }
         objData.Query = "select COUNT(*) from " + objModule.TableName + " where " + Filter + " 1=1 ";
         IfDuplicate = Convert.ToBoolean(objData.GetSingleValue());
         if (IfDuplicate)
         {
             script.Append("alert('Error - There are some Duplicate entries.'); ");
             ErrorOcc = true;
         }
         if (ErrorOcc)
         {
             this.ScriptOutput = script + " ValidateBeforeSaving = false;";
             this.StayContent = "yes";
             return;
         }
         if (DataSetupColumnName != "")
         {
             foreach (MModule module in dataSetupModules.Modules)
             {
                 xml += "<" + module.TableName + ">" + Request["d" + module.TableName] + "</" + module.TableName + ">";
             }
          }
          foreach (DataRow dr in dtChildModule.Rows)
          {
              if (Request["t" + dr["ParentModuleID"]] == "")
              {
                  this.Message = "Please select Parent Module ID.";
                  return;
              }
              xml += "<t" + dr["ParentModuleID"] + ">" + Convert.ToInt32("0" + Request["t" + dr["ParentModuleID"]]) + "</t" + dr["ParentModuleID"] + ">";
          }
          xml += "<Status>1</Status><CreatedBy>0</CreatedBy><UpdatedBy>0</UpdatedBy>";
          if (dtRestrictions.Rows.Count > 0)
          {
               xml += "<Level>" + GetLevel(EBusiness.GetCompanySetupModules(this.Data.WFID)) + "</Level>";
               xml += "<LevelDataID>" + Request["t" + Request["ParentModuleID"]] + "</LevelDataID>";
          }
          xml += "</" + objModule.TableName + ">";
      }
  }
  xml += "</Table>";
  DataSet dsXml = new DataSet();
  dsXml.ReadXml(new XmlTextReader(new StringReader(xml)));
Hitesh
  • 3,449
  • 8
  • 39
  • 57
DineshChauhan
  • 121
  • 1
  • 4
  • 17

2 Answers2

1

There are several ways to make it work, the first two that come to my mind are either the use of HashTables or LinQ expressions.

Take a look to this: Best way to remove duplicate entries from a data table but instead of removing the duplicate (look at the second foreach) you print the message.

public void CheckDuplicateRows(DataTable dTable, string colName)
{
   Hashtable hTable = new Hashtable();
   ArrayList duplicateList = new ArrayList();

   //Add list of all the unique item value to hashtable, which stores combination of     key, value pair.
   //And add duplicate item value in arraylist.
   foreach (DataRow drow in dTable.Rows)
   {
      if (hTable.Contains(drow[colName]))
         duplicateList.Add(drow);
      else
         hTable.Add(drow[colName], string.Empty); 
   }

  //Checks the list dimension to verify if there is any duplicate
  if(duplicateList.Count() > 0)
  {
  //you can print your message here or eventually get info about the duplicate row
  }   
}
Community
  • 1
  • 1
Saverio Terracciano
  • 3,885
  • 1
  • 30
  • 42
0

Here is the answer of my above Question

You can Check duplicate rows in DataSet like this that is working fine, try it:

DataSet dsXml = new DataSet();
dsXml.ReadXml(new XmlTextReader(new StringReader(xml)));

List<string> duplicateList = new List<string>();
foreach (DataRow drow in dsXml.Tables[0].Rows)
{
    string strr = "";

    for (int j = 0; j < dsXml.Tables[0].Columns.Count; j++ )
    {
        strr += drow[j];
    }

    if (!duplicateList.Contains(strr))
    {
        duplicateList.Add(strr);
    }
    else
    {
        script.Append("alert('Error - There are some Duplicate entries.'); ");
        ErrorOcc = true;
        if (ErrorOcc)
        {
            this.ScriptOutput = script + " ValidateBeforeSaving = false;";
            this.StayContent = "yes";
            return;
        }
    }
}
shA.t
  • 16,580
  • 5
  • 54
  • 111
DineshChauhan
  • 121
  • 1
  • 4
  • 17