The next step will be to add the code to allow the user to
create or update records in the table.
1.
Switch to Design view and double click on the Save button to create the
button click event handler.
2.
Add the following code to the click event handler.
protected void btnSave_Click(object sender, EventArgs e)
{
ArrayList validationErrors = new ArrayList();
UserAccountEO userAccount = new UserAccountEO();
if (ddlUsers.SelectedItem.Value != "")
{
//Updating
userAccount.Id = Convert.ToInt32(ddlUsers.SelectedValue);
}
userAccount.FirstName = txtFirstName.Text;
userAccount.LastName = txtLastName.Text;
userAccount.Save(ref validationErrors);
if (validationErrors.Count > 0)
{
ShowValidationMessages(validationErrors);
}
else
{
//Reload the drop down list
LoadUserDropDownList();
//Select the one the user just saved.
ddlUsers.Items.FindByValue(userAccount.Id.ToString()).Selected = true;
lblErrorLabel.Visible = false;
lblErrorMessages.Visible = false;
}
}
private void ShowValidationMessages(ArrayList validationErrors)
{
if (validationErrors.Count > 0)
{
lblErrorLabel.Visible = true;
lblErrorMessages.Visible = true;
lblErrorMessages.Text = "<br />";
foreach (string message in validationErrors)
{
lblErrorMessages.Text += message + "<br />";
}
}
else
{
lblErrorLabel.Visible = false;
lblErrorMessages.Visible = false;
}
}
This code
creates an ArrayList to hold any validation errors that are discovered in the
Business Layer. It then creates an instance of the UserAccountEO object and
sets its properties based on what the user has entered on the web page. After
the Save method is called the Count of the validationErrors is checked. If
there are errors then they are displayed to the user by calling the
ShowValidationMessages method.