Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 13th, 2012  |  syntax: None  |  size: 9.33 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. linq to sql c#: SubmitChanges() not updating the database
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Data.Linq;
  9. using System.Text;
  10. using System.Windows.Forms;
  11.  
  12. namespace Coke_Hold_Database
  13. {
  14.     public partial class frmFGHold : Form
  15.     {
  16.         private PINAuthentication cert = new PINAuthentication();
  17.         linqCokeDBDataContext db = new linqCokeDBDataContext();
  18.         Record_HoldData thisHold;
  19.         bool isNew; //flag if hold is new or not
  20.  
  21.  
  22.         public frmFGHold()
  23.         {
  24.             //constructor for new hold
  25.             InitializeComponent();
  26.             populateLists();
  27.             thisHold = new Record_HoldData();
  28.  
  29.             isNew = true;
  30.             btnEdit.Visible = false;
  31.         }
  32.  
  33.         public frmFGHold(Record_HoldData holdData)
  34.         {
  35.             //constructor for existing hold (edit)
  36.             InitializeComponent();
  37.             populateLists();
  38.             this.thisHold = holdData;
  39.  
  40.             //fill out the form
  41.             FillForm();
  42.  
  43.             isNew = false;
  44.             btnEdit.Visible = true;
  45.         }
  46.  
  47.         private void FillForm()
  48.         {
  49.             //fill out the form with existing information
  50.  
  51.             txtPONumber.Text = thisHold.PO_.ToString();
  52.             cboProductionSupervisor.SelectedValue = thisHold.ProductionSupervisor;
  53.             cboLineNumber.SelectedValue = thisHold.LineNumber;
  54.             cboQASupervisor.SelectedValue = thisHold.QASupervisor;
  55.             cboFlavorName.SelectedValue = thisHold.Flavor;
  56.             cboContainer.Text = thisHold.ContainerType;
  57.             cboContainerSize.Text = thisHold.ProductSize;
  58.             cboPackage.Text = thisHold.Package;
  59.             txtQuantity.Text = thisHold.HoldQty.ToString();
  60.             mtbDateCode.Text = thisHold.DateCode;
  61.             cboDefectiveComponent.Text = thisHold.NonConformingItem;
  62.             cboDefectReason.Text = thisHold.NonConformance;
  63.             cboOccuredAt.Text = thisHold.FoundDuring;
  64.             chkTestRequired.Checked = (bool) thisHold.TestRequired;
  65.             txaDescription.Text = thisHold.Comments;
  66.             txaRootCauseAnalysis.Text = thisHold.RootCause;
  67.         }
  68.  
  69.         private void UpdateHoldObject()
  70.         {
  71.             thisHold.PO_ = int.Parse(txtPONumber.Text);
  72.             thisHold.ProductionSupervisor = (int)cboProductionSupervisor.SelectedValue;
  73.             thisHold.LineNumber = (int) cboLineNumber.SelectedValue;
  74.             thisHold.QASupervisor = (int) cboQASupervisor.SelectedValue;
  75.             thisHold.Flavor = (int) cboFlavorName.SelectedValue;
  76.             thisHold.ContainerType = cboContainer.Text;
  77.             thisHold.ProductSize = cboContainerSize.Text;
  78.             thisHold.Package = cboPackage.Text;
  79.             thisHold.HoldQty = int.Parse(txtQuantity.Text);
  80.             thisHold.DateCode = mtbDateCode.Text;
  81.             thisHold.NonConformingItem = cboDefectiveComponent.Text;
  82.             thisHold.NonConformance = cboDefectReason.Text;
  83.             thisHold.FoundDuring = cboOccuredAt.Text;
  84.             thisHold.TestRequired = chkTestRequired.Checked;
  85.             thisHold.Comments = txaDescription.Text;
  86.             thisHold.RootCause = txaRootCauseAnalysis.Text;
  87.         }
  88.  
  89.         private void CreateNewHold()
  90.         {
  91.             db.Record_HoldDatas.InsertOnSubmit(thisHold);
  92.             //GenerateHoldNumber()
  93.             db.SubmitChanges();
  94.             MessageBox.Show(this, "Hold submitted!nnYou're hold number is " + thisHold.HoldID.ToString(),"Hold #" + thisHold.HoldID.ToString() + " created", MessageBoxButtons.OK, MessageBoxIcon.Information);
  95.             this.Close();
  96.         }
  97.  
  98.         private void UpdateHold()
  99.         {
  100.             db.SubmitChanges();
  101.             MessageBox.Show(this, "Hold #" + thisHold.HoldID.ToString() + " updated.", thisHold.HoldID.ToString() + " updated", MessageBoxButtons.OK, MessageBoxIcon.Information);
  102.             this.Close();  
  103.         }
  104.  
  105.         private bool ValidateEntry()
  106.         {
  107.             //TODO write some frakkin validation
  108.             return true;
  109.         }
  110.  
  111.         private void GetPIN()
  112.         {
  113.             frmPINPrompt pin = new frmPINPrompt(cert);
  114.             pin.ShowDialog();
  115.         }
  116.  
  117.         private void SubmitData()
  118.         {
  119.             UpdateHoldObject();
  120.             GetPIN();
  121.  
  122.             if (ValidateEntry() && cert.authenticated)
  123.             {
  124.                 try
  125.                 {
  126.                     if (isNew)
  127.                     {
  128.                         thisHold.LabTech = cert.empNumber;
  129.                         thisHold.LastEditBy = cert.empNumber;
  130.                         thisHold.HoldStatus = "Open";
  131.                         thisHold.DateOpened = DateTime.Now;
  132.                         thisHold.LastEditDate = DateTime.Now;
  133.  
  134.                         CreateNewHold();
  135.                     }
  136.                     else
  137.                     {
  138.                         thisHold.LastEditBy = cert.empNumber;
  139.                         thisHold.LastEditDate = DateTime.Now;
  140.  
  141.                         UpdateHold();
  142.                     }
  143.                 }
  144.                 catch (Exception ex)
  145.                 {
  146.                     MessageBox.Show(ex.Message);
  147.                 }
  148.             }
  149.         }
  150.  
  151.         public void populateLists()
  152.         {
  153.             //load production names
  154.             var productionNames =
  155.                 from a in db.LUT_Employees
  156.                 where a.position == "Supervisor" && a.department == "Production"
  157.                 select new { ID = a.ID, Names = a.lastName + ", " + a.firstName };
  158.  
  159.             cboProductionSupervisor.DataSource = productionNames;
  160.             cboProductionSupervisor.DisplayMember = "Names";
  161.             cboProductionSupervisor.ValueMember = "ID";
  162.  
  163.             //load QA names
  164.             var qaNames =
  165.                 from a in db.LUT_Employees
  166.                 where a.position == "Supervisor" && a.department == "Quality Assurance"
  167.                 select new { ID = a.ID, Names = a.lastName + ", " + a.firstName };
  168.  
  169.             cboQASupervisor.DataSource = qaNames;
  170.             cboQASupervisor.DisplayMember = "Names";
  171.             cboQASupervisor.ValueMember = "ID";
  172.  
  173.             //load flavor names
  174.             var flavorNames =
  175.                 from a in db.LUT_Flavors
  176.                 select new { ID = a.ID, Flavor = a.flavor };
  177.  
  178.             cboFlavorName.DataSource = flavorNames;
  179.             cboFlavorName.DisplayMember = "flavor";
  180.             cboFlavorName.ValueMember = "ID";
  181.  
  182.             //load line numbers
  183.             var lineNumbers =
  184.                 (from a in db.LUT_ProductionLines
  185.                 select new { a.lineNumber }).ToList();
  186.  
  187.             cboLineNumber.DataSource = lineNumbers;
  188.             cboLineNumber.DisplayMember = "LineNumber";
  189.             cboLineNumber.ValueMember = "LineNumber";
  190.         }
  191.  
  192.         private void mtbDateCode_MouseUp(object sender, MouseEventArgs e)
  193.         {
  194.             SendKeys.Send("{HOME}");
  195.         }
  196.  
  197.         private void btnCancel_Click(object sender, EventArgs e)
  198.         {
  199.             DialogResult result = MessageBox.Show("Are you sure you want to cancel? All data will be lost!", "Confirm Cancel", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
  200.             if (result == DialogResult.Yes)
  201.             {
  202.                 this.Close();
  203.             }
  204.             else { }
  205.         }
  206.  
  207.  
  208.  
  209.  
  210.  
  211.         private void btnSubmit_Click(object sender, EventArgs e)
  212.         {
  213.             SubmitData();
  214.  
  215.         }
  216.  
  217.         private void linkAutoFill_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  218.         {
  219.             //autogenerage date code
  220.             mtbDateCode.Text = Etc.BuildDateCode(DateTime.Now,(int)cboFlavorName.SelectedValue,cboContainer.Text,Etc.getDayCode(DateTime.Now),int.Parse(cboLineNumber.Text));
  221.         }
  222.  
  223.         private void cboContainerSize_SelectedIndexChanged(object sender, EventArgs e)
  224.         {
  225.             //load package sizes
  226.             var packageSizes =
  227.                 from a in db.LUT_Packagings
  228.                 where a.Container == cboContainer.Text && a.Size == cboContainerSize.SelectedValue
  229.                 select new { ID = a.ID, Size = a.Package };
  230.  
  231.             cboPackage.DataSource = packageSizes;
  232.             cboPackage.DisplayMember = "Size";
  233.             cboPackage.ValueMember = "ID";
  234.  
  235.         }
  236.  
  237.         private void cboContainer_SelectedIndexChanged(object sender, EventArgs e)
  238.         {
  239.             //make container size list
  240.             var containerSizes =
  241.                 from a in db.LUT_Containers
  242.                 where a.ContainerType == cboContainer.Text
  243.                 select new { ID = a.ID, Size = a.size };
  244.  
  245.             cboContainerSize.DataSource = containerSizes;
  246.             cboContainerSize.DisplayMember = "Size";
  247.             cboContainerSize.ValueMember = "ID";
  248.  
  249.             //load components
  250.             var defectiveComponents =
  251.                 from a in db.LUT_ContainerComponents
  252.                 where a.Container == cboContainer.Text
  253.                 select new { ID = a.ID, Comp = a.Component };
  254.  
  255.             cboDefectiveComponent.DataSource = defectiveComponents;
  256.             cboDefectiveComponent.DisplayMember = "Comp";
  257.             cboDefectiveComponent.ValueMember = "ID";
  258.         }
  259.  
  260.         private void btnEdit_Click(object sender, EventArgs e)
  261.         {
  262.             //TODO ask verify PIN
  263.             foreach (Control c in this.Controls)
  264.             {
  265.                 c.Enabled = true;
  266.             }
  267.  
  268.             btnSubmit.Text = "Update Hold";
  269.         }
  270.     }
  271. }