private void btnSaveEdit_ItemClick(object sender, ItemClickEventArgs e) { if (te_FoodName.Text != "" && te_FoodAmount.Text != "" && te_FoodPrice.Text != "" && te_FoodSalePrice.Text != "") { PetFoodModel pfm = new PetFoodModel(); String image = ""; String oldImageName = pfm.getPetFood(te_FoodID.Text).pf_image; if (te_FoodImage.Text != "") { if (openDialog.FileName.EndsWith(".jpg")) { image = te_FoodID.Text + ".jpg"; } else { image = te_FoodID.Text + ".png"; } String projectPath = Path.GetFullPath(Path.Combine(Application.StartupPath, "..\\..")); String oldFilePath = projectPath + "\\img\\" + oldImageName; FileInfo f = new FileInfo(oldFilePath); if (f.Exists) { File.Delete(oldFilePath); } String newFilepath = Path.GetFullPath(projectPath + "\\img\\" + image); File.Copy(te_FoodImage.Text, newFilepath); } else { image = oldImageName; } pfm.UpdateFood(te_FoodID.Text, te_FoodName.Text, Convert.ToInt32(te_FoodPrice.Text), Convert.ToInt32(te_FoodSalePrice.Text), Convert.ToInt32(te_FoodAmount.Text), 2, te_FoodStatus.Text, image); XtraMessageBox.Show("Edit successful !!!", "Congratulation", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Close(); } else { XtraMessageBox.Show("Please fill in full information !!!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
private void btnSaveEdit_ItemClick(object sender, ItemClickEventArgs e) { if (te_FoodName.Text != "" && te_FoodAmount.Text != "" && te_FoodPrice.Text != "" && te_FoodSalePrice.Text != "") { PetFoodModel pfm = new PetFoodModel(); String image = ""; String oldImageName = pfm.getPetFood(te_FoodID.Text).pf_image; if (te_FoodImage.Text != "") { //set text box image = ID + ".jpg" or ".png" if (openDialog.FileName.EndsWith(".jpg")) { image = te_FoodID.Text + ".jpg"; } else { image = te_FoodID.Text + ".png"; } //get path app project String projectPath = Path.GetFullPath(Path.Combine(Application.StartupPath, "..\\..")); //old image file path String oldFilePath = projectPath + "\\img\\" + oldImageName; //delete old image file if exist FileInfo f = new FileInfo(oldFilePath); if (f.Exists) { File.Delete(oldFilePath); } //get solution path String solutionPath = Directory.GetParent(projectPath).FullName;; String oldWebFilePath = solutionPath + "\\PetStoreWebClient\\Assets\\images\\" + oldImageName; //delete old image file if exist FileInfo f2 = new FileInfo(oldWebFilePath); if (f2.Exists) { File.Delete(oldWebFilePath); } //Get new image file path and copy it to image folder String newFilepath = Path.GetFullPath(projectPath + "\\img\\" + image); String newFileWebpath = Path.GetFullPath(solutionPath + "\\PetStoreWebClient\\Assets\\images\\" + image); File.Copy(te_FoodImage.Text, newFileWebpath); File.Copy(te_FoodImage.Text, newFilepath); } else { //get old image if dont change image image = oldImageName; } //update pet food on database var db = new PetStoreEntities(); var typeID = (int)db.Types.FirstOrDefault(x => x.t_name == selectedType).t_id; pfm.UpdateFood(te_FoodID.Text, te_FoodName.Text, Convert.ToInt32(te_FoodPrice.Text), Convert.ToInt32(te_FoodSalePrice.Text), Convert.ToInt32(te_FoodAmount.Text), typeID, te_FoodStatus.Text, image); //message success XtraMessageBox.Show("Edit successful !!!", "Congratulation", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Close(); } else { XtraMessageBox.Show("Please fill in full information !!!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }