//Creating a List with TaxRules from CSV File public static void Importer(string _filepath) { var taxRuleList = new List <TaxRule>(); string csvLine; using (var sr = new StreamReader(_filepath)) { int line = 0; // Read the stream as a string, and write the string to the console. while ((csvLine = sr.ReadLine()) != null) { line++; TaxRule taxrule = ReadTaxRuleFromCSVLine(csvLine, line); if (!taxrule.Error)//Checks if TaxRule has any Errors before attempting to add it to the List { taxRuleList.Add(taxrule); } } } bool Continue = true; foreach (TaxRule _taxrule in taxRuleList) { if (!_taxrule.Error) //Makes absolutely sure there are no errors before attempting to add the TaxRule { if (!(Continue = AddTaxToDb.AddTax(_taxrule.StartDate, _taxrule.Type, _taxrule.TaxRate, _taxrule.MunicipalityId, _taxrule.MunicipalityName))) //Adds info to DB and break out of foreach loop if method returns false eg. if user selects Cancel in a dublicate warning { break; } } } }
private void btnAddTax_Click(object sender, EventArgs e) //Add Tax Button { string SelectedTaxType = lstTaxType.SelectedItem.ToString(); //Sets selected tax type from list string taxType = SelectedTaxType.First().ToString(); //Selects only First letter to make it combatible with DB DateTime selectedStartDate = dateTimePickerAddTax.Value.Date; //Finds selected date string taxRate = txtBoxAddNewTax.Text; //Finds entered taxRate int municipalityId = int.Parse(lstMunicipalities.SelectedValue.ToString()); //Gets the Id of the selected municipality string municipalityName = lstMunicipalities.Text; if (ReplaceDublicate.SelectedValue == false) { MessageBox.Show("Can't add the new tax\n'Replace existing' is set to: 'Ignore All'\nChange to 'Replace All' or 'Always Ask'", "Ignore All Selected", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (Validater.TaxRate(taxRate) && ReplaceDublicate.SelectedValue != false) //Validates correct TaxRate { AddTaxToDb.AddTax(selectedStartDate, taxType, taxRate, municipalityId, municipalityName); //Sends info to AddTax method } taxsearch(); //Refreshes TaxSearcher }