/// <summary> /// when the find customer button is clicked the fields for all the customer informationm textboxes are filled with the data from the selected customer in the GridView /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnFindCustomer_Click(object sender, EventArgs e) { form1.DefaultButton = "btnSubmit"; addmodifycustomers.Attributes["class"] = "nothidden"; ProjectCustomer selectedRecord = GetSelectedRecord(); if (selectedRecord.CustomerID != 0) { txtEmail.DataBind(); GridView1.DataSourceID = sdsSelectedCustomer.ID; Session["InsertNewCustomer"] = false; Session["LoadedCustomer"] = selectedRecord; txtCustomerFirst.Text = selectedRecord.CustomerFname; txtCustomerLast.Text = selectedRecord.CustomerLName; txtCustomerAddress.Text = selectedRecord.CustomerStreetAddress; txtCustomerCity.Text = selectedRecord.CustomerCity; ddlState.SelectedIndex = ddlState.Items.IndexOf(ddlState.Items.FindByValue(selectedRecord.CustomerState)); txtCustomerZip.Text = selectedRecord.CustomerZipCode; txtCustomerPhone.Text = selectedRecord.CustomerPhone; } //if the customer is new the delete button will not work because there is no record yet that exists to delete else { btnDelete.Enabled = false; //SHOW HIDDEN CUSTOMER FIELDS SO THEY CAN ADD NEW CUSTOMER } }
/// <summary> /// this code gets the customer information from the database and populates the customer data fields /// </summary> /// <returns></returns> private ProjectCustomer GetSelectedRecord() { ProjectCustomer p = new ProjectCustomer(); DataView customersTable = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty); customersTable.RowFilter = string.Format("CustomerEmail = '{0}'", txtEmail.Text); try { DataRowView row = customersTable[0]; p.CustomerID = Convert.ToInt32(row["CustomerID"]); p.CustomerFname = row["CustomerFName"].ToString(); p.CustomerLName = row["CustomerLName"].ToString(); p.CustomerStreetAddress = row["CustomerStreetAddress"].ToString(); p.CustomerCity = row["CustomerCity"].ToString(); p.CustomerState = row["CustomerState"].ToString(); p.CustomerZipCode = row["CustomerZipCode"].ToString(); p.CustomerPhone = row["CustomerPhone"].ToString(); } catch (IndexOutOfRangeException) { } return(p); }
/// <summary> /// this delete button when clicked will first warn the user that the record will be deleted forever and then changes the button text to say "confirm delete" /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnDelete_Click(object sender, EventArgs e) { if (!lblDeleteConfirmation.Visible) { lblDeleteConfirmation.Text = "Deleting a customer is an irreversable process. If you would like to continue, please click "Confirm Delete""; lblDeleteConfirmation.Visible = true; btnDelete.Text = "Confirm Delete"; } else if (!((bool)Session["InsertNewCustomer"])) { string strConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MMABooksConnectionString2"].ConnectionString; SqlConnection con = new SqlConnection(); con.ConnectionString = strConnectionString; SqlCommand command; ProjectCustomer existing = (ProjectCustomer)Session["LoadedCustomer"]; con.Open(); //DELETE customer from CustomerTable string strDeleteCustomer = "DELETE FROM CustomerTable WHERE CustomerID=@ID;" ; command = new SqlCommand(strDeleteCustomer, con); command.Parameters.AddWithValue("@ID", existing.CustomerID); command.ExecuteNonQuery(); con.Close(); GridView1.DataSourceID = SqlDataSource1.ID; txtEmail.Text = ""; txtCustomerFirst.Text = ""; txtCustomerLast.Text = ""; txtCustomerAddress.Text = ""; txtCustomerCity.Text = ""; ddlState.SelectedIndex = 0; txtCustomerZip.Text = ""; txtCustomerPhone.Text = ""; addmodifycustomers.Attributes["class"] = "hidden"; lblDeleteConfirmation.Visible = false; Session["InsertNewCustomer"] = true; btnDelete.Text = "Delete Customer"; } //if the customer record does not yet exist it will not delete anything and an error message will display to the user else { lblDeleteConfirmation.Text = "You cannot delete a new customer."; lblDeleteConfirmation.Visible = true; } }
/// <summary> /// If user indicates they are an existing customer, then it will populate their information based on their email address. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnExistingCustomer_Click(object sender, EventArgs e) { //imports record of customer selectedRecord = this.GetSelectedRecord(); //customerID can't be 0, so if the customer ID isn't 0 they must exist in the DB if (selectedRecord.CustomerID != 0) { //customer isn't new Session["IsExistingCustomer"] = true; Session["ExistingCustomer"] = selectedRecord; txtEmail.DataBind(); //populate fields with customer info txtFirstName.Text = selectedRecord.CustomerFname; txtLastName.Text = selectedRecord.CustomerLName; txtAddress.Text = selectedRecord.CustomerStreetAddress; txtCity.Text = selectedRecord.CustomerCity; ddlState.SelectedIndex = ddlState.Items.IndexOf(ddlState.Items.FindByValue(selectedRecord.CustomerState)); txtZip.Text = selectedRecord.CustomerZipCode; txtPhoneNumber.Text = selectedRecord.CustomerPhone; } }
/// <summary> /// polulate existing customer information /// </summary> /// <returns></returns> private ProjectCustomer GetSelectedRecord() { DataView customersTable = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty); //selects customer based on their email address customersTable.RowFilter = string.Format("CustomerEmail = '{0}'", txtEmail.Text); DataRowView row = customersTable[0]; //creates and populates ProjectCustomer variable ProjectCustomer p = new ProjectCustomer(); p.CustomerID = Convert.ToInt32(row["CustomerID"]); p.CustomerFname = row["CustomerFName"].ToString(); p.CustomerLName = row["CustomerLName"].ToString(); p.CustomerStreetAddress = row["CustomerStreetAddress"].ToString(); p.CustomerCity = row["CustomerCity"].ToString(); p.CustomerState = row["CustomerState"].ToString(); p.CustomerZipCode = row["CustomerZipCode"].ToString(); p.CustomerPhone = row["CustomerPhone"].ToString(); return(p); }
/// <summary> /// the submit button saves the new or modified customer data to the database /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnSubmit_Click(object sender, EventArgs e) { string strConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MMABooksConnectionString2"].ConnectionString; SqlConnection con = new SqlConnection(); con.ConnectionString = strConnectionString; SqlCommand command; if ((bool)Session["InsertNewCustomer"]) { con.Open(); //ADD customer to CustomerTable string strInsertCustomer = "INSERT INTO dbo.CustomerTable (CustomerFname,CustomerLName,CustomerStreetAddress,CustomerCity,CustomerState,CustomerZipCode,CustomerPhone,CustomerEmail) VALUES (@CustomerFirst,@CustomerLast,@Address, @City,@State,@ZipCode,@PhoneNumber,@Email)"; command = new SqlCommand(strInsertCustomer, con); command.Parameters.AddWithValue("@CustomerFirst", txtCustomerFirst.Text); command.Parameters.AddWithValue("@CustomerLast", txtCustomerLast.Text); command.Parameters.AddWithValue("@Address", txtCustomerAddress.Text); command.Parameters.AddWithValue("@City", txtCustomerCity.Text); command.Parameters.AddWithValue("@State", ddlState.SelectedItem.Text); command.Parameters.AddWithValue("@ZipCode", txtCustomerZip.Text); command.Parameters.AddWithValue("@PhoneNumber", txtCustomerPhone.Text); command.Parameters.AddWithValue("@Email", txtEmail.Text); command.ExecuteNonQuery(); con.Close(); GridView1.DataSourceID = SqlDataSource1.ID; txtEmail.Text = ""; txtCustomerFirst.Text = ""; txtCustomerLast.Text = ""; txtCustomerAddress.Text = ""; txtCustomerCity.Text = ""; ddlState.SelectedIndex = 0; txtCustomerZip.Text = ""; txtCustomerPhone.Text = ""; addmodifycustomers.Attributes["class"] = "hidden"; txtEmail.DataBind(); } //if the customer record already exists this will update thier information else { ProjectCustomer existing = (ProjectCustomer)Session["LoadedCustomer"]; con.Open(); //UPDATE customer in CustomerTable string strUpdateCustomer = "UPDATE dbo.CustomerTable " + "SET CustomerFname = @CustomerFirst, " + "CustomerLName = @CustomerLast, " + "CustomerStreetAddress = @Address, " + "CustomerCity = @City, " + "CustomerState = @State, " + "CustomerZipCode = @ZipCode, " + "CustomerPhone = @PhoneNumber, " + "CustomerEmail = @Email " + "WHERE CustomerID = @ID;" ; command = new SqlCommand(strUpdateCustomer, con); command.Parameters.AddWithValue("@CustomerFirst", txtCustomerFirst.Text); command.Parameters.AddWithValue("@CustomerLast", txtCustomerLast.Text); command.Parameters.AddWithValue("@Address", txtCustomerAddress.Text); command.Parameters.AddWithValue("@City", txtCustomerCity.Text); command.Parameters.AddWithValue("@State", ddlState.SelectedItem.Value); command.Parameters.AddWithValue("@ZipCode", txtCustomerZip.Text); command.Parameters.AddWithValue("@PhoneNumber", txtCustomerPhone.Text); command.Parameters.AddWithValue("@Email", txtEmail.Text); command.Parameters.AddWithValue("@ID", existing.CustomerID); command.ExecuteNonQuery(); con.Close(); GridView1.DataBind(); } }
/// <summary> /// Confirms order. Adds new customers to DB. Adds order to DB. Prevents accidental duplicate orders. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnConfirmation_Click(object sender, EventArgs e) { string strConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MMABooksConnectionString2"].ConnectionString; SqlConnection con = new SqlConnection(); con.ConnectionString = strConnectionString; SqlCommand command; //for new customers if ((bool)Session["IsExistingCustomer"] == false) { con.Open(); //ADD customer to CustomerTable string strInsertCustomer = "INSERT INTO dbo.CustomerTable (CustomerFname,CustomerLName,CustomerStreetAddress,CustomerCity,CustomerState,CustomerZipCode,CustomerPhone,CustomerEmail) VALUES (@CustomerFirst,@CustomerLast,@Address, @City,@State,@ZipCode,@PhoneNumber,@Email)"; command = new SqlCommand(strInsertCustomer, con); command.Parameters.AddWithValue("@CustomerFirst", txtFirstName.Text); command.Parameters.AddWithValue("@CustomerLast", txtLastName.Text); command.Parameters.AddWithValue("@Address", txtAddress.Text); command.Parameters.AddWithValue("@City", txtCity.Text); command.Parameters.AddWithValue("@State", ddlState.SelectedItem.Text); command.Parameters.AddWithValue("@ZipCode", txtZip.Text); command.Parameters.AddWithValue("@PhoneNumber", txtPhoneNumber.Text); command.Parameters.AddWithValue("@Email", txtEmail.Text); command.ExecuteNonQuery(); con.Close(); Session["ExistingCustomer"] = GetSelectedRecord(); } //import selected ColorId and SizeID int intColorID = Convert.ToInt32(Session["SelectedColorID"]); int intSizeID = Convert.ToInt32(Session["SelectedSizeID"]); ProjectCustomer orderer = (ProjectCustomer)Session["ExistingCustomer"]; con.Open(); string strInsertOrder = ""; //insert user to TeamMembersTable if they are on a team if (lblTeamName.Text != "None") { int intTeamID = Convert.ToInt32(Session["SelectedTeamID"]); strInsertOrder = "INSERT INTO dbo.OrderTable (CustomerID,ColorID,SizeID,JerseyName,JerseyNumber,TeamID) " + "VALUES (@CustomerID,@ColorID,@SizeID,@JerseyName,@JerseyNumber,@TeamID);" + "INSERT INTO dbo.TeamMembersTable (TeamID,CustomerID) " + "VALUES (@TeamID,@CustomerID);"; command = new SqlCommand(strInsertOrder, con); command.Parameters.AddWithValue("@TeamID", intTeamID); } //just insert into order table else { strInsertOrder = "INSERT INTO dbo.OrderTable (CustomerID,ColorID,SizeID,JerseyName,JerseyNumber) " + "VALUES (@CustomerID,@ColorID,@SizeID,@JerseyName,@JerseyNumber)"; command = new SqlCommand(strInsertOrder, con); } command.Parameters.AddWithValue("@CustomerID", orderer.CustomerID); command.Parameters.AddWithValue("@ColorID", intColorID); command.Parameters.AddWithValue("@SizeID", intSizeID); command.Parameters.AddWithValue("@JerseyName", lblJerseyName.Text); command.Parameters.AddWithValue("@JerseyNumber", Convert.ToInt32(lblJerseyNumber.Text)); command.ExecuteNonQuery(); con.Close(); lblConfirmationMessage.Text = "Your order has been confirmed!"; }