How would you refactor something like this?
protected void btnAdd_Click(object sender, EventArgs e)
{
try
{
string username = txtUsername.Text.Trim().ToLower();
string password = txtPassword.Text.Trim().ToLower();
string email = txtEmail.Text.Trim().ToLower();
string status = ddlStatus.SelectedValue.Trim();
IUser user = UserFactory.getInstance().createUser(username, password, email,status);
if (user.save())
{
jsMsgBox("Successfully added new user");
Response.Redirect(ConfigurationManager.AppSettings["AdminLink"], true);
}
else
{
jsMsgBox("An error was encountered while trying to add a new user.");
}
}
catch (Exception ex)
{
jsMsgBox("An Error was encountered while trying to add a new user.");
lblInfo.Text = ex.Message;
lblInfo.Visible = true;
}
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
try
{
string username = txtUsername.Text.Trim().ToLower();
string password = txtPassword.Text.Trim().ToLower();
string email = txtEmail.Text.Trim().ToLower();
int userPK = Int32.Parse(txtUserPK.Text.ToString());
string status = ddlStatus.SelectedValue.Trim();
IUser user = UserFactory.getInstance().createUser(userPK, username, password, email,status);
if (user.save())
{
jsMsgBox("Successfully updated selected users information.");
Response.Redirect(ConfigurationManager.AppSettings["AdminLink"], true);
}
else
{
jsMsgBox("An error was encountered while trying to update the selected users information.");
}
}
catch (Exception ex)
{
jsMsgBox("An Error was encountered while trying to update the selected users information.");
lblInfo.Text = ex.Message;
lblInfo.Visible = true;
}
}
Take care