} //End Page_Load protected void GetUser() { //Populate the form with the user info Int32 UserID = Convert.ToInt32(Request.QueryString["UserID"]); try { //Connect to db using (COMP2007Entities db = new COMP2007Entities()) { //Populate the user from the database CarUser u = (from objs in db.CarUsers where objs.userID == UserID select objs).FirstOrDefault(); //Map the user info into the form if (u != null) { txtFirstName.Text = u.firstName; txtLastName.Text = u.lastName; txtPhone.Text = u.phoneNum; txtEmail.Text = u.email; txtUsername.Text = u.userName; txtOldPassHidden.Text = u.userPassword; } //End IF } //End using } //End TRY //Catch any error and redirect to the error page catch (SystemException ex) { Response.Redirect("/error.aspx"); } //End CATCH } //End GetUser
} //End grdUsers_RowDeleting protected void getAccount() { try { //Connect to Entity Framework using (COMP2007Entities db = new COMP2007Entities()) { //get the current user name string name = HttpContext.Current.User.Identity.Name; //Find the user with that user name var User = from u in db.CarUsers where u.userName == name select u; //Show the resualts in the grid grdUsers.DataSource = User.ToList(); grdUsers.DataBind(); } //End using } //End TRY //Catch any error and redirect to the error page catch (SystemException ex) { Response.Redirect("/error.aspx"); } //End CATCH } //End getAccount
protected void btnAddMake_Click(object sender, EventArgs e) { try { //Connect to EF using (COMP2007Entities db = new COMP2007Entities()) { //Check if the make exists int count = (from m in db.CarModels where m.model == txtModel.Text select m).Count(); if (count != 0) { //Reset the fields lblStatus.Visible = true; txtModel.Text = ""; //Show message lblStatus.Text = "Model already exists"; } else { CarModel model = new CarModel(); //Get the user input model.model = txtModel.Text; model.makeID = Convert.ToInt32(ddlMake.SelectedValue); //Add to th DB db.CarModels.Add(model); //Save the DB db.SaveChanges(); //Reset the fields lblStatus.Visible = true; ddlMake.SelectedIndex = 0; txtModel.Text = ""; //Show message lblStatus.Text = "Model successfully added"; } } } //End TRY //Catch any error and redirect to the error page catch (SystemException ex) { Response.Redirect("/error.aspx"); } //End CATCH }
} //end Page_Load protected void grdUsers_RowDeleting(object sender, GridViewDeleteEventArgs e) { //Store the selected row Int32 selectedRow = e.RowIndex; //Get the data key, in this case the UserID Int32 userID = Convert.ToInt32(grdUsers.DataKeys[selectedRow].Values["UserID"]); //Connect To Entity Framework using (COMP2007Entities db = new COMP2007Entities()) { //Count the number of postings int count = (from cars in db.Cars where cars.userID == userID select cars).Count(); if (count != 0) { Response.Write(@"<script language='javascript'>alert('Please delete your postings first!');</script>"); } else { //Select the user by using the UserID CarUser u = (from objs in db.CarUsers where objs.userID == userID select objs).FirstOrDefault(); //Delete it from our CarUsers table db.CarUsers.Remove(u); db.SaveChanges(); //Find our user in the ASP.Net Idenity user table var userStore = new UserStore<IdentityUser>(); var userManager = new UserManager<IdentityUser>(userStore); var user = userManager.Find(u.userName, u.userPassword); //Delete that user from Idenity user table userManager.Delete(user); //Sign the deleted user out var authenticationManager = HttpContext.Current.GetOwinContext().Authentication; authenticationManager.SignOut(); //Redirect Response.Redirect("/default.aspx"); } } //End using } //End grdUsers_RowDeleting
protected void ddlMake_SelectedIndexChanged(object sender, EventArgs e) { //If the selected index is anything but 0 if (ddlMake.SelectedItem != (new ListItem("Please select a make", "0"))) { ddlMake.Items.Remove(new ListItem("Please select a make", "0")); //Show the model dropdown lblModel.Visible = true; ddlModel.Visible = true; pnlSecondaryCarInput.Visible = false; pnlButton.Visible = false; DropDownList makeDropdown = (DropDownList)sender; //Populate car models based on selected car make Int32 makeID = Int32.Parse(makeDropdown.SelectedValue); try { //Connect to EF using (COMP2007Entities db = new COMP2007Entities()) { //Select the models from the db var models = (from d in db.CarModels where d.makeID == makeID select d); //Bind them to the grid ddlModel.DataSource = models.ToList(); ddlModel.DataBind(); //Add please select a model to the dropdown ddlModel.Items.Insert(0, new ListItem("Please select a model", "0")); } //End using } //End TRY //Catch any error and redirect to the error page catch (SystemException ex) { Response.Redirect("/error.aspx"); } //End CATCH } //End IF //If no car make is selected, disable the car model dropdown else { ddlModel.Visible = false; lblModel.Visible = false; ddlModel.SelectedIndex = 0; } //End ELSE } //End ddlMake_SelectedIndexChanged
protected void btnAddMake_Click(object sender, EventArgs e) { try { //Connect to EF using (COMP2007Entities db = new COMP2007Entities()) { //Check if the make exists int count = (from m in db.CarMakes where m.make == txtMake.Text select m).Count(); if (count != 0) { //Reset the fields lblStatus.Visible = true; txtMake.Text = ""; //Show message lblStatus.Text = "Make already exists"; } else { CarMake make = new CarMake(); //The the user input make.make = txtMake.Text; //Add to the database db.CarMakes.Add(make); //Save the database db.SaveChanges(); //Reset the fields lblStatus.Visible = true; txtMake.Text = ""; //Show message lblStatus.Text = "Make successfully added"; } } } //End TRY //Catch any error and redirect to the error page catch (SystemException ex) { Response.Redirect("/error.aspx"); } //End CATCH }
} //End btnSave_Click protected void saveEditUser() { try { //Connect to EF using (COMP2007Entities db = new COMP2007Entities()) { //Initialized a new CarUser CarUser u = new CarUser(); Int32 UserID = 0; //Check he query string for an ID so we know to add or edit if (Request.QueryString["UserID"] != null) { //Get the ID from url UserID = Convert.ToInt32(Request.QueryString["UserID"]); //Get the current user from EF u = (from objs in db.CarUsers where objs.userID == UserID select objs).FirstOrDefault(); } //End IF //Set the CarUser field to the form feild u.firstName = txtFirstName.Text; u.lastName = txtLastName.Text; u.email = txtEmail.Text; u.phoneNum = txtPhone.Text; u.userName = txtUsername.Text; u.userPassword = txtPassword.Text; //If the user ID is 0 - We are adding if (UserID == 0) { db.CarUsers.Add(u); } //End IF //Save the changes done to the database db.SaveChanges(); } //End using } //End TRY //Catch any error and redirect to the error page catch (SystemException ex) { Response.Redirect("/error.aspx"); } //End CATCH } //End saveEditUser
public static List<string> GetLocation(string pre) { List<string> allLocations = new List<string>(); try { using (COMP2007Entities dc = new COMP2007Entities()) { allLocations = (from a in dc.Cities where a.city1.StartsWith(pre) select a.city1).ToList(); } //End using } //End TRY //Catch any error and redirect to the error page catch (SystemException ex) { HttpContext.Current.Response.Redirect("/error.aspx"); } //End CATCH return allLocations; }
protected void grdPosts_RowDeleting(object sender, GridViewDeleteEventArgs e) { //store Theme row that was click Int32 selectedRow = e.RowIndex; //get the selected StudetnID using the grid's data key collection Int32 carID = Convert.ToInt32(grdPosts.DataKeys[selectedRow].Values["carID"]); //use EF to remove the seleted student using (COMP2007Entities db = new COMP2007Entities()) { Car cars = (from objs in db.Cars where objs.carID == carID select objs).FirstOrDefault(); CarClass carClass = (from objs in db.CarClasses where objs.carID == carID select objs).FirstOrDefault(); db.Cars.Remove(cars); db.CarClasses.Remove(carClass); db.SaveChanges(); } //refresh the grid getAccount(); getPosts(); }
//Populate the dropdown for the car makes protected void GetCarMakes() { try { //Connect to EF using (COMP2007Entities db = new COMP2007Entities()) { //Get the makes from the db var makes = (from d in db.CarMakes orderby d.make select d); //Bind them to the grid ddlMake.DataSource = makes.ToList(); ddlMake.DataBind(); } //End using } //End TRY //Catch any error and redirect to the error page catch (SystemException ex) { Response.Redirect("/error.aspx"); } //End CATCH } //End GetCarmakes
protected void getMakes() { try { //Connect to EF using (COMP2007Entities db = new COMP2007Entities()) { //Get the makes from the db var makes = (from d in db.CarMakes orderby d.makeID select d); //Bind them to the grid ddlMake.DataSource = makes.ToList(); ddlMake.DataBind(); ddlMake.Items.Insert(0, new ListItem("Please select a make", "0")); } //End using } //End TRY //Catch any error and redirect to the error page catch (SystemException ex) { } //End CATCH }
} //End getAccount protected void getPosts() { //Get the user name of the logged in user string userName = User.Identity.Name; //Create some objects Int32 userID = 0; CarUser carUser = new CarUser(); Car car = new Car(); CarClass cClass = new CarClass(); try { //Connect to EF using (COMP2007Entities db = new COMP2007Entities()) { //Cherck the URL for ID if (Request.QueryString["UserID"] != null) { //Get the id from url userID = Convert.ToInt32(Request.QueryString["userID"]); //Get that records from the database carUser = (from objs in db.CarUsers where objs.userID == userID select objs).FirstOrDefault(); } //The URL is empty, get the userID of the logged in user. else { carUser = (from objs in db.CarUsers where objs.userName == userName select objs).FirstOrDefault(); } userID = carUser.userID; //Count the number of postings int count = (from cars in db.Cars where cars.userID == userID select cars).Count(); //If its greater that 0 them them if (count > 0) { var objE = (from cars in db.Cars join cu in db.CarUsers on cars.userID equals cu.userID join carE in db.CarEngines on cars.engineID equals carE.engineID join carclass in db.CarClasses on cars.carID equals carclass.carID join model in db.CarModels on carclass.modelID equals model.modelID join make in db.CarMakes on model.makeID equals make.makeID where cars.userID == userID select new { make.make, model.model, carclass.@class, cars.modelYear, cars.transmission, carE.fuelType, cars.@new, cars.cost, cars.kilometer, cars.carID }); grdPosts.DataSource = objE.ToList(); grdPosts.DataBind(); } //If there is none ask if they want to make one else { grdPosts.Visible = false; pnlAdPost.Visible = true; } } } //End TRY //Catch any error and redirect to the error page catch (SystemException ex) { Response.Redirect("/error.aspx"); } //End CATCH }
} //End Page_Load protected void GetCar() { //Show controls ddlMake.Visible = true; ddlModel.Visible = true; lblMake.Visible = true; lblModel.Visible = true; //populate the form with existing car records Int32 carID = Convert.ToInt32(Request.QueryString["carID"]); try { //connect to the db via EF using (COMP2007Entities db = new COMP2007Entities()) { //Populate the car instances Car cars = (from objs in db.Cars where objs.carID == carID select objs).FirstOrDefault(); CarClass carClass = (from model in db.CarClasses where model.carID == carID select model).FirstOrDefault(); //Get the model ID int modelID = carClass.modelID; //Select the model CarModel carModel = (from make in db.CarModels where make.modelID == modelID select make).FirstOrDefault(); int makeID = carModel.makeID; //map the cars properties the form if (cars != null) { ddlYear.SelectedValue = cars.modelYear.ToString(); ddlMake.SelectedValue = makeID.ToString(); Int32 makeid = Int32.Parse(ddlMake.SelectedValue); try { //Connect to EF using (COMP2007Entities ef = new COMP2007Entities()) { //Select the models from the db var models = (from d in ef.CarModels where d.makeID == makeID select d); //Bind them to the grid ddlModel.DataSource = models.ToList(); ddlModel.DataBind(); //Add please select a model to the dropdown ddlModel.Items.Insert(0, new ListItem("Please select a model", "0")); } //End using } //End TRY //Catch any error and redirect to the error page catch (SystemException ex) { Response.Redirect("/error.aspx"); } //End CATCH pnlSecondaryCarInput.Visible = true; ddlModel.SelectedValue = modelID.ToString(); txtKM.Text = cars.kilometer.ToString(); txtCost.Text = cars.cost.ToString(); txtColour.Text = cars.color; ddlEngine.SelectedIndex = cars.engineID; ddlTransmission.SelectedValue = cars.transmission; if (cars.transmission.ToString() == "Automatic") ddlTransmission.SelectedIndex = 1; if (cars.transmission.ToString() == "Manual") ddlTransmission.SelectedIndex = 2; else ddlTransmission.SelectedIndex = 1; if (cars.@new == true) rblNewUsed.SelectedIndex = 0; else rblNewUsed.SelectedIndex = 1; txtLocation.Text = cars.location; } } } //End TRY //Catch any error and redirect to the error page catch (SystemException ex) { Response.Redirect("/error.aspx"); } //End CATCH }
protected void btnPost_Click(object sender, EventArgs e) { if (checkClasses()) { string userName = User.Identity.Name; Int32 carID = 0; CarUser carUser = new CarUser(); Car car = new Car(); CarClass cClass = new CarClass(); try { //use EF to connect to SQL seer using (COMP2007Entities db = new COMP2007Entities()) { //check he query string for ID some we know add or edit if (Request.QueryString["carID"] != null) { //Get the id from url carID = Convert.ToInt32(Request.QueryString["carID"]); car = (from objs in db.Cars where objs.carID == carID select objs).FirstOrDefault(); cClass = (from objs in db.CarClasses where objs.carID == carID select objs).FirstOrDefault(); } //The URL is empty, get the userID of the logged in user. else { carUser = (from objs in db.CarUsers where objs.userName == userName select objs).FirstOrDefault(); car.userID = carUser.userID; } car.engineID = Convert.ToInt32(ddlEngine.SelectedValue); car.modelYear = Convert.ToInt32(ddlYear.SelectedValue); car.transmission = ddlTransmission.SelectedItem.Text; if (rblNewUsed.SelectedValue == "New") car.@new = true; else car.@new = false; car.color = txtColour.Text; car.cost = Convert.ToDecimal(txtCost.Text); car.location = txtLocation.Text; car.kilometer = Convert.ToInt32(txtKM.Text); car.listedDate = DateTime.Today; if (carID == 0) { db.Cars.Add(car); } db.SaveChanges(); cClass.modelID = Convert.ToInt32(ddlModel.SelectedValue); cClass.@class = carClass; if (carID == 0) { cClass.carID = car.carID; db.CarClasses.Add(cClass); } db.SaveChanges(); } } //End TRY catch (SystemException ex) { Debug.WriteLine("ERROR MESSAGE: " + ex.Message); Response.Redirect("/error.aspx"); } //End CATCH } else { lblClass.Text = "Please pick a class"; } Response.Redirect("/admin/account.aspx"); }//End btnPost_Click