title: Specified cast is not valid using asp.net C#
body: <p>I have many check box lists for searching, but when select multiple checks for search then getting an error, below is my full code:   </p>
  
  <pre><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 &lt; 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["id"] == (int)newdr["id"])
                          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;
  }
  </code></pre>
  
  <p>Exception is:</p>
  
  <pre><code>Specified cast is not valid
  </code></pre>
  
  <p>I am getting exception in below line:</p>
  
  <pre><code>if ((int)dr["id"] == (int)newdr["id"])
  </code></pre>
  
  <p>Thank you in advance</p>