public void AddCustomerPoints(CustomerPointsEntry entry) { // create sql connection object. Be sure to put a valid connection string SqlConnection cnn = new SqlConnection(ConnectionString); // create command object with SQL query and link to connection object SqlCommand cmd = new SqlCommand( "exec sp_AddPoints @phoneNumber, @pointsAdded, @dateOfEntry, @entryType,@pointsRedeemed", cnn ); cmd.Parameters.AddWithValue("@phoneNumber", entry.PhoneNumber); cmd.Parameters.AddWithValue("@pointsAdded", entry.PointsAdded); cmd.Parameters.AddWithValue("@dateOfEntry", entry.EntryDate); cmd.Parameters.AddWithValue("@entryType", entry.EntryType); cmd.Parameters.AddWithValue("@pointsRedeemed", entry.PointsRedeemed); // open sql connection cnn.Open(); // execute the query and return number of rows affected, should be one //Catch SQL Errors int RowsAffected = cmd.ExecuteNonQuery(); // close connection when done cnn.Close(); }
private void addCustomerPointsFormAddPointsButton_Click(object sender, EventArgs e) { try { var entry = new CustomerPointsEntry(); entry.EntryDate = Convert.ToDateTime(addCustomerPointsFormDateOfVisitPicker.Text); entry.EntryType = "add"; entry.PhoneNumber = addCustomerPointsFormPhoneTextBox.Text; entry.PointsAdded = Convert.ToSingle(addCustomerPointsFormPointsTextBox.Text); entry.PointsRedeemed = Convert.ToSingle(0); var customerData = new CustomerData(); customerData.AddCustomerPoints(entry); MessageBox.Show("Points Added Successfully"); this.Close(); } //catch (SqlException f) when (f.Number == 2627) //{ // MessageBox.Show("That Phone Number Already Exists in the Database. Please Enter another number or use existing customer entry form."); //} //catch (SqlException f) when (f.Number == 8152) //{ // MessageBox.Show("One of your fields is too long, fix it."); //} //finally //{ //} catch (Exception ex) { MessageBox.Show($"An error occurred with message: {ex.Message}"); } }