Solved

How do I write the code to delete a table row in my db from my MVC controller? (ASP.NET, MVC)

Posted on 2016-10-25
7
71 Views
Last Modified: 2016-10-26
I'm using the Entity framework in my MVC project. I would like to delete a record when the user navigates to a specific controller. What is wrong with my code below? And how can I accomplish this?

The table name is: fooItem

MYENTITIES oe = new MYENTITIES();
fooItem j = oe.fooItems.ToList().Where(d => d.fooItemId == Id).First();

oe.fooItems.Remove(j);
oe.SaveChanges();

Open in new window

0
Comment
Question by:bmanmike39
[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
7 Comments
 
LVL 15

Assisted Solution

by:Lokesh B R
Lokesh B R earned 250 total points
ID: 41859828
Hi,

try the below code

fooItem f;

using (var ctx = new MYENTITIES())
{
    f = ctx.fooItems.Where(s => s.fooItemId == Id).FirstOrDefault();
}

if(f != null)
{
   using (var newContext = new MYENTITIES())
   {
        newContext.Entry(f).State = System.Data.Entity.EntityState.Deleted;    

        newContext.SaveChanges();
    }
}

Open in new window

0
 
LVL 51

Expert Comment

by:Vitor Montalvão
ID: 41859920
SQL Server topic removed.
0
 

Author Comment

by:bmanmike39
ID: 41860209
I'm trying to delete the row from my records, not check if it  exist.
0
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 
LVL 15

Expert Comment

by:Lokesh B R
ID: 41860223
Yes, debug the code see whether it works or not.
0
 

Author Comment

by:bmanmike39
ID: 41860302
I'm  a little confused. Doesn't this code do the same thing.

NOTE: It's very Important that It only deletes the sing record.

MYENTITIES oe = new MYENTITIES();
            var f = oe.fooItems.Where(s => s.fooItemId = Id).FirstOrDefault();
            oe.fooItems.Remove(f);
            oe.SaveChanges();

Open in new window

0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 250 total points
ID: 41860306
Hi bmanmike39;

This should work. If that does not work for you please post the exception you are getting and inner exception.
MYENTITIES oe = new MYENTITIES();
fooItem j = oe.fooItems.ToList().Where(d => d.fooItemId == Id).FirstOrDefault();

if( j != null ) { 
    oe.fooItems.Remove(j);
    oe.SaveChanges();
} else {
    // Record was not found
}

Open in new window

0
 

Author Closing Comment

by:bmanmike39
ID: 41860348
Thank you!  This was just what I was looking for.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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.…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

636 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