public int UpdateFood() { Food food = foodCurrent; if (txtName.Text == "" || txtUnit.Text == "" || txtPrice.Text == "") { MessageBox.Show("Chưa nhập dữ liệu cho các ô, vui lòng nhập lại"); } else { food.Name = txtName.Text; food.Unit = txtUnit.Text; food.Notes = txtNotes.Text; int price = 0; try { price = int.Parse(txtPrice.Text); } catch { price = 0; } food.Price = price; food.FoodCategoryID = int.Parse(cbCategory.SelectedValue.ToString()); FoodBL foodBL = new FoodBL(); return(foodBL.Update(food)); } return(-1); }
/// <summary> /// Phương thức cập nhật dữ liệu cho bảng Food /// </summary> /// <returns>Trả về dương nếu cập nhật thành công, ngược lại là số âm</returns> public int UpdateFood() { //Khai báo đối tượng Food và lấy đối tượng hiện hành Food food = foodCurrent; // Kiểm tra nếu các ô nhập khác rỗng if (txtName.Text == "" || txtUnit.Text == "" || txtPrice.Text == "") { MessageBox.Show("Chưa nhập dữ liệu cho các ô, vui lòng nhập lại"); } else { //Nhận giá trị Name, Unit, và Notes khi người dùng sửa food.Name = txtName.Text; food.Unit = txtUnit.Text; food.Notes = txtNotes.Text; //Giá trị price là giá trị số nên cần bắt lỗi khi người dùng nhập sai int price = 0; try { // Chuyển giá trị từ kiểu văn bản qua kiểu int price = int.Parse(txtPrice.Text); } catch { // Nếu sai, gán giá = 0 price = 0; } food.Price = price; // Giá trị FoodCategoryID được lấy từ ComboBox food.FoodCategoryID = int.Parse(cbbCategory.SelectedValue.ToString()); // Khao báo đối tượng FoodBL từ tầng Business FoodBL foodBL = new FoodBL(); // Cập nhật dữ liệu trong bảng return(foodBL.Update(food)); } return(-1); }