/// <summary> /// Edit Pet and close form /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSaveEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { if (te_PName.Text != "" && te_POriginPrice.Text != "" && te_PSalePrice.Text != "" && Status != "") { PetModel pm = new PetModel(); TypeModel tm = new TypeModel(); //get pet status int pStatus = tm.getIDByTName(Status); String image = ""; //get name of image file already in database String oldImageName = pm.getPet(te_PID.Text).p_image; //Check Image is empty or not if (te_PImage.Text != "") { //set name image file is Pet's ID appends .jpg or .png if (openDialog.FileName.EndsWith(".jpg")) { image = te_PID.Text + ".jpg"; } else { image = te_PID.Text + ".png"; } //get solution Apps path String projectPath = Path.GetFullPath(Path.Combine(Application.StartupPath, "..\\..")); String oldAppFilePath = projectPath + "\\img\\" + oldImageName; //delete old image file if exist FileInfo fiApps = new FileInfo(oldAppFilePath); if (fiApps.Exists) { File.Delete(oldAppFilePath); } //get solution path String solutionPath = Directory.GetParent(projectPath).FullName;; String oldWebFilePath = solutionPath + "\\PetStoreWebClient\\Assets\\images\\" + oldImageName; //delete old image file if exist FileInfo fiWeb = new FileInfo(oldWebFilePath); if (fiWeb.Exists) { File.Delete(oldWebFilePath); } //get image folder on Apps and Web String newFileAppPath = Path.GetFullPath(projectPath + "\\img\\" + image); String newFileWebPath = Path.GetFullPath(solutionPath + "\\PetStoreWebClient\\Assets\\images\\" + image); //Copy image to 2 image folder File.Copy(te_PImage.Text, newFileAppPath); File.Copy(te_PImage.Text, newFileWebPath); } else { image = oldImageName; //if no new image is seleted then don't change image file } //Update Pet on database pm.UpdatePet(te_PID.Text, te_PName.Text, Convert.ToInt32(te_POriginPrice.Text), Convert.ToInt32(te_PSalePrice.Text), image, te_PDescription.Text, te_PStatus.Text, pStatus); //Notify to user XtraMessageBox.Show("Edit successful !!!", "Congratulation", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Close(); } else //send error message { XtraMessageBox.Show("Please fill in full information !!!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }