Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public partial class EditUser : System.Web.UI.Page
- {
- Shop shopDB;
- Person loggedInPerson;
- protected void Page_Load(object sender, EventArgs e)
- {
- //if session variable is null, sent to login page
- if (Session["ShopDB"] == null)
- {
- Response.Redirect("LoginPage.aspx");
- }
- else
- {
- //load a session variable into normal variable
- shopDB = (Shop)Session["ShopDB"];
- }
- ////Do not repopulate listbox after the page is refreshed
- if (!IsPostBack)
- {
- //Binding the listbox to the tshirt dictionary
- //The SELECT method i used only so I can assign I want to show the products name in the listbox (called TextField) and use the products key for the listbox Value (called ValueField)
- //Note: 'd' can be called anything
- lstUser.DataSource = shopDB.getPeople().Select(d => new { id = d.Key, text = d.Value.getEmail() });
- lstUser.DataTextField = "text";
- lstUser.DataValueField = "id";
- lstUser.DataBind();
- }
- }
- /// <summary>
- /// Button clicked to delete old instance from db and create new
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- protected void btnAdd_Click(object sender, EventArgs e)
- {
- string firstname = txtFirstName.Text;
- string surname = txtSurname.Text;
- string email = txtEmail.Text;
- string password = txtPassword.Text;
- string phoneNumber = txtPhoneNumber.Text;
- string street = txtStreet.Text;
- string town = txtTown.Text;
- string postcode = txtPostcode.Text;
- string role = Convert.ToString(ddlRole.SelectedItem.Text);
- //make sure all fields are filled
- if (email == "" || password == "" || firstname == "" || surname == "" || phoneNumber == "" || email == "" || street == "" || street == "" || town == "" || postcode == "" || role == "")
- {
- lblDisplay.Text = "Please enter all details.";
- }
- else
- {
- ///selected user
- string selectedEmail = Convert.ToString(lstUser.SelectedValue);
- //sql
- string sqlQuery = "DELETE FROM People WHERE email='" + selectedEmail + "';";
- string connectionString = ConfigurationManager.ConnectionStrings["ShopDB"].ConnectionString;
- SqlConnection connection = new SqlConnection(connectionString);
- SqlCommand command = new SqlCommand(sqlQuery, connection);
- try
- {
- //open connection string
- connection.Open();
- command.ExecuteNonQuery();
- lblDisplay.Text = "USER EDITED ";
- }
- catch (Exception ex)
- {
- string message = ex.Message;
- lblDisplay.Text = "ERROR DELETING";
- }
- finally
- {
- connection.Close();
- }
- ///removes from db
- shopDB.getPeople().Remove(selectedEmail);
- Person newPerson = new Person(email, password, firstname, surname, street, town, postcode, phoneNumber, role);
- shopDB.getPeople().Add(email, newPerson);
- //saves to db
- shopDB.SavePeopleToDB();
- lblDisplay.Text = "You have edited successfully";
- }
- }
- protected void btnClear_Click(object sender, EventArgs e)
- {
- Response.Redirect("EditUser.aspx");
- }
- /// <summary>
- /// Gets peron details when clicked on
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- protected void lstUser_SelectedIndexChanged1(object sender, EventArgs e)
- {
- //retireve the selected item from the list box
- string selectedPersonID = lstUser.SelectedValue;
- //use id to retrive the correct member from dict
- Person selectedPerson = shopDB.getPeople()[selectedPersonID];
- //show details in textboxes allowing the staff member to edit the dteails
- txtEmail.Text = Convert.ToString(selectedPerson.getEmail());
- txtFirstName.Text = Convert.ToString(selectedPerson.getFirstName());
- txtSurname.Text = Convert.ToString(selectedPerson.getSurname());
- txtPassword.Text = Convert.ToString(selectedPerson.getPassword());
- txtPhoneNumber.Text = Convert.ToString(selectedPerson.getPhonenumber());
- txtStreet.Text = Convert.ToString(selectedPerson.getStreet());
- txtTown.Text = Convert.ToString(selectedPerson.getTown());
- txtPostcode.Text = Convert.ToString(selectedPerson.getPostcode());
- ddlRole.Text = Convert.ToString(selectedPerson.getRole());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement