예제 #1
0
        protected void AddContactDetailsView_ItemInserting(object sender, DetailsViewInsertEventArgs e)
        {
            string firstName = e.Values["FirstName"].ToString();
            string lastName = e.Values["LastName"].ToString();
            string city = e.Values["City"].ToString();
            string country = e.Values["Country"].ToString();
            string phone = e.Values["PhoneNumber"].ToString();
            string email = string.Empty;

            if (e.Values["Email"] != null)
                email = e.Values["Email"].ToString();
            else
            {
                email = "";
            }

            try
            {
                AddressBookRepository context = new AddressBookRepository();
                context.InsertContact(firstName, lastName, city, country, phone, email, User.Identity.Name);
                lblMessage.Text = "Contact Added: " + e.Values[0] + e.Values[1];
            }

            catch (OptimisticConcurrencyException ocex)
            {
                lblMessage.Text = ocex.ToString();
            }
            catch (Exception)
            {
                lblMessage.Text = "An error occured on inserting new contact. Try again.";
            }
        }
예제 #2
0
        protected void Page_Load(object sender, EventArgs e)
        {
            // if user typed link directly in addresss bar then redirect it to home page (user can only come to this page by clicking on a link - "View Details")
            if (Request.UrlReferrer != null)
            {
                lblMessage.Text = "Came from" + Request.UrlReferrer.ToString() + "page.";
            }
            else
            {
                Response.Redirect("~/Default.aspx");
            }

            // check QueryString not null OR empty - if null OR empty redirect to Vehicles.aspx
            if (Request.QueryString["personID"] == null || Request.QueryString["personID"].ToString() == "")
               {
                Response.Redirect("Default.aspx");
            }

            personID = Convert.ToInt32(Request.QueryString["personID"]);

            if (!IsPostBack)
            {
                if (User.Identity.IsAuthenticated)
                {
                    AddressBookRepository context2 = new AddressBookRepository();

                    try
                    {
                        IQueryable<Person> query2 = context2.GetPersonByID(personID);
                        personDetailsView.DataSource = query2.ToList();
                        personDetailsView.DataBind();

                    }
                    catch (Exception)
                    {
                        lblMessage.Text = "An error occurred. Please try again.";
                    }
                }
                else
                {
                    //ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Access denied - You are not authorized to access this page. Please Login or Register to view this page.');", true);
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", " alert('Access denied - You are not authorized to access this page. Please Login or Register to view this page.'); window.location='../Account2/Login.aspx';", true);
                }
              }
        }
예제 #3
0
        protected void contactsGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                /*
                 cell refers to the column index of your CommandField and ctrl refers to the control index (Delete button) within the cell you're referencing.
                 stackoverflow.com/questions/14397309/how-to-call-javascript-function-in-gridview-command-field
                 */
                // reference Delete button
                LinkButton btnDelete = (LinkButton)e.Row.Cells[7].Controls[0];

                btnDelete.Attributes.Add("onclick", "javascript:return " + "confirm('Are you sure you want to delete this record: " +
                                            DataBinder.Eval(e.Row.DataItem, "FirstName") + " " + DataBinder.Eval(e.Row.DataItem, "LastName") + " PersonID: " + contactsGridView.DataKeys[e.Row.RowIndex].Values[0] + "')");

                int personID = Convert.ToInt16(contactsGridView.DataKeys[e.Row.RowIndex].Values[0]);

                try
                {
                    AddressBookRepository context = new AddressBookRepository();
                    var query = context.GetPersonByID(personID).FirstOrDefault();
                    string addedBy = query.AddedBy.Trim();

                    //  reference ViewDetails button
                    HyperLink btnDetails = (HyperLink)e.Row.Cells[8].Controls[0];
                    // show ViewDetails button only if current user is "Admin" or if contact is added by current user (logged in user)
                    btnDetails.Visible = ((User.IsInRole("canEdit")) || User.Identity.Name.ToUpper() == addedBy.ToUpper());
                    //User.Identity.GetUserId();
                    if ((User.IsInRole("canEdit")) || (User.Identity.Name.ToUpper() == addedBy.ToUpper()))
                    {
                        btnDelete.Visible = true;
                    }
                    else
                    {
                        btnDelete.Visible = false;
                    }
                }
                catch (Exception)
                {
                    lblError.Text = "An error occurred. Please try again.";
                }

            }
        }
예제 #4
0
 protected void btnDelete_Click(object sender, EventArgs e)
 {
     try
     {
         AddressBookRepository context = new AddressBookRepository();
         context.DeletePersonByID(1);
         GetPeople();
         lblError.Text = "Contact deleted.";
     }
     catch (OptimisticConcurrencyException)
     {
         lblError.Text = "There was an error while deleting record from database. The record you attempted to delete was modified by another " +
                         "user after you got the original value. The delete operation was canceled.";
     }
     catch (ArgumentNullException)
     {
         lblError.Text = "There was an error while deleting record from database. Make sure that user exist.";
     }
     catch (Exception)
     {
         lblError.Text = "There was an error while deleting record from database. Please try again.";
     }
 }
예제 #5
0
        protected void personDetailsView_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
        {
            try
            {
                int id = Convert.ToInt32(e.Keys["PersonId"]);
                lblMessage.Text = id.ToString();

                string firstName = e.NewValues["FirstName"].ToString();
                string lastName = e.NewValues["LastName"].ToString();
                string city = e.NewValues["City"].ToString();
                string country = e.NewValues["Country"].ToString();
                string phoneNumber = e.NewValues["PhoneNumber"].ToString();

                string email = string.Empty;
                if (e.NewValues["Email"] == null)
                { email = ""; }
                else
                {
                    email = e.NewValues["Email"].ToString();
                }

                AddressBookRepository context = new AddressBookRepository();
                context.UpdatePersonByID(id, firstName, lastName, city, country, phoneNumber, email, User.Identity.Name);
            }
            catch (UpdateException)
            {
                lblMessage.Text = "An error occurred while updating person information. Please try again.";
            }
            catch (ArgumentNullException)
            {
                lblMessage.Text = "An error occurred while updating person information. Make sure that person exists.";
            }
            catch (Exception)
            {
                lblMessage.Text = "An error occurred while updating person information. Please try again.";
            }
        }
예제 #6
0
        protected void contactsGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            // int index = e.RowIndex; // index of the row to delete -> where Delete button clicked
            int personID = Convert.ToInt32(contactsGridView.DataKeys[e.RowIndex].Value);

            try
            {
                AddressBookRepository context = new AddressBookRepository();
                context.DeletePersonByID(personID);
                GetPeople();
                lblError.Text = "Contact " + " " + e.Values["FirstName"] + " " + e.Values["LastName"] + " deleted!";
            }
            catch (OptimisticConcurrencyException)
            {
                lblError.Text = "There was an error while deleting record from database. The record you attempted to delete was modified by another " +
                                 "user after you got the original value. The delete operation was canceled.";
            }
            catch (ArgumentNullException)
            {
                lblError.Text = "An error occured while deleting contact. Make sure that contact exists.";
            }
            catch (Exception)
            {
                lblError.Text = "An error occured while deleting contact. Please try again.";
            }
        }
예제 #7
0
 protected void GetPeople()
 {
     try
     {
         AddressBookRepository context = new AddressBookRepository();
         var query = context.GetPeople();
         contactsGridView.DataSource = query;
         contactsGridView.DataBind();
     }
     catch (Exception)
     {
         lblError.Text = "There was an error while retrieving data from database. Please try again.";
     }
 }