Reputation: 15
How can I update a row in my database including where clause?
my c# codes-
[HttpPost]
public ActionResult Index(string ID, string notes)
{
SampleDBContext db = new SampleDBContext();
// update table name Customers, set row `customerNote` with passed string notes, where `id` == given string ID
return View();
}
Can anyone please help me
Upvotes: 0
Views: 4433
Reputation: 281
your data will update like
public ActionResult Demoupdate(int id,EmployeeModel emp)
Employee emptbl = new Employee();
var data = (from t in dbc.Employees where t.EmpId == id select t).FirstOrDefault();
if (data != null)
{
emp.EmpName = data.EmpName;
emp.EmpAddress = data.EmpAddress;
}
return View(emp);
}
[HttpPost]
public ActionResult Demoupdate(EmployeeModel emp, int id)
{
Employee emptbl = new Employee();
emptbl.EmpId = id;
emptbl.EmpName = emp.EmpName;
emptbl.EmpAddress = emp.EmpAddress;
dbc.Entry(emptbl).State = EntityState.Modified;
dbc.SaveChanges();
return View();
}
Upvotes: 0
Reputation: 253
public void InsertOrUpdate(string ID, string notes)
{
SampleDBContext dbContext = new SampleDBContext();
var customer= dbContext .Customers.Where(e=>e.ID =ID )
if (customer.ID == default(System.Guid)) {
// New entity
customer.UID = Guid.NewGuid();
dbContext .customers.Add(customer);
} else {
// Existing entity
dbContext .Entry(customer).State = System.Data.Entity.EntityState.Modified;
}
dbContext .SaveChanges();
}
Upvotes: 2