Solved

how to access listview value on delete command

Posted on 2014-01-29
6
503 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 

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

Industry Leaders: 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!

Question has a verified solution.

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

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…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

749 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