protected void dgProgramme_ItemCommand(object sender, System.Web.UI.WebControls.DataGridCommandEventArgs e) { Programme programme = new Programme(); if (e.CommandName == "AddProgramme") { TextBox txtTempProgrammeCode = (TextBox)e.Item.Cells[0].FindControl("txtProgrammeNew"); DropDownList txtTempFacultyID = (DropDownList)e.Item.Cells[1].FindControl("ddlFacultyNameNew"); TextBox txtTempProgrammeName = (TextBox)e.Item.Cells[2].FindControl("txtProgrammeNameNew"); Regex codeRegex = new Regex(@"[A-Z]{3}$"); Match checkCode = codeRegex.Match(txtTempProgrammeCode.Text); //Validation for programme (No digit allowed and only up to 50 characters) Regex progRegex = new Regex(@"[a-zA-Z() ]{1,50}$"); Match checkProg = progRegex.Match(txtTempProgrammeName.Text); if (txtTempProgrammeCode.Text.Trim() == "") { ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script type='text/javascript'>swal('Error!', 'Please fill in the programme code!', 'error')</script>'"); } else if (!checkCode.Success) { ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script type='text/javascript'>swal('Error!', 'The programme code must have 3 uppercase characters', 'error')</script>'"); } else if (!checkProg.Success) { ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script type='text/javascript'>swal('Error!', 'Programme name can only consist characters or ().', 'error')</script>'"); } else if (txtTempFacultyID.Text.Trim() == "") { ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script type='text/javascript'>swal('Error!', 'Please fill in the faculty ID!', 'error')</script>'"); } else if (txtTempProgrammeName.Text.Trim() == "") { ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script type='text/javascript'>swal('Error!', 'Please fill in the programme name!', 'error')</script>'"); } else { if (programme.Add(txtTempProgrammeCode.Text, txtTempFacultyID.Text, txtTempProgrammeName.Text) == false) { ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script type='text/javascript'>swal('Error!', 'This code is existed already, try another programme code!', 'error')</script>'"); } else { ClientScript.RegisterStartupScript(this.GetType(), "Success", "<script type='text/javascript'>swal({title: 'Added!', text: 'This " + txtTempProgrammeName.Text + " (" + txtTempProgrammeCode.Text + ") programme is added successfully', type : 'success', confirmButtonText : 'OK'}, function (isConfirm) { if (isConfirm) { window.location.href = 'cpProgramme.aspx'; }});</script>'"); } } } else if (e.CommandName == "DeleteProgramme") { Label rowProgrammeID = (Label)e.Item.Cells[0].FindControl("lblID"); if (programme.DeleteProgramme(rowProgrammeID.Text) == false) { ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script type='text/javascript'>swal('Error!', 'Delete this programme unsuccessfully', 'error')</script>'"); } else { ClientScript.RegisterStartupScript(this.GetType(), "Success", "<script type='text/javascript'>swal({title: 'Deleted!', text: 'Deleted this programme successfully', type : 'success', confirmButtonText : 'OK'}, function (isConfirm) { if (isConfirm) { window.location.href = 'cpProgramme.aspx'; }});</script>'"); } } }