Adapter SaveEntity with Relation

Posts   
 
    
tprohas
User
Posts: 257
Joined: 23-Mar-2004
# Posted on: 18-Nov-2004 20:10:42   

I'm trying to update data in two tables that are related via a one to one relationship and keep getting an error when trying to set a property value of the related entity.

In the code below I'm trying to update the fields in BottomBracket and ProductVersion tables for a specific ProductVersionID. Everything works fine until I try and call the property bottomBracket.ProductVersion.ActiveState. When this code is executed an error is thrown saying:

"Object reference not set to an instance of an object."

What do I need to do to update fields in two tables with a one to one relationship?


int productVersionID = Convert.ToInt32(e.Item.Cells[1].Text);
BottomBracketEntity bottomBracket = new BottomBracketEntity( this.productID, productVersionID );
bottomBracket.IsNew = false;
bottomBracket.ProductVersion.ActiveState = ((TextBox)e.Item.FindControl("activeState")).Text;
bottomBracket.SpindleLength = Convert.ToDecimal( ((TextBox)e.Item.FindControl("spindleLength")).Text );
bottomBracket.SpindleSplines = Convert.ToDecimal( ((TextBox)e.Item.FindControl("spindleSplines")).Text );
bottomBracket.Width = Convert.ToDecimal( ((TextBox)e.Item.FindControl("width")).Text );
bottomBracket.WidthMax = Convert.ToDecimal( ((TextBox)e.Item.FindControl("widthMax")).Text );
DataAccessAdapter adapter = new DataAccessAdapter();
adapter.SaveEntity( bottomBracket );

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 18-Nov-2004 20:29:17   

Could you please post the stacktrace? simple_smile

Frans Bouma | Lead developer LLBLGen Pro
tprohas
User
Posts: 257
Joined: 23-Mar-2004
# Posted on: 18-Nov-2004 20:44:27   

Otis wrote:

Could you please post the stacktrace? simple_smile

Here is the stacktrace. I don't know if these helps, but d:\inetpub\wwwroot\partseditor.bottombracket\detail.aspx.cs:81 is:

bottomBracket.ProductVersion.ActiveState = ((TextBox)e.Item.FindControl("activeState")).Text;

STACK TRACE:

[NullReferenceException: Object reference not set to an instance of an object.] PartsEditor.BottomBracket.Detail.UpdateItemClick(Object sender, DataGridCommandEventArgs e) in d:\inetpub\wwwroot\partseditor.bottombracket\detail.aspx.cs:81 System.Web.UI.WebControls.DataGrid.OnUpdateCommand(DataGridCommandEventArgs e) System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs e) System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source, EventArgs e) System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) System.Web.UI.Page.ProcessRequestMain()

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 18-Nov-2004 22:28:39   

That stacktrace seems to suggest the exception is originating in your code, could you step through teh code please to see where the code actually breaks?

Frans Bouma | Lead developer LLBLGen Pro
tprohas
User
Posts: 257
Joined: 23-Mar-2004
# Posted on: 18-Nov-2004 22:51:27   

Otis wrote:

That stacktrace seems to suggest the exception is originating in your code, could you step through teh code please to see where the code actually breaks?

The problem is originating in my code, I just don't know why. The problem is on line 81:

Line 79: BottomBracketEntity bottomBracket = new BottomBracketEntity( this.productID, productVersionID ); Line 80: bottomBracket.IsNew = false; Line 81: bottomBracket.ProductVersion.ActiveState = ((TextBox)e.Item.FindControl("activeState")).Text; Line 82: bottomBracket.IsFixedCup = ((CheckBox)e.Item.FindControl("isFixedCup")).Checked; Line 83: bottomBracket.SpindleLength = Convert.ToDecimal( ((TextBox)e.Item.FindControl("spindleLength")).Text );

I believe the problem is the bottomBracket.ProductVersion entity property. It seems like there is not an instance of the ProductVersion entity. Does this help?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 18-Nov-2004 23:09:22   

Yes that entity isn't there, as it isn't fetched by default. Adapter doesn't fetch entities automatically, selfservicing does. Perhaps that's the mistake you made. Use a prefetch path to fetch both entities in 1 go. simple_smile

Frans Bouma | Lead developer LLBLGen Pro
tprohas
User
Posts: 257
Joined: 23-Mar-2004
# Posted on: 18-Nov-2004 23:16:44   

OK, I am now able to save these two related entities together. The solution was to fetch the related BottomBracket and ProductVersion entities first, change the field values and then save it back to the database. It would be really nice if there was a way to save both related entities without fetching the data first, is this possible?

Below is the working code.


            DataAccessAdapter adapter = new DataAccessAdapter();
            int productVersionID = Convert.ToInt32(e.Item.Cells[1].Text);
            BottomBracketEntity bottomBracket = new BottomBracketEntity( this.productID, productVersionID );
            adapter.FetchEntity( bottomBracket );
            bottomBracket.ProductVersion = (ProductVersionEntity)adapter.FetchNewEntity( new ProductVersionEntityFactory(), bottomBracket.GetRelationInfoProductVersion() );
            bottomBracket.ProductVersion.ActiveState = ((TextBox)e.Item.FindControl("activeState")).Text;
            bottomBracket.IsFixedCup = ((CheckBox)e.Item.FindControl("isFixedCup")).Checked;
            bottomBracket.SpindleLength = Convert.ToDecimal( ((TextBox)e.Item.FindControl("spindleLength")).Text );
            bottomBracket.SpindleSplines = Convert.ToDecimal( ((TextBox)e.Item.FindControl("spindleSplines")).Text );
            bottomBracket.Width = Convert.ToDecimal( ((TextBox)e.Item.FindControl("width")).Text );
            bottomBracket.WidthMax = Convert.ToDecimal( ((TextBox)e.Item.FindControl("widthMax")).Text );
            adapter.SaveEntity( bottomBracket );

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 19-Nov-2004 08:27:22   

If both are new, you can of course create 2 new entities, set the values, do bottomBracket.ProductVersion = newProductVersion and save bottomBracket, but I think that's not what you were asking for. simple_smile

You want to update the entities but you don't want them to fetch into memory? You could try updating the entities directly in the database, using adapter.UpdateEntitiesDirectly().

Frans Bouma | Lead developer LLBLGen Pro