/// <summary> /// fires when Delete button is clicked /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void DeleteRecord(object sender, GridViewDeleteEventArgs e) { int personID = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString()); // instantiate BAL PersonBAL3 pBAL = new PersonBAL3(); try { pBAL.Delete(personID); lblMessage.Text = "Record Deleted Successfully."; } catch (Exception ee) { lblMessage.Text = ee.Message.ToString(); } finally { pBAL = null; } GridView1.EditIndex = -1; // Refresh the list BindGrid(); }
/// <summary> /// Add records into database /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void AddRecords(object sender, EventArgs e) { //Lets validate the page first if (!Page.IsValid) { return; } int intResult = 0; // Page is valid, lets go ahead and insert records // Instantiate BAL object PersonBAL3 pBAL = new PersonBAL3(); // Instantiate the object we have to deal with string firstName = txtFirstName.Text; string lastName = txtLastName.Text; int age = Int32.Parse(txtAge.Text); try { intResult = pBAL.Insert(firstName, lastName, age); if (intResult > 0) { lblMessage.Text = "New record inserted successfully."; } else { lblMessage.Text = "FirstName [<b>" + txtFirstName.Text + "</b>] alredy exists, try another name"; } } catch (Exception ee) { lblMessage.Text = ee.Message.ToString(); } finally { pBAL = null; } }
/// <summary> /// Fires when Update button is clicked /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void UpdateRecord(object sender, GridViewUpdateEventArgs e) { int personID = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString()); int intResult = 0; GridViewRow row = GridView1.Rows[e.RowIndex]; TextBox tFN = (TextBox)row.FindControl("txtFName"); TextBox tLN = (TextBox)row.FindControl("txtLName"); TextBox tAge = (TextBox)row.FindControl("txtAge"); // instantiate BAL PersonBAL3 pBAL = new PersonBAL3(); try { intResult = pBAL.Update(personID, tFN.Text, tLN.Text, int.Parse(tAge.Text)); if (intResult > 0) { lblMessage.Text = "Record Updated Successfully."; } else { lblMessage.Text = "Record couldn't updated"; } } catch (Exception ee) { lblMessage.Text = ee.Message.ToString(); } finally { pBAL = null; } GridView1.EditIndex = -1; // Refresh the list BindGrid(); }