protected void SaveNewCar_Click(object sender, EventArgs e) { string type = VehicleType.Text; string strYear = Convert.ToString(InjectYear.Text); string make = InjectMake.Text; string model = InjectModel.Text; string desc = VehicleDesc.Text; string valueCat = ValueRange.SelectedValue; string virtualPath = "~/Images/"; string physicalFolder = Server.MapPath(virtualPath); string fileName = Guid.NewGuid().ToString(); string extension = System.IO.Path.GetExtension(FileUpload1.FileName); string imgUrl = System.IO.Path.Combine(virtualPath, fileName + extension); int year = Convert.ToInt32(strYear); int miles = Convert.ToInt32(VehicleMiles.Text); UserClass uc = new UserClass(Profile.UserName); if (extension == ".jpg" || extension == ".jpeg") { FileUpload1.SaveAs(System.IO.Path.Combine(physicalFolder, fileName + extension)); CarClass cc = new CarClass(Profile.UserName); UserControl ucx = (UserControl)LoadControl("~/Controls/UserNoticeModal.ascx"); Label txtLabel = (Label)ucx.FindControl("TextLabel"); if (cc.AddCarToProfile(type, make, model, year, miles, desc, imgUrl, valueCat)) { txtLabel.Text = "Vehicle successfully added to your profile!"; Form.Controls.Add(ucx); } else { txtLabel.Text = "Sorry, an error occurred. Only 2 vehicles are allowed."; Form.Controls.Add(ucx); } } else { CustomValidator imgValidate = (CustomValidator)ImgValid; imgValidate.IsValid = false; } if (uc.UserIsValid()) { if (Profile.IsValidated != true) { Profile.IsValidated = true; Response.Redirect("~/MyProfile/MyProfile.aspx?isvalid=true"); } } else { if (Profile.IsValidated == true) { Profile.IsValidated = false; } } CarsGridView.DataSourceID = "CarsDataSource"; CarsGridView.DataBind(); }
protected void VeEditListView_ItemCommand(object sender, ListViewCommandEventArgs e) { string[] commandArgs = e.CommandArgument.ToString().Split(new char[] { ',' }); string veId = commandArgs[0]; string veImgId = commandArgs[1]; Guid veGuid = Guid.Parse(veId); Guid imgGuid = Guid.Parse(veImgId); if (e.CommandName == "SetAsMain") { CarClass cc = new CarClass(Profile.UserName); cc.SetMainImage(imgGuid, veGuid); UserControl ucx = (UserControl)LoadControl("~/Controls/UserNoticeModal.ascx"); Label txtLabel = (Label)ucx.FindControl("TextLabel"); txtLabel.Text = "Success!"; Form.Controls.Add(ucx); } if (e.CommandName == "DeleteImg") { using (SwapEntities ent = new SwapEntities()) { var delImg = (from tbl in ent.VeImages where tbl.Id == imgGuid select tbl).SingleOrDefault(); if (delImg != null) { string imgPath = delImg.ImageUrl; string serverPath = Server.MapPath(imgPath); try { System.IO.File.Delete(serverPath); } catch { } ent.DeleteObject(delImg); ent.SaveChanges(); UserControl ucx = (UserControl)LoadControl("~/Controls/UserNoticeModal.ascx"); Label txtLabel = (Label)ucx.FindControl("TextLabel"); txtLabel.Text = "Success!"; Form.Controls.Add(ucx); } } } CarsGridView.DataSourceID = "CarsDataSource"; CarsGridView.DataBind(); }
protected void CarsGridView_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "EditCar") { int rowIndex = Convert.ToInt32(e.CommandArgument); CarClass cc = new CarClass(Profile.UserName); int miles = 0; string desc = ""; string imgUrl = ""; GridViewRow myRow = CarsGridView.Rows[rowIndex]; TextBox boxMiles = (TextBox)myRow.FindControl("EditMiles"); TextBox boxDesc = (TextBox)myRow.FindControl("EditDesc"); FileUpload newImg = (FileUpload)myRow.FindControl("FileUpload2"); Label myId = (Label)myRow.FindControl("CarId"); Guid id = Guid.Parse(myId.Text); if (boxMiles.Text != "") { miles = Convert.ToInt32(boxMiles.Text); } if (boxDesc.Text != "") { desc = boxDesc.Text; } cc.EditCarInfo(id, miles, desc); UserControl ucx = (UserControl)LoadControl("~/Controls/UserNoticeModal.ascx"); Label txtLabel = (Label)ucx.FindControl("TextLabel"); if (newImg.FileName != "") { string ext = System.IO.Path.GetExtension(newImg.FileName); if (ext == ".jpg" || ext == ".jpeg") { string virtualPath = "~/Images/"; string physicalPath = Server.MapPath(virtualPath); Guid fileName = Guid.NewGuid(); newImg.SaveAs(System.IO.Path.Combine(physicalPath, fileName + ext)); imgUrl = System.IO.Path.Combine(virtualPath, fileName + ext); if (cc.AddVehicleImage(imgUrl, id, false)) { txtLabel.Text = "Success!"; Form.Controls.Add(ucx); } else { txtLabel.Text = "3 Images max."; Form.Controls.Add(ucx); } } else { txtLabel.Text = "Image must be .jpg or .jpeg"; Form.Controls.Add(ucx); } } else { txtLabel.Text = "Success!"; Form.Controls.Add(ucx); } CarsGridView.DataSourceID = "CarsDataSource"; CarsGridView.DataBind(); } if (e.CommandName == "DeleteVehicle") { using (SwapEntities myEnt = new SwapEntities()) { string userName = Profile.UserName; string strId = Convert.ToString(e.CommandArgument); Guid veGuid = Guid.Parse(strId); var checkSwaps = from tbl in myEnt.ScheduledSwaps where tbl.UserMain == userName & tbl.MainRated == false & tbl.VeMain == veGuid || tbl.UserMain == userName & tbl.MainRated == false & tbl.VeOther == veGuid || tbl.UserOther == userName & tbl.OtherRated == false & tbl.VeMain == veGuid || tbl.UserOther == userName & tbl.OtherRated == false & tbl.VeOther == veGuid select tbl; if (checkSwaps.Count() >= 1) { UserControl ucx = (UserControl)LoadControl("~/Controls/UserNoticeModal.ascx"); Label txtLabel = (Label)ucx.FindControl("TextLabel"); txtLabel.Text = "You cannot delete a vehicle with a scheduled swap pending."; Form.Controls.Add(ucx); } else { var getVe = (from vetbl in myEnt.UserVehicles where vetbl.Id == veGuid select vetbl).SingleOrDefault(); if (getVe != null) { myEnt.DeleteObject(getVe); myEnt.SaveChanges(); } var getImgs = from tbl in myEnt.VeImages where tbl.VehicleId == veGuid select tbl; foreach (var item in getImgs) { string imgPath = item.ImageUrl; string serverPath = Server.MapPath(imgPath); try { System.IO.File.Delete(serverPath); } catch { } } var validCheck = (from vetbl in myEnt.UserVehicles where vetbl.UserName == userName select vetbl).SingleOrDefault(); if (validCheck == null) { Profile.IsValidated = false; } Response.Redirect("~/MyProfile/EditProfile.aspx"); } } } }