protected void btnAddUpdate_Click(object sender, EventArgs e) { //instantiate a new user class se256_Dobachesky.User aUser; aUser = new se256_Dobachesky.User(); aUser.user_email = txtEmail.Text.Trim(); aUser.user_first = txtFirstName.Text.Trim(); aUser.user_last = txtLastName.Text.Trim(); aUser.user_add1 = txtAddress1.Text.Trim(); aUser.user_add2 = txtAddress2.Text.Trim(); aUser.user_city = txtCity.Text.Trim(); aUser.state_id = ddlState.SelectedValue.Trim(); aUser.user_zip = TxtZip.Text.Trim(); aUser.user_password = txtPassword.Text.Trim(); aUser.user_phone = txtPhone.Text.Trim(); aUser.user_active = Convert.ToBoolean(true.ToString()); //run the insert method, and if it fails display an error message in a label if (se256_Dobachesky.User.InsertUser(aUser)) { Response.Redirect("/Home"); } else { lblMessage.Text = "Error signing up!"; } }
//function that sets the form to the information of the selected primary key private void BindData(int userID) { //if the class if doesn't equal -1, set the page up as an update if (userID != -1) { //make the button say update btnAddUpdate.Text = "Update"; //instantiate a new class of the form type, passing in the routeData primary key se256_Dobachesky.User aUser = new se256_Dobachesky.User(userID); //if the data exists, fill the form with it's information if (aUser != null) { txtEmail.Text = aUser.user_email; txtFirstName.Text = aUser.user_first; txtLastName.Text = aUser.user_last; txtAddress1.Text = aUser.user_add1; txtAddress2.Text = aUser.user_add2; txtCity.Text = aUser.user_city; ddlState.SelectedValue = aUser.state_id; TxtZip.Text = aUser.user_zip; txtPassword.Text = aUser.user_password; txtPhone.Text = aUser.user_phone; chkIsActive.Checked = aUser.user_active; } } //if the class does equal -1, set the form up as an add else { btnAddUpdate.Text = "Add"; } }
protected void btnAddUpdate_Click(object sender, EventArgs e) { //instantiate a new class se256_Dobachesky.User aUser; //if the routedata information exists, finish the class by passing in the primary key id if (RouteData.Values["userID"] != null) { //make a new user class using it's overloaded constructor aUser = new se256_Dobachesky.User(Convert.ToInt32(RouteData.Values["userID"].ToString())); } //if the routedata information does not exist, finish the class with a blank class else { aUser = new se256_Dobachesky.User(); } //set the object's properties to be equal to the information on the form aUser.user_email = txtEmail.Text.Trim(); aUser.user_first = txtFirstName.Text.Trim(); aUser.user_last = txtLastName.Text.Trim(); aUser.user_add1 = txtAddress1.Text.Trim(); aUser.user_add2 = txtAddress2.Text.Trim(); aUser.user_city = txtCity.Text.Trim(); aUser.state_id = ddlState.SelectedValue.Trim(); aUser.user_zip = TxtZip.Text.Trim(); aUser.user_password = txtPassword.Text.Trim(); aUser.user_phone = txtPhone.Text.Trim(); aUser.user_active = Convert.ToBoolean(chkIsActive.Checked.ToString()); //if the primary key exists go forward with the update if (aUser.user_id > 0) { //run the update method, and if it fails display an error message in a label if (se256_Dobachesky.User.UpdateUser(aUser)) { Response.Redirect("/Admin/Users"); } else { lblMessage.Text = "User update failed!"; } } //if the primary key does not exist it is because the record does not exist yet, go forward with the add else { //run the insert method, and if it fails display an error message in a label if (se256_Dobachesky.User.InsertUser(aUser)) { Response.Redirect("/Admin/Users"); } else { lblMessage.Text = "User insert failed!"; } } }
public static bool UpdateUser(se256_Dobachesky.User aUser) { //start out with the boolean value at false bool blnSuccess = false; //create sql connection object that gets connection string from web.config SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SE256_CS"].ConnectionString); //create sql command as stored procedure SqlCommand cmd = new SqlCommand("users_update", conn); //set the command as a stored procedure cmd.CommandType = CommandType.StoredProcedure; //add the parameters to be used in the update method cmd.Parameters.Add("@user_id", SqlDbType.Int).Value = aUser.user_id; cmd.Parameters.Add("@user_email", SqlDbType.VarChar).Value = aUser.user_email; cmd.Parameters.Add("@user_first", SqlDbType.VarChar).Value = aUser.user_first; cmd.Parameters.Add("@user_last", SqlDbType.VarChar).Value = aUser.user_last; cmd.Parameters.Add("@user_add1", SqlDbType.VarChar).Value = aUser.user_add1; cmd.Parameters.Add("@user_add2", SqlDbType.VarChar).Value = aUser.user_add2; cmd.Parameters.Add("@user_city", SqlDbType.VarChar).Value = aUser.user_city; cmd.Parameters.Add("@state_id", SqlDbType.VarChar).Value = aUser.state_id; cmd.Parameters.Add("@user_zip", SqlDbType.VarChar).Value = aUser.user_zip; cmd.Parameters.Add("@user_salt", SqlDbType.VarChar).Value = "salt"; cmd.Parameters.Add("@user_pwd", SqlDbType.VarChar).Value = aUser.user_password; cmd.Parameters.Add("@user_phone", SqlDbType.VarChar).Value = aUser.user_phone; cmd.Parameters.Add("@user_active", SqlDbType.Bit).Value = aUser.user_active; //try to open the connection and run the command, then set the boolean value to true try { conn.Open(); cmd.ExecuteNonQuery(); blnSuccess = true; } //prepare the error as a string and set boolean value to false catch (Exception e) { e.ToString(); blnSuccess = false; } //close the connection finally { conn.Close(); } //return the boolean containing information as to of if the operation was a success or not return(blnSuccess); }