protected void btnYes_Click(object sender, EventArgs e) { if (Page.IsValid) { // Check if there is a query string "staffid" in the URL if (Request.QueryString["staffid"] != null) { // Get the staffid from query string // and convert the query stirng to 32-bit integer. int staffID = Convert.ToInt32(Request.QueryString["staffid"]); // Create a Staff object Staff objStaff = new Staff(); // Call the Delete() method of Staff class to delete // the staff Id of the record to be deleted is passed as a property // of the staff object, the method returns an integer error // the calling program objStaff.StaffId = staffID; int errorCode = objStaff.Delete(); if (errorCode == 0) { // Display appropriate message lblMessage.Text = "Staff record has been deleted successfully."; // Disable "Yes" and "No" buttons btnYes.Enabled = false; btnNo.Enabled = false; // Display link back to View Staff Page lnkVewStaff.Visible = true; } } } }
protected void btnSubmit_Click(object sender, EventArgs e) { if (Page.IsValid) { // All validation passed // Create a new object from the Staff class Staff objStaff = new Staff(); // Pass data to the properties of the Staff object, // do the necessary data type conversion if need to. objStaff.Name = lblName.Text; // if-else required because Gender property only can contain 'M' or 'F' (char(1)) // Check database design for this portion if (lblGender.Text == "Male") objStaff.Gender = "M"; else objStaff.Gender = "F"; objStaff.Dob = Convert.ToDateTime(lblDOB.Text); objStaff.Salary = Convert.ToDouble(lblSalary.Text); objStaff.Email = lblEmail.Text; objStaff.Nationality = lblNationality.Text; if (lblIsFullTimeStaff.Text == "yes") objStaff.IsFullTime = true; else objStaff.IsFullTime = false; // Call the add method to insert the staff record to database. int errorCode = objStaff.Add(); // Record has been inserted successfully if the returned code is 0. if (errorCode == 0) lblMessage.Text = "New staff is added successfully"; } }
protected void Page_Load(object sender, EventArgs e) { if (Page.IsPostBack == false) { ddlNationality.Items.Add("Singapore"); ddlNationality.Items.Add("Malaysia"); ddlNationality.Items.Add("Indonesia"); ddlNationality.Items.Add("China"); //ddlBranch.Items.Add("1"); // *** this is hardcoding, but we will learn how to import data from db to ddl //ddlBranch.Items.Add("2"); //ddlBranch.Items.Add("3"); displayBranchDropDownList(); if (Request.QueryString["staffid"] != null) { // *** check if there is a querystring that exists //Create a new Staff object Staff objStaff = new Staff(); //Read Staff ID from query string objStaff.StaffId = Convert.ToInt32(Request.QueryString["staffid"]); // Load staff information to various controls int errorCode = objStaff.GetDetails(); // *** objStaff.staffId must be called first in order for this to work if (errorCode == 0) { //Assign property values from the Staff object to various controls txtName.Text = objStaff.Name; txtDOB.Text = objStaff.Dob.ToShortDateString(); if (objStaff.Gender == "M") radMale.Checked = true; else if (objStaff.Gender == "F") radFemale.Checked = true; //Format currency value: 2 decimal places, punctuated at thousand txtSalary.Text = objStaff.Salary.ToString("##,##0.00"); txtEmail.Text = objStaff.Email; ddlNationality.SelectedValue = objStaff.Nationality; chkFullTime.Checked = objStaff.IsFullTime; // Select Branch Number in DropDownList ddlBranch.SelectedValue = objStaff.BranchNo.ToString(); } else if (errorCode == -2) { lblMessage.Text = "Unable to retrieve staff details for ID " + objStaff.StaffId.ToString(); lblMessage.ForeColor = System.Drawing.Color.Red; } } } }
protected void btnSave_Click(object sender, EventArgs e) { if (Page.IsValid) { Staff objStaff = new Staff(); // Read Staff ID from query string passed from ViewStaff.aspx objStaff.StaffId = Convert.ToInt32(Request.QueryString["staffid"]); //Read updated values from various web controls and //transfer them to the staff object properties objStaff.Name =txtName.Text.Trim(); // also trims excess spaces between words //Trim method: remove excessive spaces if (radMale.Checked == true) objStaff.Gender = "M"; else if (radFemale.Checked == true) objStaff.Gender = "F"; objStaff.Dob = Convert.ToDateTime(txtDOB.Text.Trim()); objStaff.Salary = Convert.ToDouble(txtSalary.Text.Trim()); objStaff.Email = txtEmail.Text.Trim(); objStaff.Nationality = ddlNationality.SelectedValue; objStaff.IsFullTime = chkFullTime.Checked; if (ddlBranch.SelectedIndex != 0) // Prevent errors if user does not select objStaff.BranchNo = Convert.ToInt32(ddlBranch.SelectedValue); else objStaff.BranchNo = 0; int errorCode = objStaff.Update(); if (errorCode == 0) { lblMessage.Text = "Staff record has been updated successfully."; } else if (errorCode == -2) { lblMessage.Text = "Unable to save edited record as it is not found!"; lblMessage.ForeColor = System.Drawing.Color.Red; } } }