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
61 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
7 Comments
 
LVL 14

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 45

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 14

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 62

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

12 Experts available now in Live!

Get 1:1 Help Now