Solved

how to access listview value on delete command

Posted on 2014-01-29
6
491 Views
Last Modified: 2014-01-30
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
0
Comment
Question by:metropia
  • 3
  • 2
6 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39818149
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.
0
 

Author Comment

by:metropia
ID: 39818189
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

0
 

Author Comment

by:metropia
ID: 39818277
I am still not able to make it work. I hope you can offer some help.

I really need it.

Thank you
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:metropia
ID: 39818301
This is my current code and my error.

screen shot error - current
0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 250 total points
ID: 39820297
You don't need to cast the data key to a textbox. Try:
Dim myRefineRecipeSteps = myContext.RefineRecipeStep.Where("it.Id=" & currentDataKey.Value).FirstOrDefault()

Open in new window

0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 250 total points
ID: 39820508
Try changing

Dim myRefineRecipeSteps = myContext.RefineRecipeStep.Where("it.Id=" & Me.lvRecipeSteps.DataKeys().ToString())


to

Dim myRefineRecipeSteps = myContext.RefineRecipeStep.Where("it.Id=" & e.Keys("ID"))
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now