Sakir Khan
Sakir Khan

Reputation: 15

asp.net MVC4 - update database where id=given

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

Answers (2)

sanket parikh
sanket parikh

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

Sarbanjeet
Sarbanjeet

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();
    }
  1. Get data row from table.
  2. Update data as per need
  3. Save changes are required to save the data in table.

Upvotes: 2

Related Questions