Solved

how to access listview value on delete command

Posted on 2014-01-29
6
507 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

724 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