public int InsertFood() { Food food = new Food(); food.ID = 0; if (txtName.Text == "" || txtUnit.Text == "" || txtPrice.Text == "") { MessageBox.Show("Chưa cập nhật 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.Insert(food)); } return(-1); }
/// <summary> /// Phương thức thêm dữ liệu cho bảng Food /// </summary> /// <returns>Trả về số dương nếu thành công, ngược lại trả về số âm</returns> public int InsertFood() { //Khai báo đối tượng Food từ tầng DataAccess Food food = new Food(); food.ID = 0; // 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 từ người dùng nhập vào 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 { // Cố gắng lấy giá trị 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(); // Chèn dữ liệu vào bảng return(foodBL.Insert(food)); } return(-1); }