//Edit Button click protected void btnCreate_Click(object sender, EventArgs e) { //check a record has been selected if (id != -1 && Servicevalidation()) { DataRow record = repairsDataSet.service.FindByid(id); // find the related Record try { //update record with user's input record[1] = this.txtName.Text; record[2] = this.txtDescription.Text; record[3] = Convert.ToDecimal(this.txtPrice.Text); serviceTableAdapter daservice = new serviceTableAdapter(); daservice.Update(record); // Call update method on the service adapter so it updates the table in memory ( All changes made are applied - CRUD) repairsDataSet.AcceptChanges(); // Call accept method on the dataset so it update the chanmges to the database this.Label1.Visible = true; Label1.Text = "✔ Record Successfully Updated"; // Response.Redirect("Services.aspx"); // Redirect the user to Edit page on btn click } catch { this.Label1.ForeColor = Color.Red; this.Label1.Visible = true; Label1.Text = "❌ Record Updation Failed"; } } }
protected void btnCreate_Click(object sender, EventArgs e) { if (Servicevalidation()) { try { DataRow service = RepairsDataSet.service.NewRow(); // Create a new row of service table in memory //update record with user's input service[1] = this.txtName.Text; service[2] = this.txtDescription.Text; service[3] = this.txtPrice.Text; RepairsDataSet.service.Rows.Add(service); // add the rows to the dataset serviceTableAdapter daService = new serviceTableAdapter(); daService.Update(service); // Call update method on the service adapter so it updates the table in memory ( All changes made are applied - CRUD) RepairsDataSet.AcceptChanges(); // Call accept method on the dataset so it update the chanmges to the database //Refresh the page to show the record being deleted // Response.Redirect("Services.aspx"); // Redirect the user to dexpage on to show created data this.Label1.Visible = true; this.Label1.Text = "✔ Record Successfully Created"; this.Label1.ForeColor = Color.Green; } catch { this.Label1.Visible = true; this.Label1.Text = "❌ Record Creation Failed"; this.Label1.ForeColor = Color.Red; } } }
static NewService() { RepairsDataSet = new RepairsDataSet(); serviceTableAdapter daservices = new serviceTableAdapter(); try { daservices.Fill(RepairsDataSet.service); } catch { flag = true; } }
protected void Page_Load(object sender, EventArgs e) { if (!User.Identity.IsAuthenticated) //if not logged in { Response.Redirect("/"); } if (Request.Cookies["ID"] != null) // Request the cookies which contaions the ID Of thr record that was carried over from the index page { Label1.Text = Request.Cookies["ID"].Value; } id = Convert.ToInt32(Request.Cookies["ID"].Value); if (!IsPostBack) { if (id != -1) { try { serviceTableAdapter daservices = new serviceTableAdapter(); daservices.Fill(repairsDataSet.service); DataRow Service = repairsDataSet.service.FindByid(id); // Find the related Record and fill the fields in the page with the data if (Service != null) { this.txtName.Text = Service.ItemArray[1].ToString(); this.txtDescription.Text = Service.ItemArray[2].ToString(); this.txtPrice.Text = Service.ItemArray[3].ToString(); } else { this.Label1.Visible = true; Label1.Text = "Please Try Again"; } } catch { this.Label1.Visible = true; Label1.Text = "❌ Database Eror, Contact System Administrator"; } } else { Servicevalidation(); } } }
protected void Page_Load(object sender, EventArgs e) { if (!User.Identity.IsAuthenticated) //if not logged in { Response.Redirect("/"); } try { //refresh the dataset, so the newly created record is shown in index serviceTableAdapter daservices = new serviceTableAdapter(); daservices.Fill(RepairsDataSet.service); } catch { this.Label1.Text = "Failed to load Data, Please Contact the system administrator"; this.Label1.ForeColor = Color.Red; return; } Session["RepairCriteria"] = null; if (Session["deleteMsg"] != null) { if (Session["deleteMsg"].ToString() == "true") { this.lblDeleteMsg.Visible = true; this.lblDeleteMsg.Text = "✔ Record deleted Successfully"; Session["deleteMsg"] = null; this.pnlDeleteConfirm.Visible = false; } else { this.lblDeleteMsg.Visible = true; this.lblDeleteMsg.Text = "❌ Record not deleted, please check if this service is related to any repairs"; Session["deleteMsg"] = null; this.lblDeleteMsg.ForeColor = Color.Red; this.pnlDeleteConfirm.Visible = false; } } //get records rows = (Session["ServiceCriteria"] != null) ? RepairsDataSet.service.Select(Session["ServiceCriteria"].ToString()) //has criteria : RepairsDataSet.service.Select(); //select all DisplayServiceTable(); }
// Filter Search Btn protected void btnSearch_Click(object sender, EventArgs e) { serviceTableAdapter daservices = new serviceTableAdapter(); daservices.Fill(RepairsDataSet.service); if (RepairsDataSet.service.Count > 0) { string criteria = FilterCriteria(); Session["ServiceCriteria"] = FilterCriteria(); rows = (criteria.Length > 0) ? RepairsDataSet.service.Select(criteria) : RepairsDataSet.service.Select(); // Data satisfying the conditions is saved in rows DisplayServiceTable(); } else { this.Label1.Text = "Database Error, Please Contact The System Administarator"; this.Label1.ForeColor = Color.Red; } }
protected void btnDeleteConfirm_Click(object sender, EventArgs e) { if (id != -1) { try { DataRow record = RepairsDataSet.service.FindByid(id); // Find and add the record to tbe record variable record.Delete(); // Deletes the record in memory serviceTableAdapter daservice = new serviceTableAdapter(); // table adapter to service table (Service adapter) daservice.Update(record); // Call update method on the service adapter so it updates the table in memory ( All changes made are applied - CRUD) RepairsDataSet.AcceptChanges(); // Call accept method on the dataset so it update the chanmges to the database //Refresh the page to show the record being deleted Session["deleteMsg"] = "true"; this.Label1.Text = "✔ Record Deleted Successfully"; } catch (Exception ex) { if (ex.Message.Contains("constraint")) { Session["deleteMsg"] = "false"; } else { this.Label1.Text = "❌ Record Deletion Failed"; this.Label1.ForeColor = Color.Red; } } finally { Response.Redirect(Request.RawUrl); } } }