/// <summary> /// Fetches values from the form-fields and populates the customerDetail object. /// </summary> /// <param name="customerDetail">The CustomerDetail object into which the form-field-values are filled.</param> private void fetchFormValues(CustomerDetail customerDetail) { customerDetail.EmailId = textBox_EmailId.Text; customerDetail.CustomerName = textBox_firstName.Text; customerDetail.LastName = textBox_lastName.Text; customerDetail.Address = textBox_Address.Text; int depositAmount = 0; Int32.TryParse(textBox_Deposit.Text.Trim(), out depositAmount); customerDetail.PhoneNumber = textBox_PhoneNumber.Text; customerDetail.Deposit = depositAmount; }
/// <summary> /// Submit button is clicked. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button_SubmitAddNewUser_Click_1(object sender, EventArgs e) { if(validate()) return; //Validation fails. //Validation successful. //Create a new CustomerDetail object, fetch form-field-values and insert into the DB. CustomerDetail customerDetail = new CustomerDetail(); int lastInsertedCustomerId = db.CustomerDetails.Count(); customerDetail.CustomerId = lastInsertedCustomerId + 1; //New Customer's id. fetchFormValues(customerDetail); bool exceptionOccured = false; try { try { db.CustomerDetails.Add(customerDetail); db.SaveChanges(); } catch (Exception ex) { } } catch (Exception ex) { Console.WriteLine(ex.StackTrace); exceptionOccured = true; } if (!exceptionOccured) { MessageBox.Show("User added successfully.", "Success"); } else { MessageBox.Show("Something went wrong.", "Error"); } }