SHARE
TWEET

editUser

a guest Jun 8th, 2017 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public partial class EditUser : System.Web.UI.Page
  2. {
  3.     Shop shopDB;
  4.     Person loggedInPerson;
  5.  
  6.     protected void Page_Load(object sender, EventArgs e)
  7.     {
  8.         //if session variable is null, sent to login page
  9.         if (Session["ShopDB"] == null)
  10.         {
  11.             Response.Redirect("LoginPage.aspx");
  12.         }
  13.         else
  14.         {
  15.             //load a session variable into normal variable
  16.             shopDB = (Shop)Session["ShopDB"];
  17.         }
  18.  
  19.  
  20.         ////Do not repopulate listbox after the page is refreshed
  21.         if (!IsPostBack)
  22.         {
  23.             //Binding the listbox to the tshirt dictionary
  24.             //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)
  25.             //Note: 'd' can be called anything
  26.  
  27.  
  28.             lstUser.DataSource = shopDB.getPeople().Select(d => new { id = d.Key, text = d.Value.getEmail() });
  29.  
  30.  
  31.  
  32.             lstUser.DataTextField = "text";
  33.             lstUser.DataValueField = "id";
  34.             lstUser.DataBind();
  35.         }
  36.     }
  37.  
  38.  
  39.        
  40.     /// <summary>
  41.     /// Button clicked to delete old instance from db and create new
  42.     /// </summary>
  43.     /// <param name="sender"></param>
  44.     /// <param name="e"></param>
  45.     protected void btnAdd_Click(object sender, EventArgs e)
  46.     {
  47.         string firstname = txtFirstName.Text;
  48.         string surname = txtSurname.Text;
  49.         string email = txtEmail.Text;
  50.         string password = txtPassword.Text;
  51.         string phoneNumber = txtPhoneNumber.Text;
  52.  
  53.         string street = txtStreet.Text;
  54.         string town = txtTown.Text;
  55.         string postcode = txtPostcode.Text;
  56.         string role = Convert.ToString(ddlRole.SelectedItem.Text);
  57.  
  58.         //make sure all fields are filled
  59.         if (email == "" || password == "" || firstname == "" || surname == "" || phoneNumber == "" || email == "" || street == "" || street == "" || town == "" || postcode == "" || role == "")
  60.         {
  61.             lblDisplay.Text = "Please enter all details.";
  62.         }
  63.        
  64.         else
  65.            
  66.                 {
  67.  
  68.             ///selected user
  69.             string selectedEmail = Convert.ToString(lstUser.SelectedValue);
  70.  
  71.  
  72.             //sql
  73.             string sqlQuery = "DELETE FROM People WHERE email='" + selectedEmail + "';";
  74.  
  75.             string connectionString = ConfigurationManager.ConnectionStrings["ShopDB"].ConnectionString;
  76.             SqlConnection connection = new SqlConnection(connectionString);
  77.             SqlCommand command = new SqlCommand(sqlQuery, connection);
  78.  
  79.  
  80.  
  81.  
  82.  
  83.             try
  84.             {
  85.                 //open connection string
  86.                 connection.Open();
  87.  
  88.                 command.ExecuteNonQuery();
  89.  
  90.                 lblDisplay.Text = "USER EDITED ";
  91.             }
  92.             catch (Exception ex)
  93.             {
  94.                 string message = ex.Message;
  95.                 lblDisplay.Text = "ERROR DELETING";
  96.             }
  97.             finally
  98.             {
  99.                 connection.Close();
  100.             }
  101.  
  102.             ///removes from db
  103.             shopDB.getPeople().Remove(selectedEmail);
  104.  
  105.  
  106.             Person newPerson = new Person(email, password, firstname, surname, street, town, postcode, phoneNumber, role);
  107.                     shopDB.getPeople().Add(email, newPerson);
  108.  
  109.             //saves to db
  110.                     shopDB.SavePeopleToDB();
  111.  
  112.                     lblDisplay.Text = "You have edited successfully";
  113.  
  114.  
  115.                    
  116.                 }
  117.             }
  118.    
  119.  
  120.  
  121.  
  122.  
  123. protected void btnClear_Click(object sender, EventArgs e)
  124.     {
  125.         Response.Redirect("EditUser.aspx");
  126.     }
  127.  
  128.     /// <summary>
  129.     /// Gets peron details when clicked on
  130.     /// </summary>
  131.     /// <param name="sender"></param>
  132.     /// <param name="e"></param>
  133.     protected void lstUser_SelectedIndexChanged1(object sender, EventArgs e)
  134.     {
  135.  
  136.  
  137.         //retireve the selected item from the list box
  138.         string selectedPersonID = lstUser.SelectedValue;
  139.  
  140.         //use id to retrive the correct member from dict
  141.         Person selectedPerson = shopDB.getPeople()[selectedPersonID];
  142.  
  143.         //show details in textboxes allowing the staff member to edit the dteails
  144.         txtEmail.Text = Convert.ToString(selectedPerson.getEmail());
  145.         txtFirstName.Text = Convert.ToString(selectedPerson.getFirstName());
  146.         txtSurname.Text = Convert.ToString(selectedPerson.getSurname());
  147.         txtPassword.Text = Convert.ToString(selectedPerson.getPassword());
  148.         txtPhoneNumber.Text = Convert.ToString(selectedPerson.getPhonenumber());
  149.         txtStreet.Text = Convert.ToString(selectedPerson.getStreet());
  150.         txtTown.Text = Convert.ToString(selectedPerson.getTown());
  151.         txtPostcode.Text = Convert.ToString(selectedPerson.getPostcode());
  152.         ddlRole.Text = Convert.ToString(selectedPerson.getRole());
  153.  
  154.  
  155.     }
  156. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top