Linq to Sql: Error on InsertOnSubmit

1k views Asked by At
   private Table<Gallery> galleryTable;
   public GalleryRepository ( string connectionString ) {
        dc = new DataContext(connectionString);
        galleryTable = dc.GetTable<Gallery>();
    }

    public void SaveGallery(Gallery gallery) {

        if (gallery.GalleryId == 0)
            galleryTable.InsertOnSubmit(gallery);
        else if (galleryTable.GetOriginalEntityState(gallery) == null) {
            galleryTable.Attach(gallery);
            galleryTable.Context.Refresh(RefreshMode.KeepCurrentValues, gallery);
        }
        galleryTable.Context.SubmitChanges();
    }

When inserting a new gallery into the table, the method throws a Object reference not set to an instance of an object error. The gallery is not null and neither is the galleryTable Thanks in advance

1

There are 1 answers

0
Collin O'Connor On BEST ANSWER

So the problem was with my Gallery entity I had

   private EntitySet<Tag> _Tags;

    [System.Data.Linq.Mapping.Association(Storage = "_Tags", OtherKey = "TagId")]
    public EntitySet<Tag> Tags
    {
        get { return this._Tags; }
        set { this._Tags.Assign(value); }
    }

and it was throwing a null reference at this._Tags.Assign(value); So i assigned a blank EntitySet to the _Tags variable and problem solved