protected void btnUpdate_Click(object sender, EventArgs e) { btnUpdate.Visible = false; BtnCancel.Visible = false; btnEditGrid.Visible = true; #region Converting the dropdown boxes foreach (GridViewRow row in ViewAllTechsGrid.Rows) //Converting the dropdown boxes to integers { if (row.RowType == DataControlRowType.DataRow) //Checks to make sure the row contains data { for (int i = 1; i < row.Cells.Count; i++) { //Employed dropdown boxes if (row.Cells[i].Controls.OfType <DropDownList>().ToList().Count > 0 && row.Cells[i].Controls.OfType <DropDownList>().FirstOrDefault().SelectedIndex == 0 && row.Cells[i].Controls.OfType <DropDownList>().FirstOrDefault().ID.Equals("DropDown1")) //If the value in the dropdown list is "Yes" { row.Cells[i].Controls.OfType <DropDownList>().FirstOrDefault().SelectedItem.Value = "0"; Convert.ToInt32(row.Cells[i].Controls.OfType <DropDownList>().FirstOrDefault().Text); } if (row.Cells[i].Controls.OfType <DropDownList>().ToList().Count > 0 && row.Cells[i].Controls.OfType <DropDownList>().FirstOrDefault().SelectedIndex == 1 && row.Cells[i].Controls.OfType <DropDownList>().FirstOrDefault().ID.Equals("DropDown1")) //If the value in the dropdown list is "No" { row.Cells[i].Controls.OfType <DropDownList>().FirstOrDefault().SelectedItem.Value = "1"; Convert.ToInt32(row.Cells[i].Controls.OfType <DropDownList>().FirstOrDefault().Text); } //Technician Type dropdown boxes if (row.Cells[i].Controls.OfType <DropDownList>().ToList().Count > 0 && row.Cells[i].Controls.OfType <DropDownList>().FirstOrDefault().SelectedValue == "Support Officer Level 1" && row.Cells[i].Controls.OfType <DropDownList>().FirstOrDefault().ID.Equals("DropDown2")) //If the value in the dropdown list is "Level 1" { row.Cells[i].Controls.OfType <DropDownList>().FirstOrDefault().SelectedItem.Value = "1"; Convert.ToInt32(row.Cells[i].Controls.OfType <DropDownList>().FirstOrDefault().Text); } if (row.Cells[i].Controls.OfType <DropDownList>().ToList().Count > 0 && row.Cells[i].Controls.OfType <DropDownList>().FirstOrDefault().SelectedValue == "Technician Level 2" && row.Cells[i].Controls.OfType <DropDownList>().FirstOrDefault().ID.Equals("DropDown2")) //If the value in the dropdown list is "Level 2" { row.Cells[i].Controls.OfType <DropDownList>().FirstOrDefault().SelectedItem.Value = "2"; Convert.ToInt32(row.Cells[i].Controls.OfType <DropDownList>().FirstOrDefault().Text); } } } } #endregion //UPDATING THE DATABASE foreach (GridViewRow row in ViewAllTechsGrid.Rows) { if (row.RowType == DataControlRowType.DataRow) { SqlCommand cmd = new SqlCommand("UPDATE [Technicians] SET [Name] = @Name, [Email] = @Email, [Phone] = @Phone, [Employed] = @Employed, [TypeID] = @TypeID WHERE [TechID] = @TechID"); cmd.Parameters.AddWithValue("@Name", row.Cells[1].Controls.OfType <TextBox>().FirstOrDefault().Text); cmd.Parameters.AddWithValue("@Email", row.Cells[2].Controls.OfType <TextBox>().FirstOrDefault().Text); cmd.Parameters.AddWithValue("@Phone", row.Cells[3].Controls.OfType <TextBox>().FirstOrDefault().Text); cmd.Parameters.AddWithValue("@Employed", row.Cells[4].Controls.OfType <DropDownList>().FirstOrDefault().SelectedItem.Value); cmd.Parameters.AddWithValue("@TypeID", row.Cells[5].Controls.OfType <DropDownList>().FirstOrDefault().SelectedItem.Value); cmd.Parameters.AddWithValue("@TechID", ViewAllTechsGrid.DataKeys[row.RowIndex].Value); //Connection string conStr = ConfigurationManager.ConnectionStrings["TechSupportConnectionString"].ConnectionString; using (SqlConnection con = new SqlConnection(conStr)) { cmd.Connection = con; try { con.Open(); cmd.ExecuteNonQuery(); con.Close(); } catch (Exception) { throw; } } } } ViewAllTechsGrid.DataBind(); //Rebinds the grid to the datasource and refreshes it }
protected void btnSearch_Click(object sender, EventArgs e) { ViewAllTechsGrid.DataSourceID = "TechSearch"; lblViewLevel1.Visible = false; lblViewLevel2.Visible = false; lblEditDetails.Visible = false; lblSearchByID.Visible = true; lblViewAllTechs.Visible = false; btnViewAllTechs.Enabled = true; btnEditDetails.Enabled = true; btnViewLevel1.Enabled = true; btnViewLevel2.Enabled = true; btnEditGrid.Visible = false; btnUpdate.Visible = false; BtnCancel.Visible = false; string pattern = @"[a-z A-Z]|[^\w \xC0-\xFF]"; //Pattern that matches anything but a number Regex regexNotNumbers = new Regex(pattern); Match match = regexNotNumbers.Match(txtBoxSearch.Text); if (txtBoxSearch.Text.Equals("")) { lblErrorText.Text = "The search box is empty, please input a number to continue."; lblErrorText.Visible = true; ViewAllTechsGrid.Visible = false; } else if (match.Success) { lblErrorText.Text = "The search box contains invalid characters or is empty, please input a number to continue."; lblErrorText.Visible = true; ViewAllTechsGrid.Visible = false; } else { lblErrorText.Visible = false; ViewAllTechsGrid.Visible = true; TechSearch.SelectCommand = "SELECT * FROM Technicians WHERE TechID = '" + txtBoxSearch.Text + "'"; } SqlCommand cmd = new SqlCommand("SELECT TechID FROM Technicians WHERE TechID = '" + txtBoxSearch.Text + "'"); if (!match.Success) { //Connection string conStr = ConfigurationManager.ConnectionStrings["TechSupportConnectionString"].ConnectionString; using (SqlConnection con = new SqlConnection(conStr)) { cmd.Connection = con; try { con.Open(); SqlDataReader reader = cmd.ExecuteReader(); if (reader.HasRows == false) //If the reader hasn't retrieved any rows then the id doesnt exist in the database, display error message { lblErrorText.Visible = true; lblErrorText.Text = "That Technician doesn't exist in the database."; } con.Close(); } catch (Exception) { throw; } } } else { lblErrorText.Text = "The search box contains invalid characters or is empty, please input a number to continue."; lblErrorText.Visible = true; ViewAllTechsGrid.Visible = false; } ViewAllTechsGrid.DataBind(); }