I have many checkbox lists for searching, but when I will search by any of the checkbox list search if any record will be two times in table means will not fill into gridview.

This line of code is not allowed duplicate values, if duplicate means allow single record.

    if (newdt.TableName == dt.TableName)
       {
        // see if filter is already present in the dataset
           tableMatchFound = true;
          // when the current filter is already present in the dataset
          foreach (DataRow dr in dt.Rows)
          ds.Tables[newdt.TableName].ImportRow(dr);

       }

 // importrow() adds distinct new subfilters to the existing filter, duplicate items are not added
        if (!tableMatchFound)
            ds.Tables.Add(dt);

Below is my full .cs code.

   private DataTable list(String dbObject, String filterName, String filterValue,string PositonId,string Status)
{
    NameValuePairList objNameValuePairList = new NameValuePairList();
    objNameValuePairList.Add(new NameValuePair("@FilterValue", filterValue, PositonId, Status));
    objNameValuePairList.Add(new NameValuePair("@Action", "FilterBy" + filterName, PositonId, Status));
    DataTable dt = dl.Search_RegisterationInfo(dbObject, objNameValuePairList, PositonId, Status);
    return dt;
}

public DataTable list(String dbOject, FilterList myFilterList,string PositonId,string Status)
{
    // gets a collection(dataset) of all unique filters(datatables) and also group all subfilters(rows) under each filter
    DataTable dt;
    DataSet ds = new DataSet();
    // a filter may be a Nationality or a Qualification 
    foreach (Filter item in myFilterList)
    // a subfilter may be Indian or Expatriate under the filter Nationality 
    {
        // another subfilter may be Bachelor degree or Master Degree under the filter Qualification
        dt = list(dbOject, item.getFilterName, item.getFilterValue, PositonId,Status);
        dt.TableName = item.getFilterName;
        // datatables are named based on the filters
        if (ds.Tables.Count == 0)
            // so we get a collection of unique filters (datatables) in the dataset
            ds.Tables.Add(dt);
        // add new filter without checking, since for the first time, no conflicts are possible
        else
        {
            bool tableMatchFound = false;
            foreach (DataTable newdt in ds.Tables)
                if (newdt.TableName == dt.TableName)
                {
                    // see if filter is already present in the dataset
                    tableMatchFound = true;
                    // when the current filter is already present in the dataset
                    foreach (DataRow dr in dt.Rows)
                        ds.Tables[newdt.TableName].ImportRow(dr);

                }
            // importrow() adds distinct new subfilters to the existing filter, duplicate items are not added
            if (!tableMatchFound)
                ds.Tables.Add(dt);
        }
        // if the filter does not exist, add the new filter to the collection
    }
    // the entire collection of filters will contain duplicate items
    // distinct items from the entire collection is filtered out in the next section
    dt = ds.Tables[0].Clone();
    // get the structure of the first filter as they all apply to the same table object  
    if (ds.Tables.Count == 1)
        dt = ds.Tables[0];
    // if there is only one filter, no filtering is required
    else
        // if there are more than one, compare each subfilter of every other filter with the subfilters of the first filter
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            // each subfilter from the first filter is used as a pivot
            int rowMatchFound = 1;
            for (int i = 1; i < ds.Tables.Count; i++)
                // search all filters except the first one
                foreach (DataRow newdr in ds.Tables[i].Rows)
                    // select each subfilter from all the filter
                    if ((int)dr["RegistrationId"] == (int)newdr["RegistrationId"])
                        rowMatchFound++;
            if (rowMatchFound == ds.Tables.Count)
                // a match is found exactly once in all the filters
                dt.ImportRow(dr);
            // the final item is selected so that is is present in all the filters   
        }
    return dt;
}

Please follow the link for more details of my question. https://forums.asp.net/t/2078301.aspx?Duplicate+items+are+not+added+using+ASP+NET+C+

Related posts

Recent Viewed