Reputation: 784
i am creating a database application in mvc .net with sql server database.i have a table name seatplans with fields id,layout_id,seat_no,seat_id,booked i want to update the database with the following quesy inside the controller action
update seatplanes set booked=1 where seatid=seat_id and layoutid=layout_id
my controller action is as follows but i am unable to update the database
[HttpPost]
public ActionResult Seatbook(String seat_id, String seat_no, int layout_id)
{
// int id = 10;
SeatPlans S = new SeatPlans();
S.seat_id = seat_id;
S.seat_no = seat_no;
S.layout_id = layout_id;
S.booked = 1;
if (ModelState.IsValid)
{
var OldInsObj = db.SEATPLAN.Find(seat_id,layout_id);
S.Id = OldInsObj.Id;
//var OldInsObj = db.SEATPLAN.Find(d=>d.seat_id==seat_id , d=>d.layout_id==layout_id).ToList();
db.Entry(S).State = EntityState.Modified;
db.SaveChanges();
// return RedirectToAction("Index");
}
return View("Index");
}
can i update the database without using the stored procedure.
Upvotes: 1
Views: 516
Reputation: 174
Firstly I think you want to have a read of the documentation for Entity Framework, it'll get you going with all you need.
However, to answer your question in order to update a database entry you'd want to first get the entry:
var OldInsObj = db.SEATPLAN.Where(s => s.seat_id == seat_id
&& s.layout_id == layout_id).FirstOrDefault();
Using find will only work when searching on the primary key, when you are wanting to search on other fields you will need to use Linq as shown above.
Then update it:
OldInsObj.booked = 1;
Finally, you can save your work to the database:
db.SaveChanges();
Upvotes: 1