mercredi 17 juin 2015

Data is not for 2nd time saving when first time it throws an exception

Following is the code segment I used to save two objects to same data.here Comment field is mandotary field.and the DataContext object instance is intilialized in the constructor of the class. When I give comment field as "NULL" for the Obj1 saving both objects are failing but when I give Obj1.Comment="somthing" and Obj2.Comment=NULL the first one is save to the database. I can avoid this declaring a new data context inside the "SaveObj" method.But I want to know why is this happening.does this DataContext going to an error state ? Does any one has idea the reason to happen to this

 public bool SaveObjectList(exemp List)
    {
        try
        {
            SaveObject(List.object1);
            SaveObject(List.object2);
        }
        catch
        {
            return false; 
        }
        return true;
    }

   public bool SaveObject(Exemp object)
   {  
          try{
                Table newObj = new Table()
                {
                    Id = Guid.NewGuid(),
                    Comment = exemption.Comment
                };
                newContext.Table.InsertOnSubmit(newObj);                                                                           
                newContext.SubmitChanges();
            }
         catch{
            return false;
            }
      return true;
    }

Aucun commentaire:

Enregistrer un commentaire