public string Create(Customer customerEntity) { try { using(System.Data.SqlClient.SqlConnection sqlConnection = new System.Data.SqlClient.SqlConnection(Connectionstring)) { sqlConnection.Open(); string sqlQuery = "INSERT INTO [dbo].[Customer]([FirstName],[LastName],[Address],[City]) VALUES (@FirstName, @LastName, @Address, @City)"; sqlConnection.Execute(sqlQuery, new { customerEntity.FirstName, customerEntity.LastName, customerEntity.Address, customerEntity.City, }); sqlConnection.Close(); } return "Created"; } catch (Exception ex) { return ex.Message; } }
public bool Update(Customer customerEntity) { try { using (System.Data.SqlClient.SqlConnection sqlConnection = new System.Data.SqlClient.SqlConnection(Connectionstring)) { sqlConnection.Open(); string sqlQuery = "UPDATE [dbo].[Customer] SET [FirstName] = @FirstName, [LastName] =@LastName,[Address] =@Address,[City] = @City WHERE CustomerId=@Id"; sqlConnection.Execute(sqlQuery, new { customerEntity.FirstName, customerEntity.LastName, customerEntity.Address, customerEntity.City, Id = customerEntity.CustomerId }); sqlConnection.Close(); } return true; } catch (Exception) { return false; } }
public ActionResult Edit(Customer customer) { try { // TODO: Add update logic here var customerEntities = new CustomerDB(); customerEntities.Update(customer); return RedirectToAction("Index"); } catch { return View(); } }
public ActionResult Create(Customer customer) { try { //TODO: add insert logic here var customEntities = new CustomerDB(); customEntities.Create(customer); return RedirectToAction("Index"); } catch (Exception) { return View(); } }