private void onSubmit_Click(object sender, EventArgs e) { //Initialize variables TrackingEntry newEntry = new TrackingEntry(); TrackingProcessing newProcess = new TrackingProcessing(connection); //Parse and validate input if (!ParseInput(ref newEntry)) { //Show error message MessageBox.Show("Could not add entry to the repository!\nPlease check all fields for errors.", "Error Message:", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { //Add entry to repository newProcess.addTrackingEntry(newEntry); //Show success message MessageBox.Show("Entry successfully added to the repository!", "Entry Added", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
//Class methods //Import from selected file into database public static void Import(string fileName, MySqlConnection connection) { //Initialize variables int importCount = 0; //Number of entries attempted for import string entryString = ""; //Next entry string to be imported string importedEntries = ""; //Line number of each imported entry string failedEntries = ""; //Line number of each failed entry List<int> entriesImported = new List<int>(); //List of imported entry numbers List<int> entriesFailed = new List<int>(); //List of failed entry numbers StreamReader fileStream = new StreamReader(fileName); //Reader for getting input TrackingEntry newEntry = new TrackingEntry(connection); //Entry to be added to the database TrackingProcessing newProcess = new TrackingProcessing(connection); //Processing class for database queries //Validate the file sequence number, header & trailer, and the number of entries in the file if(ValidateFile(fileStream)) { //Move to the first entry string after validating file GetHeader(entryString, fileStream); //Get the next line of the file entryString = fileStream.ReadLine(); //Iterate through entries until the trailer is reached while (!entryString.StartsWith("TR ")) { //Parse the new entry, and validate data if(newEntry.Parse(entryString) != false) { Console.WriteLine(newEntry.tagNumber); //Add entry to repository newProcess.addTrackingEntry(newEntry); importCount++; entryString = fileStream.ReadLine(); //Store successfully imported entries entriesImported.Add(importCount); } //Error return if one or more fields are invalid else { importCount++; entryString = fileStream.ReadLine(); //Store unsuccessfully imported entries entriesFailed.Add(importCount); } }//while //Build the imported and failed string of entries for(int i = 0; i < entriesImported.Count; i++) { importedEntries += Convert.ToString(entriesImported[i]) + ", "; }//for for(int i = 0; i < entriesFailed.Count; i++) { failedEntries += Convert.ToString(entriesFailed[i]) + ", "; }//for //Show success message MessageBox.Show("Successfully imported file: " + fileName + "\nThe next file in sequence is " + String.Format("{0:0000}", (seqNum + 1)) + ".\nImported entries #: " + importedEntries + "\nFailed entries #: " + failedEntries,"Import Succeeded", MessageBoxButtons.OK, MessageBoxIcon.Information); //Log a success and close the file Log.Write("Successfully imported file: " + fileName); fileStream.Close(); } //Show error box if file cannot be validated else { //Show error message MessageBox.Show("Could not import file!\nThis is due to an invalid header or trailer line.", "Error Message:", MessageBoxButtons.OK, MessageBoxIcon.Warning); //Log a failure and close the file Log.Write("Failed to import file: " + fileName); fileStream.Close(); } }