/// <summary> /// Event handler for the Calculate button /// Will attempt to process a worker based on the /// selected radiobutton and then update the screen /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnCalculate_OnClick(object sender, EventArgs e) { Page.Validate(); if (Page.IsValid) // ensure our validation checks went through { Worker myWorker; // create an uninstantiated worker if (rdWorkerType.SelectedIndex == 0) // if our Piecework Worker radio button is selected { // do normal worker // myWorker = new PieceworkWorker(txtWorkerFirstNameEntry.Text, txtWorkerLastNameEntry.Text, txtMessagesEntry.Text); myWorker = new PieceworkWorker(txtWorkerNameEntry.Text, txtMessagesEntry.Text); } else if (rdWorkerType.SelectedIndex == 1) // if our Senior worker radio button is selected { // do senior worker // myWorker = new SeniorWorker(txtWorkerFirstNameEntry.Text, txtWorkerLastNameEntry.Text, txtMessagesEntry.Text); myWorker = new SeniorWorker(txtWorkerNameEntry.Text, txtMessagesEntry.Text); } else if (rdWorkerType.SelectedIndex == 2) // if our hourly worker radio button is selected { // do hourly worker myWorker = new HourlyWorker(txtWorkerNameEntry.Text, txtMessagesEntry.Text, txtHoursWorkedEntry.Text, txtHourlyPayEntry.Text); } else // in case something goes catastrophically wrong and our user has any other value { ArgumentNullException ex = new ArgumentNullException("worker", "Worker type was not selected."); throw ex; } // assume the data is valid and nothing went wrong // we've gone through client and then server side validation, // followed by class validation // increment our total data myWorker.UpdateTotals(); // add our new worker to the database myWorker.AddWorkerToDB(); // update our labels lblWorkerPayOutput.Text = myWorker.Pay.ToString("c"); DisplayPayOutputs(); // lock up our form and wait for the clear button to get pressed ToggleEnabledControls(); // focus the clear button to make the user's life easier btnClear.Focus(); lblOutNote.Text = myWorker.ToString() + "<br />This Worker has been successfully recorded."; } }
/// <summary> /// Returns all workers in the database as a list of worker objects /// </summary> /// <returns></returns> internal static List <Worker> GetEmployeeList() { // Declare the connection SqlConnection dbConnection = new SqlConnection(Conn.GetConnectionString()); // Create new SQL command, assign it prepared statement SqlCommand commandString = new SqlCommand(SQLStatements.SelectAll, dbConnection); SqlDataAdapter adapter = new SqlDataAdapter(commandString); // Declare a DataTable object that will hold the return value DataTable employeeTable = new DataTable(); // Declare a list of workers to store converted database pulls List <Worker> workerList = new List <Worker>(); // Try to connect to the database, and use the adapter to fill the table try { dbConnection.Open(); adapter.Fill(employeeTable); } catch (Exception ex) { // System.Windows.MessageBox.Show("A database error has been encountered: " + Environment.NewLine + ex.Message, "Database Error"); } finally { dbConnection.Close(); } // cycle through each row in our table foreach (DataRow item in employeeTable.Rows) { Worker thisWorker; // generic object creation // if tree to determine which worker type we want to create // uses enumerable comparisons in case things change later so we don't have // to worry about updating a bunch of static values everywhere if (Int32.Parse(item["workertype"].ToString()) == (int)IncIncEnumerables.WorkerTypes.Regular) { thisWorker = new PieceworkWorker( Int32.Parse(item["entryId"].ToString()), item["firstName"].ToString(), item["lastName"].ToString(), item["messages"].ToString(), Convert.ToDateTime(item["entryDate"]) ); } else if (Int32.Parse(item["workertype"].ToString()) == (int)IncIncEnumerables.WorkerTypes.Senior) { thisWorker = new SeniorWorker( Int32.Parse(item["entryId"].ToString()), item["firstName"].ToString(), item["lastName"].ToString(), item["messages"].ToString(), Convert.ToDateTime(item["entryDate"]) ); } else if (Int32.Parse(item["workertype"].ToString()) == (int)IncIncEnumerables.WorkerTypes.Hourly) { thisWorker = new HourlyWorker( Int32.Parse(item["entryId"].ToString()), item["firstName"].ToString(), item["lastName"].ToString(), item["messages"].ToString(), Convert.ToDateTime(item["entryDate"]), Int32.Parse(item["hoursWorked"].ToString()), Decimal.Parse(item["hourlyPay"].ToString()) ); } else // catch-all we should never see unless some wise guy tampered with the db { throw new ArgumentException("Non-existent worker type found in database."); } workerList.Add(thisWorker); } // Return an array of worker objects return(workerList);//.ToArray(); // Return the populated DataTable's DataView // return employeeTable; }