//Search function private void Search() { HOTLOADEntity = new HLDB2Entities(); List <Load> matchedLoads = null; matchedLoads = HOTLOADEntity.Loads.Where( x => x.bol_num.ToString().Contains(bolSearch_txt.Text) && x.pro_num.ToString().Contains(proSearch_txt.Text) && x.quote_num.ToString().Contains(quoteSearch_txt.Text) && x.ref_num.ToString().Contains(refSearch_txt.Text) && //Date search terms ( (pickDateStart_dtpckr.SelectedDate == null || x.pick_date.Value >= pickDateStart_dtpckr.SelectedDate) && (pickDateEnd_dtpckr.SelectedDate == null || x.pick_date.Value <= pickDateEnd_dtpckr.SelectedDate) ) && ( (dropDateStart_dtpckr.SelectedDate == null || x.drop_appointment.Value >= dropDateStart_dtpckr.SelectedDate) && (dropDateEnd_dtpckr.SelectedDate == null || x.drop_appointment.Value <= dropDateEnd_dtpckr.SelectedDate) ) ).ToList(); LoadBoard.ItemsSource = matchedLoads; }
//Fill textboxes with selected values private void LoadBoard_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e) { if (LoadBoard.SelectedIndex != -1) { //Load model Load loadModel = (Load)LoadBoard.SelectedItem; using (HLDB2Entities HOTLOADEntity = new HLDB2Entities()) { loadModel = HOTLOADEntity.Loads.Where(x => x.bol_num == loadModel.bol_num).FirstOrDefault(); bol_txt.Text = loadModel.bol_num.ToString(); pro_txt.Text = loadModel.pro_num.ToString(); quote_txt.Text = loadModel.quote_num.ToString(); ref_txt.Text = loadModel.ref_num.ToString(); weight_txt.Text = loadModel.weight.ToString(); pieces_txt.Text = loadModel.pieces.ToString(); commodity_txt.Text = loadModel.commodity.ToString(); mileage_txt.Text = loadModel.mileage.ToString(); carrierRate_txt.Text = loadModel.carrier_rate.ToString(); customerRate_txt.Text = loadModel.customer_rate.ToString(); //Dates & Times pickDate_picker.Text = loadModel.pick_date.ToString(); //pickAptTime_txt.Text = TimeStringBuilder(loadModel.pick_date.Value); dropDate_picker.Text = loadModel.drop_appointment.ToString(); //dropAptTime_txt.Text = TimeStringBuilder(loadModel.drop_appointment.Value); driver_txt.Text = loadModel.driver_id.ToString(); dispatch_txt.Text = loadModel.dispatch_id.ToString(); customer_txt.Text = loadModel.customer_id.ToString(); broker_txt.Text = loadModel.broker_id.ToString(); } //Change Update/New button text update_btn.Content = "Update Load"; //Enable copy & delete buttons delete_btn.IsEnabled = true; copy_btn.IsEnabled = true; } }
private void delete_btn_Click(object sender, RoutedEventArgs e) { //Get the selected load from Datagrid Load loadModel = (Load)LoadBoard.SelectedItem; if (MessageBox.Show("Are you sure you want to delete this record?", "Confirm Deletion", MessageBoxButton.YesNo) == MessageBoxResult.Yes) { using (HLDB2Entities HOTLOADEntity = new HLDB2Entities()) { var entry = HOTLOADEntity.Entry(loadModel); if (entry.State == EntityState.Detached) { HOTLOADEntity.Loads.Attach(loadModel); } //Remove the selected load from database HOTLOADEntity.Loads.Remove(loadModel); HOTLOADEntity.SaveChanges(); PopulateGrid(); Clear(); MessageBox.Show("Deleted Load #" + loadModel.bol_num + " succesfully.", "Load Deleted", MessageBoxButton.OK); } } }
//Update or Create Button private void update_btn_Click(object sender, RoutedEventArgs e) { //Value Setting process try { //Load model Load loadModel = new Load(); //Get the load data from textboxes loadModel.bol_num = Convert.ToInt32(bol_txt.Text.Trim()); loadModel.pro_num = pro_txt.Text.Trim(); loadModel.quote_num = quote_txt.Text.Trim(); loadModel.ref_num = ref_txt.Text.Trim(); loadModel.weight = Convert.ToDouble(weight_txt.Text.Trim()); try { loadModel.pieces = Convert.ToInt32(pieces_txt.Text.Trim()); } catch (System.FormatException ex) { MessageBox.Show(ex.Message, "Invalid Pieces Entry", MessageBoxButton.OK, MessageBoxImage.Error); } loadModel.commodity = commodity_txt.Text.Trim(); loadModel.mileage = Convert.ToDouble(mileage_txt.Text.Trim()); loadModel.carrier_rate = Convert.ToDecimal(carrierRate_txt.Text.Trim()); loadModel.customer_rate = Convert.ToDecimal(customerRate_txt.Text.Trim()); //Pick Date setter loadModel.pick_date = pickDate_picker.SelectedDate.Value; //Pick Time setter //Drop Date setter loadModel.drop_appointment = dropDate_picker.SelectedDate.Value; //Drop Time setter //Last updated is now loadModel.last_updated_time = DateTime.Now; loadModel.driver_id = Convert.ToInt32(driver_txt.Text.Trim()); loadModel.dispatch_id = Convert.ToInt32(dispatch_txt.Text.Trim()); loadModel.customer_id = Convert.ToInt32(customer_txt.Text.Trim()); loadModel.broker_id = Convert.ToInt32(broker_txt.Text.Trim()); //Save the load to the database using (HLDB2Entities HOTLOADEntity = new HLDB2Entities()) { if (loadModel.bol_num == 0)//Insert { HOTLOADEntity.Loads.Add(loadModel); Clear(); MessageBox.Show("Saved Succesfully"); } else//Update { HOTLOADEntity.Entry(loadModel).State = EntityState.Modified; Clear(); } //Save the changes HOTLOADEntity.SaveChanges(); Search(); } } //Entity Model Exception handler catch (DbEntityValidationException dbEx) { foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { Trace.TraceInformation( "Class: {0}, Property: {1}, Error: {2}", validationErrors.Entry.Entity.GetType().FullName, validationError.PropertyName, validationError.ErrorMessage); } } } }
//Fill datagrid from DB public void PopulateGrid() { HOTLOADEntity = new HLDB2Entities(); LoadBoard.ItemsSource = HOTLOADEntity.Loads.ToList(); }