iRepository Delete

Topics: Writing modules
Aug 19, 2011 at 7:32 PM

This is driving me absolutely crazy because I can't seem to figure out why this isn't working.

I'm using a Telerik MVC grid and doing an Ajax delete with the same code that seems to work on every other table except this one. Here is the Model and Migration

 

using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace InfoSource.Models
{
    public class ReportConfigRecord
    {
        public virtual int Id { get; set; }
        public virtual ReportRecord Report { get; set; }
        public virtual ReportOptionRecord ReportOption { get; set; }
        public virtual ReportOptionValueRecord ReportOptionValue { get; set; }
        public virtual int OptionSort { get; set; }
    }
}

 

 

            // Creating Table ReportConfigRecord
            SchemaBuilder.CreateTable("ReportConfigRecord", table => table
                .Column("Id", DbType.Int32, column => column.PrimaryKey().Identity())
                .Column("Report_id", DbType.Int32)
                .Column("ReportOption_id", DbType.Int32)
                .Column("ReportOptionValue_id", DbType.Int32)
                .Column("OptionSort", DbType.Int32)
            );

 

Report_id connects back to the ReportRecord table, ReportOption_id connects back to the ReportOptionRecord table and ReportOptionValue_id connects back to the ReportOptionValueRecord table.

My Controller action is simply...

 

_setupService.DeleteOption(Id);

I have verified in the Ajax Post that an Id of 33 is being passed. I have verified with a breakpoint in the controller that Id is 33 and I have verified in the Service that Id is 33 when it runs this...

_reportconfigRepository.Delete(_reportconfigRepository.Get(Id));

Yet the error that is being returned from the server is: 

Object reference not set to an instance of an object

and the record is never deleted from the table. If you look at the table, there is indeed a record in that table with an Id of 33.

Am I missing something? Is there another way to delete a record using iRepository that may resolve this? Again, I'm using the same code on several other tables and it works without issue.

Thanks for any insight.

 

Aug 19, 2011 at 7:37 PM

I changed the Service code to match exactly the other methods so it looks like this now, but I still get the same error

            ReportConfigRecord findConfig = _reportconfigRepository.Get(Id);
            if (findConfig != null)
            {
                _reportconfigRepository.Delete(findConfig);
            }

Coordinator
Aug 19, 2011 at 7:44 PM

Which reference is null ?

Aug 19, 2011 at 8:02 PM

I can't seem to figure it out with debugging...I would assume from the error and what the method is doing, that findConfig is null, but if that's the case, then it should never try to run the Delete method...correct?

There's so little in this code that findConfig seems like it's the only thing that could be null since I know Id is equal to 33. 

So...my assumption is that findConfig is coming back with a ReportConfigRecord object, otherwise it wouldn't run the delete code...but for some reason the iRepository.Delete isn't recognizing findConfig as a valid ReportConfigRecord object. Is that even possible?

Aug 19, 2011 at 8:07 PM

Double checked my Repository declarations...

        public IOrchardServices _orchardServices { get; set; }
        private readonly IRepository<ReportRecord> _reportRepository;
        private readonly IRepository<ReportTypeRecord> _reporttypeRepository;
        private readonly IRepository<ReportTypesRecord> _reporttypesRepository;
        private readonly IRepository<ReportOptionRecord> _reportoptionRepository;
        private readonly IRepository<ReportOptionValueRecord> _reportoptionvalueRepository;
        private readonly IRepository<BenefitRecord> _benefitRepository;
        private readonly IRepository<ReportBenefitsRecord> _reportbenefitsRepository;
        private readonly IRepository<ReportConfigRecord> _reportconfigRepository;
        public SetupService(IOrchardServices orchardServices, IRepository<ReportRecord> reportRepository, IRepository<ReportTypeRecord> reporttypeRepository, IRepository<ReportTypesRecord> reporttypesRepository, IRepository<ReportOptionRecord> reportoptionRepository, IRepository<ReportOptionValueRecord> reportoptionvalueRepository, IRepository<BenefitRecord> benefitRepository, IRepository<ReportBenefitsRecord> reportbenefitsRepository, IRepository<ReportConfigRecord> reportconfigRepository)
        {
            _orchardServices = orchardServices;
            _reportRepository = reportRepository;
            _reporttypeRepository = reporttypeRepository;
            _reporttypesRepository = reporttypesRepository;
            _reportoptionRepository = reportoptionRepository;
            _reportoptionvalueRepository = reportoptionvalueRepository;
            _benefitRepository = benefitRepository;
            _reportbenefitsRepository = reportbenefitsRepository;
            _reportconfigRepository = reportconfigRepository;
        }
Looks correct to me

Aug 19, 2011 at 8:10 PM
Edited Aug 19, 2011 at 8:10 PM

and here's the entire method if you want to see it

 

        public void DeleteOption(int Id)
        {
            ReportConfigRecord findConfig = _reportconfigRepository.Get(Id);
            if (findConfig != null)
            {
                _reportconfigRepository.Delete(findConfig);
            }
        }

as you can see...not much to it

Coordinator
Aug 19, 2011 at 8:17 PM

Well if you can see the Id is 33, then you can also check which object is null. Just type the name of each reference inside your Watch window while debugging, and you will see which object is null.

Aug 19, 2011 at 8:18 PM

Let me give that a try...not that great with Visual Studio debugging

Aug 19, 2011 at 8:23 PM

Okay... the value of findConfig is {InfoSource.Models.ReportConfigRecord}

and that's what it should be

Aug 19, 2011 at 8:54 PM

ARG...I can't find any object that is showing as NULL...everything has a value

Aug 19, 2011 at 9:45 PM

Nevermind...It's Telerik's control. When you put the Grid into Client Operation Mode so you can do grouping, it throws this wonderfully descriptive error that has absolutely NOTHING to do with Orchard. I'll go bug them and leave you wonderfully helpful folks alone =)

and yes...there is a pile of hair under my desk from pulling it out for the past 3 hours.