views:

465

answers:

3

Whenever I attempt to use a simple repository to simply update a record, I get an object reference not set to an instance of an object error. I suspect that I am doing something incorrect, can anyone see a problem with the below lines of code for saving data to subsonic?

I'm using .Net 3.5, SQL Server 2008 and Subsonic 3.0.

Thanks for any help.

        SimpleRepository repo = new SimpleRepository("Test", SimpleRepositoryOptions.RunMigrations);

        //Add the test object
        SimpleObject simple = new SimpleObject();
        simple.TestString = "Test";
        repo.Add(simple);

        Console.WriteLine(simple.TestString);

        //Reload the object and update it
        SimpleObject simpleReloaded = repo.Single<SimpleObject>(simple.ID);
        simpleReloaded.TestString = "Editted";
        repo.Update(simpleReloaded);//This line seems to crash repeatedly

        Console.WriteLine(repo.Single<SimpleObject>(simple.ID).TestString);
+2  A: 

My guess is that for some reason you're getting a new object on the line:

SimpleObject simpleReloaded = repo.Single<SimpleObject>(simple.ID);

So when you try and update it SubSonic is throwing an exception because simpleReloaded doesn't have a primary key. Try checking the value of simple.ID and then see if simpleReloaded is actually populated or whether you're just getting a new SimpleObject

EDIT: Looks like this is a bug try pulling the latest version from github

Adam
Thanks for the response, I don't think its that though, the ID values are the same for both objects:Value of simple.ID: 2Value of simpleReloaded.ID = 2Any other ideas?
grrrrrrrrrrrrr
What happens if you change the value of TestString on simple and then update that? Also can you post some more details about your crash, stack trace and the sql generated by the Update would be most useful.
Adam
Can you also confirm you're using the latest version of SubSonic - 3.0.0.3. There's quite a few bugs in 3.0 that have been fixed.
Adam
Looks like a bug as someone else is having the exact same problem
Adam
Just got the latest version from the github link you sent me, built it and it works. I guess it was a bug. Thanks for your help.
grrrrrrrrrrrrr
A: 

get the daily build here, to address your issue (as Adam said above):

http://github.com/subsonic/SubSonic-3.0/tree/master

you need to compile to extract the SubSonic.Core.dll

No Body
A: 

Hi,

I am getting the exact same error as grrrrrrrrrrrrrr described.

I'm using .Net 3.5, SQL Server 2008 Express, SubsonicCore.dll - 3.0.0.3

Once you I try to update the existing record I also get the "object reference not set to an instance of an object" error.

Here is the stacktrace from the inner exception:

at SubSonic.Query.Update.GetCommand()
at SubSonic.Query.Update.Execute()
at SubSonic.Repository.SimpleRepository.Update[T](T item)
at _Default.Page_Load(Object sender, EventArgs e) in d:\Test\Test\Default.aspx.cs:line 37
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)

I've also tried it with both Guid and int data types as primary keys but it doesn't make any difference.

Please let me know if you guys managed to get it sorted out or what?

Thanks a lot.

Ryno Burger
As Adam suggested get the latest version from github and it should work.
grrrrrrrrrrrrr
Just got the latest build from github and still get the same error. Any ideas perhaps? Thanks
Ryno Burger