bluem1
asked on
delete row from datalist
Team -
This has to be way more simple that I am getting. Been searching google for hours with no luck.
Simply stated, I am trying to delete a datalist row which is bound to a mssql database. Simple right!
I am missing where it is putting the key or something because I get getting the error "Object reference not set to an instance of object". I can't figure out how to give it the object!
Page code has the datakey:
Code behind has the code from msdn:
But still getting the error. I am guessing the key isn't getting passed.
I am using asp.net (VB, with Visual Studio 2010, .net 3.5)
Thanks for helping!
Dewayne
This has to be way more simple that I am getting. Been searching google for hours with no luck.
Simply stated, I am trying to delete a datalist row which is bound to a mssql database. Simple right!
I am missing where it is putting the key or something because I get getting the error "Object reference not set to an instance of object". I can't figure out how to give it the object!
Page code has the datakey:
<asp:DataList ID="DataList1" runat="server" DataKeyField="cartid"
DataSourceID="SqlDataSource1" Width="698px">
Code behind has the code from msdn:
Dim aTable As DataTable
aTable = CType(DataList1.DataSource, DataTable)
aTable.Rows(e.Item.ItemIndex).Delete()
'reference from here http://msdn.microsoft.com/en-us/library/b7y72882(v=vs.71).aspx
But still getting the error. I am guessing the key isn't getting passed.
I am using asp.net (VB, with Visual Studio 2010, .net 3.5)
Thanks for helping!
Dewayne
ASKER
Thanks for the input. The event handler as a button inside the datalist.
Here is the entire code block
Here is the entire code block
Private Sub DataList1_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles DataList1.DeleteCommand
Dim aTable As DataTable
aTable = CType(DataList1.DataSource, DataTable)
aTable.Rows(e.Item.ItemIndex).Delete()
'reference from here http://msdn.microsoft.com/en-us/library/b7y72882(v=vs.71).aspx
End Sub
If you are processing this in a DeleteCommand handler, then shouldn't you be removing the data from the DataSource itself (i.e. the database)? What you are trying to do with your current code is delete a record from the already populated GridView, DataList, etc.; nothing would actually be deleted from the DB with this method.
What is the business goal of the code?
What is the business goal of the code?
ASKER
This is being processed by the DeleteCommand handler. The item in question would actually already be in the database as a single row with a key, shown within a datalist on the page as the standard "Items In your cart" type page.
Working on a shopping cart. The item would already been in the database, pending check out unless deleted/removed.
I would want this item removed from the database but I can't seem to figure out how.
I hope I am making sense.
Thank you for your time.
Working on a shopping cart. The item would already been in the database, pending check out unless deleted/removed.
I would want this item removed from the database but I can't seem to figure out how.
I hope I am making sense.
Thank you for your time.
ASKER
I haven't heard back from you so I wanted to check in and make sure what I posted made sense.
I understand what you are saying about deleting from the source however I am not sure how that could would look. Any suggestions?
I understand what you are saying about deleting from the source however I am not sure how that could would look. Any suggestions?
Are you able to grab the primary key using the e.Item?
ASKER
That's part of my problem. Not exactly shure what that would look like.
Dewayne
Dewayne
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
That was the ticket! thanks for the help.
Glad to help :-)
Your DataSource is a SqlDataSource, not a DataTable, so the CType() is not succeeding due to an invalid cast. Where (in what method) are you trying to do this deletion?