0

I have this code for adding items into gridview. But in my below code duplicate items are not added. I need distinct items with no duplicate entries.

Below is my .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;
}

For more details, I am using below example code:

http://www.codeproject.com/Tips/773362/Filtering-search-results-with-multiple-CheckBox-Li

There is an above comment line saying clearly in my question like:

// importrow() adds distinct new subfilters to the existing filter, duplicate items are not added

Thank you in advance

Alan McBee
  • 4,202
  • 3
  • 33
  • 38
mastermind
  • 67
  • 9
  • Why not you select distinct rows from Database – Vijay Kumbhoje Dec 17 '15 at 08:08
  • I agree with vjay but take a look at the accepted answer on this http://stackoverflow.com/questions/1199176/how-to-select-distinct-rows-in-a-datatable-and-store-into-an-array – Simon Price Dec 17 '15 at 08:10
  • I had select distinct, but not coming. Did u understand my above code and cheked my code. @VijayKumbhoje – mastermind Dec 17 '15 at 08:11
  • @mastermind can you show us your code for how your selecting the distinct values as its something very fundamental to SQL that when you ask for distinct, you get distinct. which would suggest your data is bad and you are getting distinct rows back – Simon Price Dec 17 '15 at 08:12
  • select distinct(Qualification),Email, RegistrationId ,GivenName,Nationality,CellPhone, ExperienceYear,Title from View_Seach_ByPositionIdStatus where Status=@Status and Qualification=@FilterValue and AppliedPositionId=@PositionId .. – mastermind Dec 17 '15 at 08:15
  • you can see my issue more details: http://forums.asp.net/t/2078301.aspx?Duplicate+items+are+not+added+using+ASP+NET+C+ – mastermind Dec 17 '15 at 08:18
  • Yes.. tried that also... Did you check the above comments link will understand more. @SimonPrice – mastermind Dec 17 '15 at 08:23
  • @mastermind what is it yourexpecting as yoru call on the other page is different to what youre asking here. the call `select DISTINCT Email ,RegistrationId,GivenName,Nationality,CellPhone,ExperienceYear,Title from View_Seach_ByPositionIdStatus where Status=@Status and Qualification=@FilterValue and AppliedPositionId=@PositionId group by Email ,RegistrationId,GivenName,Nationality,CellPhone,ExperienceYear,Title` is giving you the right information back because the data is different – Simon Price Dec 17 '15 at 08:26
  • and also your qualifications are different too so again you would be betting the right information. – Simon Price Dec 17 '15 at 08:26
  • actually some users have two time records in education table like master degree and bachelor degree.. Its coming correct only. duplication also not coming into gridview. @SimonPrice – mastermind Dec 17 '15 at 08:29
  • when you make the call directly in sql are you getting the right amount of data back? – Simon Price Dec 17 '15 at 08:31
  • Ya i have same user but multiple qualication of the user, that why not getting and in my c# code if duplication means that user data is not added in gridview that is my problem. @SimonPrice – mastermind Dec 17 '15 at 08:33
  • what are you classing as duplicate data? – Simon Price Dec 17 '15 at 08:33
  • Yes getting right data, when i call directly in sql, u r correct that is my problem. @SimonPrice – mastermind Dec 17 '15 at 08:36
  • What i have do next, classing means.. @SimonPrice – mastermind Dec 17 '15 at 08:41
  • I didn't get any answer, in sql only data is coming correct, while in gridview duplciation is not added. @SimonPrice – mastermind Dec 17 '15 at 08:45
  • ok, lets break this down, if you put a break point in and you get to the point just before you bind it to the grid, check the datatable and see that has everything you are expecting. – Simon Price Dec 17 '15 at 08:45
  • Ya i am using multiple checkbox list for searching, if i will select multiple qualication of same user like master degree and bachelor degree of same user means.. that user details is not added.. In above question comments line given clearly... what happenind in my code, if (ds.Tables.Count == 0) then first time its adding into ds.Tables.Add(dt); and if it is second time means going to else condition , if first time details are availble in second means duplication is not adding like. @SimonPrice – mastermind Dec 17 '15 at 08:52
  • One more important point, http://www.codeproject.com/Tips/773362/Filtering-search-results-with-multiple-CheckBox-Li , the example which i am using that is for single table because in single table its giving correct because id(identity column values are diffrent), but in my case i am using multiple tables and created view and getting data from view and also select joining query. @SimonPrice – mastermind Dec 17 '15 at 08:57
  • I think we are near to result, please reply. @SimonPrice – mastermind Dec 17 '15 at 08:59
  • whats the reason for multiple tables? – Simon Price Dec 17 '15 at 09:05
  • it might be worth separating your results out into a list of classes and then using a lambda expression to rebuild a new temp table to bind at each filter – Simon Price Dec 17 '15 at 09:06
  • 1. there are following tables. i) table_registrationinfo when user will register the details will be inserted into this.. – mastermind Dec 17 '15 at 09:07
  • ii) the second table Table_applciationinfo, the user who had applied to job that user details only search, thats y joining two table table_registrationinfo and table_applciationinfo – mastermind Dec 17 '15 at 09:10
  • iii) third table i created table_educationinfo for that i created view: View_Seach_ByPositionIdStatus , for getting education details, i join three tables table_registrationinf,table_educationinfo adn table_ applciationinfo to get qualication. – mastermind Dec 17 '15 at 09:14
  • sorry been working... will have a look at your comments now – Simon Price Dec 17 '15 at 10:17
  • ok, so what I would try at this instance is to get one table back with predefined data\criteria based on your boxes, but this is more of a debugging issue now which is not what this site is intended for. – Simon Price Dec 17 '15 at 10:19
  • Its ok.. Still i am waiting for your reply. @SimonPrice – mastermind Dec 17 '15 at 10:19
  • I am very confused about this issue since long period. @SimonPrice – mastermind Dec 17 '15 at 10:22
  • read my previous messages, about stepping through it bit by bit. this is a debugging issue which I cant really help you with as I cannot step through the code – Simon Price Dec 17 '15 at 11:01
  • Can you give me general result of code to find my result. @SimonPrice – mastermind Dec 17 '15 at 11:08

0 Answers0