protected void vehicleServicesGridView_RowDeleting(object sender, GridViewDeleteEventArgs e) { // e.RowIndex -> index of a row to delete int vServiceID = Convert.ToInt32(vehicleServicesGridView.DataKeys[e.RowIndex].Value); VehicleRepairBL contextBL = new VehicleRepairBL(); try { contextBL.DeleteService(vServiceID); } catch (OptimisticConcurrencyException ocex) { lblMessage.Text = ocex.Message.ToString() + " An error occured while deleting vehicle service. Make sure that vehicle service exists."; } catch (ArgumentNullException) { lblMessage.Text = "An error occured while deleting vehicle service. Make sure that vehicle service exists."; } catch (Exception ex) { lblMessage.Text = ex.Message.ToString() + " An error occured while deleting vehicle service. Please try again."; } string vreg = ViewState["vRegistration"].ToString(); var queryBL = contextBL.GetServicesByReg(vreg); // Rebind the vehicleServicesGridView after deletion of service vehicleServicesGridView.DataSource = queryBL.ToList(); vehicleServicesGridView.DataBind(); }
protected void Page_Load(object sender, EventArgs e) { // check QueryString not null OR empty - if null OR empty redirect to Vehicles.aspx if (Request.QueryString["id"] == null || Request.QueryString["id"] == "") { Response.Redirect("Vehicles.aspx"); } int id = Convert.ToInt16(Request.QueryString["id"]); if (!IsPostBack) { try { VehicleRepairBL contextBL = new VehicleRepairBL(); var queryBL = contextBL.GetServiceByID(id); vehicleServiceDetailsView.DataSource = queryBL.ToList(); vehicleServiceDetailsView.DataBind(); } catch (Exception) { lblMessage.Text = "An error occurred while retrieving vehicle service. Please try again."; } } }
// Select New Owner - button click protected void btnNewOwner_Click(object sender, EventArgs e) { int owner = (int)ViewState["owner"]; string reg = Convert.ToString(Request.QueryString["registration"]).Trim(); try { VehicleRepairBL contextBL = new VehicleRepairBL(); contextBL.UpdateVehicleOwner(reg, owner); // Refresh Page to see New Owner in vehicleDetailsView Page.Response.Redirect(Page.Request.Url.ToString(), true); } catch (OptimisticConcurrencyException) { lblMessage.Text = "Error while changing owner of a vehicle."; } catch (UpdateException) { lblMessage.Text = "Error while changing owner of a vehicle."; } catch (Exception ex) { if (ex.InnerException is UpdateException) { lblMessage.Text = "Error while changing owner of a vehicle. Please try again."; } } }
protected void VehiclesGridView_SelectedIndexChanging(object sender, GridViewSelectEventArgs e) { //int index = VehiclesGridView.SelectedIndex; int index = e.NewSelectedIndex; GridViewRow selectedRow = VehiclesGridView.Rows[index]; // gets the value of datakey of selected GridViewRow -> depends of which row was clicked string vreg = (VehiclesGridView.DataKeys[selectedRow.RowIndex].Value).ToString(); // save registration of clicked vehicle to the Viewstate - for rebinding vehicleServicesGridView ViewState["vRegistration"] = vreg; // gets the name of datakey -> 'registration' string reg = VehiclesGridView.DataKeyNames[0].ToString(); lblRegistration.Text = ": " + vreg.ToUpper(); try { VehicleRepairBL contextBL = new VehicleRepairBL(); var queryBL = contextBL.GetServicesByReg(vreg); vehicleServicesGridView.DataSource = queryBL.ToList(); vehicleServicesGridView.DataBind(); // vehicleServicesGridView.HeaderRow.Cells[0].Text = "Services"; } catch (Exception) { lblMessage.Text = "An error occurred while retrieving vehicle services. Please try again."; } }
protected void VehiclesGridView_RowDeleting(object sender, GridViewDeleteEventArgs e) { int index = e.RowIndex; // index of the row to delete -> where Delete button clicked string vRegistration = VehiclesGridView.DataKeys[e.RowIndex].Value.ToString(); VehicleRepairBL contextBL = new VehicleRepairBL(); try { contextBL.DeleteVehicle2(vRegistration); // to refresh the GridView - in case of deletion after filtering vehicles GridView - without next line of code it doesnt remove deleted item (vehicle) from GridView VehiclesGridView.DataSourceID = "VehiclesObjectDataSource"; } catch (OptimisticConcurrencyException ocex) { lblError.Text = ocex.ToString() + " An error occured while deleting vehicle. Make sure that vehicle exists."; } catch (ArgumentNullException) { lblError.Text = "An error occured while deleting vehicle. Make sure that vehicle exists."; } catch (Exception) { lblError.Text = "An error occured while deleting vehicle. Please try again."; } }
protected void vehicleServiceDetailsView_ItemInserting(object sender, DetailsViewInsertEventArgs e) { TextBox textBoxDesc = (TextBox)(vehicleServiceDetailsView.FindControl("descriptionTextBox")); string desc = textBoxDesc.Text; string registration = ViewState["reg"].ToString(); // DateTime serviceDate1 = Convert.ToDateTime(e.Values["seviceDate"]); // DateTime serviceDate2 = Convert.ToDateTime(e.Values[0]); TextBox serviceDateTextBox = (TextBox)vehicleServiceDetailsView.FindControl("serviceDateTextBox"); DateTime serviceDate3 = Convert.ToDateTime(serviceDateTextBox.Text); int odometer = Convert.ToInt32(e.Values["odometer"]); string subject = e.Values["subject"].ToString(); try { VehicleRepairBL contextBL = new VehicleRepairBL(); contextBL.AddServiceByReg(registration, serviceDate3, odometer, subject, desc); lblMessage.Text = "Service added!"; } catch (Exception) { lblMessage.Text = "An error occured while inserting new service. Please try again. "; } }
protected void Page_Load(object sender, EventArgs e) { // check QueryString not null OR empty - if null OR empty redirect to Vehicles.aspx if (Request.QueryString["registration"] == null || Request.QueryString["registration"] == "") { Response.Redirect("Vehicles.aspx"); } vehRegistration = Convert.ToString(Request.QueryString["registration"]).Trim(); if (!IsPostBack) { VehicleRepairBL contextBL = new VehicleRepairBL(); try { IQueryable <Vehicle> queryBL = contextBL.GetVehicleByRegistration(vehRegistration); vehicleDetailsView.DataSource = queryBL; vehicleDetailsView.DataBind(); } catch (Exception) { lblMessage.Text = "An error occurred. Please try again."; } try { VehicleRepairBL context = new VehicleRepairBL(); var queryBL = context.GetVehicleByRegistration(vehRegistration); // get the ID of Customer // if there's no vehicle owner -> customerID field is empty/NULL then Exception will be thrown int cID = (int)queryBL.FirstOrDefault().customerID; // get Customer by ID var cust = context.GetCustomerByID(cID).FirstOrDefault(); string fName; string Lname; fName = cust.firstName; Lname = cust.lastName; vehicleDetailsView.Rows[1].Cells[1].Text = fName + " " + Lname; } catch (Exception) { lblMessage.Text = "Vehicle has no owner"; } } }
// Change vehicle owner - button clcik protected void btnOwner_Click(object sender, EventArgs e) { VehicleRepairBL contextBL = new VehicleRepairBL(); try { var queryCustomers = contextBL.GetCustomers(); CustomersGridView.DataSource = queryCustomers; CustomersGridView.DataBind(); } catch (Exception ex) { throw ex; } }
protected void vehicleServiceDetailsView_ItemUpdating(object sender, DetailsViewUpdateEventArgs e) { int id = Convert.ToInt32(e.Keys["id"]); try { VehicleRepairBL contextBL = new VehicleRepairBL(); var serviceBL = contextBL.GetServiceByID(id).FirstOrDefault(); TextBox serviceDateTextBox = (TextBox)vehicleServiceDetailsView.FindControl("serviceDateTextBox"); DateTime serviceDate2 = Convert.ToDateTime(serviceDateTextBox.Text); //DateTime serviceDate = Convert.ToDateTime(e.NewValues["serviceDate"]); // e.NewValues - doesn't get KeyField values -> in this case can't retrieve 'id' and 'registration' int odometer = Convert.ToInt32(e.NewValues["odometer"]); //int odometer = Convert.ToInt32(e.NewValues[0]); string subject = e.NewValues["subject"].ToString(); string description = null; if (e.NewValues["description"] == null) { description = ""; } else { description = e.NewValues["description"].ToString(); } contextBL.UpdateServiceByID(id, serviceDate2, odometer, subject, description); lblMessage.Text = "Service updated!"; } catch (OptimisticConcurrencyException) { lblMessage.Text = "Vehicle service information changed since you retrieved it. Please try again."; } catch (UpdateException) { lblMessage.Text = "An error occurred while updating vehicle service. Please try again."; } catch (ArgumentNullException) { lblMessage.Text = "An error occurred while updating vehicle service. Make sure that vehicle service exists."; } catch (Exception) { lblMessage.Text = "An error occurred while updating vehicle service. Please try again."; } }
protected void SearchButton_Click(object sender, EventArgs e) { if (SearchTextBox.Text.Trim() != null && SearchTextBox.Text.Trim() != "") { try { VehicleRepairBL context = new VehicleRepairBL(); var query = context.FilterVehicles(SearchTextBox.Text.Trim()); VehiclesGridView.DataSourceID = null; VehiclesGridView.DataSource = query; VehiclesGridView.DataBind(); } catch (Exception) { lblError.Text = "An error occurred while retrieving vehicles. Please try again."; } } }
protected void vehicleDetailsView_ItemUpdating(object sender, DetailsViewUpdateEventArgs e) { string vRega = e.Keys[0].ToString(); string vrega = e.Keys["registration"].ToString(); string vMake = e.NewValues["make"].ToString(); string vModel = e.NewValues["model"].ToString(); string vType = e.NewValues["type"].ToString(); string vYear1 = e.NewValues["year"].ToString(); Int16 vYear2 = Convert.ToInt16(vYear1); string vColor = null; if (e.NewValues["color"] == null || e.NewValues["color"].ToString() == "") { vColor = string.Empty; } else { vColor = e.NewValues["color"].ToString().Trim(); } string vVin = null; if (e.NewValues["vin"] == null || e.NewValues["vin"].ToString() == "") { vVin = string.Empty; } else { vVin = e.NewValues["vin"].ToString().Trim(); } string vCapacity = null; if (e.NewValues["capacity"] == null || e.NewValues["capacity"].ToString() == "") { vCapacity = string.Empty; } else { vCapacity = e.NewValues["capacity"].ToString().Trim(); } string vFuelType = null; if (e.NewValues["fuelType"] == null || e.NewValues["fuelType"].ToString() == "") { vFuelType = string.Empty; } else { vFuelType = e.NewValues["fuelType"].ToString().Trim(); } VehicleRepairBL contextBL = new VehicleRepairBL(); try { contextBL.UpdateVehicleByRegistration(vrega, vMake, vModel, vType, vYear2, vColor, vVin, vCapacity, vFuelType); lblUpdateVehicle.Text = "Vehicle updated."; } catch (OptimisticConcurrencyException) { lblMessage.Text = "Error while updating vehicle. Original data was changed since you retrieved it"; } catch (UpdateException) { lblMessage.Text = "Error while updating vehicle."; } catch (Exception ex) { if (ex.InnerException is UpdateException) { lblMessage.Text = "Error while updating vehicle. Please try again."; } } }
protected void AddVehicleDetailsView_ItemInserting(object sender, DetailsViewInsertEventArgs e) { string vRegistration = e.Values["registration"].ToString(); string vMake = e.Values["make"].ToString(); string vModel = e.Values["model"].ToString(); string vType = e.Values["type"].ToString(); short vYear = Convert.ToInt16(e.Values["year"]); string vColor = null; if (e.Values["color"] == null || e.Values["color"].ToString() == "") { vColor = string.Empty; } else { vColor = e.Values["color"].ToString().Trim(); } string vVin = null; if (e.Values["vin"] == null || e.Values["vin"].ToString() == "") { vVin = string.Empty; } else { vVin = e.Values["vin"].ToString().Trim(); } string vCapacity = null; if (e.Values["capacity"] == null || e.Values["capacity"].ToString() == "") { vCapacity = string.Empty; } else { vCapacity = e.Values["capacity"].ToString().Trim(); } string vFuelType = null; if (e.Values["fuelType"] == null || e.Values["fuelType"].ToString() == "") { vFuelType = string.Empty; } else { vFuelType = e.Values["fuelType"].ToString().Trim(); } ViewState["vRegistration"] = vRegistration; try { VehicleRepairBL contextBL = new VehicleRepairBL(); contextBL.InsertVehicle(vRegistration, vMake, vModel, vType, vYear, vColor, vVin, vCapacity, vFuelType); lblErrorMessage.Text = "Vehicle Added: " + e.Values[0]; } catch (OptimisticConcurrencyException ocex) { lblErrorMessage.Text = ocex.ToString(); } catch (UpdateException ex) { lblErrorMessage.Text = "An error occured on inserting new vehicle. There is already vehicle " + "with the Registration: " + ViewState["vRegistration"] + "<br/>" + ex.Message.ToString(); // "<br/>Data count:" + e.Exception.InnerException.InnerException.Data.Count.ToString(); // e.Exception.InnerException.Data.Keys.ToString() + "<br/>" + //e.Exception.InnerException.Message + //e.Exception.InnerException.InnerException.Message; //e.Exception.InnerException.Message; // msdn.microsoft.com/en-us/library/system.exception.data%28v=vs.110%29.aspx //ex.ExceptionHandled = true; } catch (SqlException exs) { //lblErrorMessage.Text = e.Exception.InnerException.Message; lblErrorMessage.Text = "Greška - duplicate registration" + "<br/>" + exs.Message.ToString();; // .ExceptionHandled = true; } catch (Exception) { //lblErrorMessage.Text = "Greška!<br/>" + ex.Message.ToString() + "<br/>" + ex.InnerException.Message.ToString(); lblErrorMessage.Text = "An error occured on inserting new vehicle. There is already vehicle " + "with the Registration: " + ViewState["vRegistration"]; } }