Link to home
Start Free TrialLog in
Avatar of metropia
metropiaFlag for United States of America

asked on

how to access listview value on delete command

I would like to ask for help regarding an asp page with a list view.

I want to get the Id value for the selected row, so that I can query the entity framework, get the record and delete it.

Down below is the code for when I do the e.Command = "Delete"
        If e.CommandName = "Delete" Then

            Dim myContext As New OLTPEntities
            Dim myRecipeStepsHistory = myContext.RefineRecipeStep.Where("it.Recipe_Id=" & Me.lvRecipeSteps.FindControl("Recipe_Id").ToString())
            Dim strRefineRecipeStepRecord = New StringBuilder("")

            If Not IsNothing(myRecipeStepsHistory) Then
                ' Recipe_Id
                strRefineRecipeStepRecord.Append("Recipe_Id: " & Me.lvRecipeSteps.FindControl("Recipe_Id").ToString() & ", ")

Open in new window

I get an Object reference not set to an instance of an object, message.

Dim myRecipeStepsHistory = myContext.RefineRecipeStep.Where("it.Recipe_Id=" & Me.lvRecipeSteps.FindControl("Recipe_Id").ToString())  << this line, I thinkis because I am not looking for the right control?

I have attached a copy of my aspx code. Hopefully some one will be able to provide guidance on this problem.

Thank you much.
listview-aspx-code.txt
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

What event are you using to do the delete? The ListView Deleting event passes a Keys collection as part of it EventArgs. That collection will include the ID of the row being deleted.
Avatar of metropia

ASKER

I started using:
Private Sub lvRecipeSteps_ItemCommand(sender As Object, e As ListViewCommandEventArgs) Handles lvRecipeSteps.ItemCommand

Open in new window

And then check the Command Name:
If e.CommandName = "Delete"

Open in new window

I now changed the code to use ItemDeleting event, because I have a DataKeyNames="Id"
defined, but I have not been able to figure out how to access it and then query the Entities.
Private Sub lvRecipeSteps_ItemDeleting(sender As Object, e As ListViewDeleteEventArgs) Handles lvRecipeSteps.ItemDeleting
Dim myContext As New OLTPEntities
Dim myRefineRecipeSteps = myContext.RefineRecipeStep.Where("it.Id=" & Me.lvRecipeSteps.DataKeys().ToString())   << this does not work

Open in new window

I am still not able to make it work. I hope you can offer some help.

I really need it.

Thank you
This is my current code and my error.

User generated image
SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial