Hi all I'm currently developing a site that requires a shopping cart, so I developed a class that handles all the shopping functions. Then user controls that needed to access the shopping cart class inherit from a base class that has a public read only property
of type shoppingCart which retrieves the current cart from the session cache or creates a new one if there isn't one in there (if you've looked at Duwamish 7 its the same as that). So all is well I inherit the base class in my user controls build it run it
all works..great. But then if I close down my user control and try to re open it I get the following error. "The file failed to load in the web form designer. Please correct the following error then load it again: Property accessor 'cart' on object cartControl
threw the following exception:'object reference not set to instance of object'" my code is as follows: base class :- Imports System Imports System.Web Imports System.Web.UI Imports System.Web.UI.WebControls Imports System.ComponentModel Imports artblends.businessObject.components
Namespace businessObject.components Public Class moduleBase Inherits UserControl Public ReadOnly Property cart() As shoppingCart Get cart = CType(Session.Item("shoppingCart"), shoppingCart) If cart Is Nothing Then cart = New shoppingCart() Session.Add("shoppingCart",
cart) End If End Get End Property End Class End Namespace user control :- Imports System Imports System.Web Imports System.Web.UI Imports System.Web.UI.WebControls Imports System.ComponentModel Imports artblends.businessObject.components Public MustInherit
Class cartControl Inherits moduleBase Protected WithEvents txtID As System.Web.UI.WebControls.TextBox Protected WithEvents Button1 As System.Web.UI.WebControls.Button Protected WithEvents txtName As System.Web.UI.WebControls.TextBox Protected WithEvents txtPrice
As System.Web.UI.WebControls.TextBox Protected WithEvents Button2 As System.Web.UI.WebControls.Button Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid #Region " Web Form Designer Generated Code " 'This call is required by the Web Form Designer.
Private Sub InitializeComponent() End Sub Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: This method call is required by the Web Form Designer 'Do not modify it using the code editor. InitializeComponent()
End Sub #End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Put user code to initialize the page here End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button2.Click DataGrid1.DataSource = cart DataGrid1.DataBind() End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click cart.addItem(txtID.Text, txtName.Text, CType(txtPrice.Text, Decimal))
End Sub End Class (ignore the crappy naming of things it was just knocked up as a test) Like I say everything works ok once but when I close down the control in the designer and try to reopen it thats when the problem occurs, I know the problem is to do with
property, and if I take it out and rebuild the solution it works again. I have picked through the duwamish example with a fine tooth comb (which probably means I've missed something) to see if I have done something differeent but I haven't. This is probably
something really simple but I just can't see what it is. Any help would be much appreciated, thanks in advance Dan
You need to make sure public properties are not trying to access a non-existent context in the designer -- like request and response, or session in your case, do not exist at design time -- test if nothing:
Public ReadOnly Property cart() As shoppingCart
Get
If Not Context Is Nothing Then
cart = CType(Session.Item("shoppingCart"), shoppingCart)
If cart Is Nothing Then
cart = New shoppingCart()
Session.Add("shoppingCart", cart)
End If
End If
End Get
End Property
Thanks, Paul Wilson, ASPInsider, MC**
For the best .NET code, examples, and tools, visit:
WilsonDotNet.com, WilsonWebPortal.com, ORMapper.net
cheers Paul I've been scratching my head over that, I got a work around with a try catch statement, but still wanted to know why. The main head banger was that it seemed to work in the duwamish 7 example but I couldn't get it to work, this is the code from
duwamish Public ReadOnly Property ShoppingCart(Optional ByVal forceCreate As Boolean = True) As Cart Get ' ' Try to get the cart from the Session object ' ShoppingCart = CType(Session.Item(KEY_CACHECART), Cart) If ShoppingCart Is Nothing Then ' ' If there
is no cart, create it now ' ShoppingCart = New Cart() ' ' Save it for later ' Session.Add(KEY_CACHECART, ShoppingCart) End If If forceCreate Then ShoppingCart.EnsureWritable End Get End Property but some how they get it to work without checking for the context
first unless its something I've over looked somewhere else in the example. thanks again Dan
dannyboy78
Member
160 Points
32 Posts
User controls inheriting from a base class
Apr 23, 2003 08:35 AM|LINK
PaulWilson
Contributor
3715 Points
745 Posts
ASPInsiders
Re: User controls inheriting from a base class
Apr 23, 2003 10:30 AM|LINK
Public ReadOnly Property cart() As shoppingCart Get If Not Context Is Nothing Then cart = CType(Session.Item("shoppingCart"), shoppingCart) If cart Is Nothing Then cart = New shoppingCart() Session.Add("shoppingCart", cart) End If End If End Get End PropertyFor the best .NET code, examples, and tools, visit:
WilsonDotNet.com, WilsonWebPortal.com, ORMapper.net
dannyboy78
Member
160 Points
32 Posts
Re: User controls inheriting from a base class
Apr 23, 2003 09:05 PM|LINK
Ibexx
Member
15 Points
3 Posts
Re: User controls inheriting from a base class
Dec 12, 2003 08:02 PM|LINK