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
69 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 48

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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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…

821 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