tags:

views:

165

answers:

4

I am testing subsonic 3, i can query my database but when i am inserting a record i have an exception. Here is my code:

Client lClient = new Client(); lClient.Name = "Peter"; lClient.FullName = "Richards"; lCliente.Save();

And i have a null reference exception on this generated code:

var newKey=_repo.Add(this,provider);

Any help is appreciated.

I am using ActiveRecords

A: 

Where does the null reference exception actually happen? Is _repo null?

Try and double check that you don’t have any columns in the “Client” table which are not nullable and you don’t set any value.

Also make sure you PK is handled correctly (check you have the [Property.IsForeignKey=true;] attribute)

Flo
A: 

Do you have NullReferenceException Checked in (file menu) Debug -> Exceptions (press Find and type NullReferenceException, press OK)?

I downloaded the SubSonic code and used it as reference for my project, the exception is thrown in SubSonic.Core\Repository\SubSonicRepository.cs:209 because prop is not checked for null at line 208, and any exceptions are swallowed as evidenced by line 213.

What happens is visual studio breaks on all the exceptions checked in the mentioned dialog. So in one way, this is the expected behaviour from the code.

What actually causes prop to become null, in my case, the table field name is lower-case, but the property is actually cased properly.

From Immediate:

tbl.PrimaryKey.Name
"playerinformationid"

item.GetType().GetProperties()  
{System.Reflection.PropertyInfo[11]}  
    [0]: {System.Collections.Generic.IList`1[SubSonic.Schema.IColumn] Columns}  
    // *snip*  
    [5]: {Int32 PlayerInformationid}
    // *snip*

item.GetType().GetProperty("PlayerInformationid")
{Int32 PlayerInformationid} // Works!

item.GetType().GetProperty(tbl.PrimaryKey.Name)
null

I hacked this together to "just make it work" (Warning: newbie at 3.5 stuff)

-var prop = item.GetType().GetProperty(tbl.PrimaryKey.Name);
+var lowerCasedPrimaryKeyName = tbl.PrimaryKey.Name.ToLowerInvariant();
+var prop = item.GetType().GetProperties().First<System.Reflection.PropertyInfo>(x => x.Name.ToLowerInvariant() == lowerCasedPrimaryKeyName);
CS
A: 

Does your table have a PrimaryKey? It doesn't sound like it does. If not - this is your problem.

Rob Conery
In my case I do have a PK, which is named playerinformationid.The only thing I can think of is the CleanUp-method, because I do a result = tableName.Replace("Playerinformation","PlayerInformation"). That would explain why the 'id' is still in lower-case and not PlayerInformation**Id**. I'm still barely getting to know SubSonic 3 so I might play way outside its fields.
CS
+1  A: 

I had a similar problem, with code not unlike the following:

var pending = myTable.SingleOrDefault(x => x.Id == 1);
if (pending == null)
    pending = new myTable();

pending.Id = 1;
pending.MyDate = DateTime.Now;
pending.MyString = someString;
pending.Save();

This worked the first time I ran it, on an empty table, but not the second time when updating. I got a nullreference exception somewhere inside the subsonic repository. The solution was to add a primary key, as Rob suggested.

Just thought I'd mention it (thanks Rob).

louieoc