Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
73 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 1000 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 52

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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
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 64

Accepted Solution

by:
Fernando Soto earned 1000 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

704 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