Link to home
Create AccountLog in
Avatar of bluem1
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:

 <asp:DataList ID="DataList1" runat="server" DataKeyField="cartid" 
        DataSourceID="SqlDataSource1" Width="698px">

Open in new window


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

Open in new window


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
Avatar of kaufmed
kaufmed
Flag of United States of America image

You missed one important point in that example code:

The event handler might look like this, if the data source were a DataTable object

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?
Avatar of bluem1
bluem1

ASKER

Thanks for the input.  The event handler as a button inside the datalist.

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

Open in new window

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?
Avatar of bluem1

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.    
Avatar of bluem1

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?
Are you able to grab the primary key using the e.Item?
Avatar of bluem1

ASKER

That's part of my problem.  Not exactly shure what that would look like.

Dewayne
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of bluem1

ASKER

That was the ticket!  thanks for the help.
Glad to help :-)