"Insert is an invalid operation"

"Insert is an invalid operation"

Old forum URL: forums.lhotka.net/forums/t/3630.aspx


komminane posted on Monday, October 01, 2007

When i am trying to add the DTO to the DTO collection it is giving this error

DTO:

public class PatientDTO
   {

       private Int32 _patiendID;
       public Int32 PatientID
       {
          
           get{return _patiendID;}
          
           set{_patiendID = value;}
       }
       private string _firstName;
       public string FirstName
       {
           get{return _firstName;}
          
           set{_firstName = value;}
       }
       private string _lastName;
       public string LastName
       {
           get{return _lastName;}
           set{_lastName = value;}
       }
   }

and the collection class is

[Serializable()]
  public class PatientList :
    ReadOnlyListBase<PatientList, PatientDTO>
  {
    #region Factory Methods
    public static PatientList GetPatientList()
    {
      return DataPortal.Fetch<PatientList>(new Criteria());
    }
    public static PatientList GetPatientList(string name)
    {
      return DataPortal.Fetch<PatientList>
        (new FilteredCriteria(name));
    }

    private PatientList()
    { /* require use of factory methods */ }

    #endregion

    #region Data Access

    [Serializable()]
    private class Criteria
    { /* no criteria - retrieve all patients */ }

    [Serializable()]
    private class FilteredCriteria
    {
      private string _name;
      public string Name { get { return _name; }}

      public FilteredCriteria(string name){ _name = name;}
    }

    private void DataPortal_Fetch(Criteria criteria)
    {
      Fetch("");
    }

    private void DataPortal_Fetch(FilteredCriteria criteria)
    {
      Fetch(criteria.Name);
    }
    private void Fetch(string nameFilter)
    {

        PatientDB DAL = new PatientDB();
        List<PatientDTO> patientsList;
        patientsList = DAL.GetPatients() as List<PatientDTO>;

        foreach (PatientDTO patient in patientsList)
        {
            try
            {
                if ((nameFilter.Length == 0) || (patient.FirstName.IndexOf(nameFilter) > 0) || (patient.LastName.IndexOf(nameFilter) > 0))
                    this.Add(patient);  // Getting Error on this line "Insert is an invalid operation
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        this.RaiseListChangedEvents = true;
    }

    #endregion
  }

can any one tell wat will be the problem

 

Thank You

 

McManus replied on Monday, October 01, 2007

Hi,

Your list class is derived from ReadOnlyListBase. By default, you cannot add items to a read only list.

In your Fetch method, before adding items to the list, you should set IsReadOnly to false. When all items are added to the list, set IsReadOnly back to true.

Cheers,
Herman

Copyright (c) Marimer LLC