Ejemplo n.º 1
0
        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
        }
Ejemplo n.º 2
0
        } //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
Ejemplo n.º 3
0
        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
        }
Ejemplo n.º 4
0
        } //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
Ejemplo n.º 5
0
        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();
        }
Ejemplo n.º 6
0
        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