Error in executing action of deleting and updating data from GridView
-
I'm developing an e-commerce web-based system & have problem in Shopping Cart part. i.e. update quantity & delete item from cart. This is code-behind for my Cart.aspx. <pre> 'other code above Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting cart.DeleteItem(e.RowIndex) 'error here GridView1.DataBind() End Sub Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing GridView1.EditIndex = e.NewEditIndex GridView1.DataBind() End Sub Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating Dim cell As DataControlFieldCell cell = GridView1.Rows(e.RowIndex).Controls(3) Dim t As TextBox = cell.Controls(0) Try Dim q As Integer q = Integer.Parse(t.Text) cart.UpdateQuantity(e.RowIndex, q) 'error here Catch ex As FormatException e.Cancel = True End Try GridView1.EditIndex = -1 GridView1.DataBind() End Sub Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit e.Cancel = True GridView1.EditIndex = -1 GridView1.DataBind() End Sub 'other code below </pre> The error saying that: Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Thanks for your help!
-
I'm developing an e-commerce web-based system & have problem in Shopping Cart part. i.e. update quantity & delete item from cart. This is code-behind for my Cart.aspx. <pre> 'other code above Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting cart.DeleteItem(e.RowIndex) 'error here GridView1.DataBind() End Sub Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing GridView1.EditIndex = e.NewEditIndex GridView1.DataBind() End Sub Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating Dim cell As DataControlFieldCell cell = GridView1.Rows(e.RowIndex).Controls(3) Dim t As TextBox = cell.Controls(0) Try Dim q As Integer q = Integer.Parse(t.Text) cart.UpdateQuantity(e.RowIndex, q) 'error here Catch ex As FormatException e.Cancel = True End Try GridView1.EditIndex = -1 GridView1.DataBind() End Sub Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit e.Cancel = True GridView1.EditIndex = -1 GridView1.DataBind() End Sub 'other code below </pre> The error saying that: Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Thanks for your help!
miss nadia wrote:
cart.DeleteItem(e.RowIndex) 'error here
Where is that function ?
Best Regards ----------------- Abhijit Jana "Success is Journey it's not a destination"
-
miss nadia wrote:
cart.DeleteItem(e.RowIndex) 'error here
Where is that function ?
Best Regards ----------------- Abhijit Jana "Success is Journey it's not a destination"
That function is in ShoppingClass class.
-
miss nadia wrote:
cart.DeleteItem(e.RowIndex) 'error here
Where is that function ?
Best Regards ----------------- Abhijit Jana "Success is Journey it's not a destination"
this is the function in ShoppingCart class.
Imports Microsoft.VisualBasic
Imports System.Collections.GenericPublic Class ShoppingCart
Private \_cart As List(Of CartItem) Public Sub New() \_cart = New List(Of CartItem)() End Sub Public Function GetItems() As List(Of CartItem) Return \_cart End Function Public Sub AddItem(ByVal id As String, ByVal name As String, ByVal price As Decimal) Dim itemFound As Boolean = False For Each Item As CartItem In \_cart If Item.ID = id Then Item.Quantity += 1 itemFound = True End If Next If Not itemFound Then Dim item As CartItem item = New CartItem(id, name, price, 1) \_cart.Add(item) End If End Sub Public Sub UpdateQuantity(ByVal index As Integer, ByVal quantity As Integer) 'update quantity function Dim item As CartItem item = \_cart(index) item.Quantity = quantity End Sub Public Sub DeleteItem(ByVal index As Integer) 'delete item function \_cart.RemoveAt(index) End Sub Public ReadOnly Property Count() As Integer Get Return \_cart.Count End Get End Property
End Class