Advertisement
Guest User

Untitled

a guest
Nov 25th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.89 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Data.Linq;
  6. using System.Data.SqlClient;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12.  
  13. namespace Sprawko2_DB
  14. {
  15.     public partial class DB2 : Form
  16.     {
  17.         public DB2()
  18.         {
  19.             InitializeComponent();
  20.         }
  21.  
  22.  
  23.         #region GLOBALVARS
  24.         int ActiveButton = 0;
  25.  
  26.         static LinqDataContext dc = new LinqDataContext();
  27.         Table<Pracownicy> table = dc.GetTable<Pracownicy>();
  28.         #endregion
  29.  
  30.         #region BASICFUNCs
  31.  
  32.         private void DisableButtons(bool onoff)
  33.         {
  34.             foreach (Control x in WorkersGroupBox.Controls)
  35.             {
  36.                 if (x is TextBox)
  37.                 {
  38.                     x.Enabled = onoff;
  39.                 }
  40.  
  41.             }
  42.  
  43.             StateBox.Enabled = onoff;
  44.         }
  45.  
  46.         void Backlight(int temp, bool type)
  47.         {        
  48.             int id;
  49.             if (type)
  50.                 id = ReadID(temp);
  51.             else
  52.                 id = temp;
  53.  
  54.             if (id > 0)
  55.             {
  56.                 DGV.Rows[id].Selected = true;
  57.                 DGV.CurrentCell = DGV.Rows[id].Cells[0];
  58.             }
  59.            
  60.         }
  61.  
  62.         int ReadID(int id)
  63.         {
  64.             int resultId = -1;
  65.  
  66.             for (int i = 0; i < DGV.Rows.Count - 1; i++)
  67.             {
  68.                 if (Convert.ToInt32(DGV.Rows[i].Cells[0].Value.ToString()) == id)
  69.                     resultId = i;
  70.             }
  71.  
  72.             return resultId;
  73.         }
  74.  
  75.         void WriteToBoxes(int id)
  76.         {
  77.             StateBox.Text = DGV.Rows[id].Cells[1].Value.ToString();
  78.             NameBox.Text = DGV.Rows[id].Cells[2].Value.ToString();
  79.             SurnameBox.Text = DGV.Rows[id].Cells[3].Value.ToString();
  80.             AddressBox.Text = DGV.Rows[id].Cells[4].Value.ToString();
  81.             PhoneBox.Text = DGV.Rows[id].Cells[5].Value.ToString();
  82.         }
  83.  
  84.         void ClearButtons()
  85.         {
  86.             AddButton.BackColor = Color.White;
  87.             EditButton.BackColor = Color.White;
  88.             DeleteButton.BackColor = Color.White;
  89.  
  90.         }
  91.  
  92.         void ClearBoxes()
  93.         {
  94.             foreach (Control x in WorkersGroupBox.Controls)
  95.             {
  96.                 if (x is TextBox)
  97.                 {
  98.                     x.Text = String.Empty;
  99.                 }
  100.  
  101.             }
  102.  
  103.             StateBox.Text = String.Empty;
  104.         }
  105.  
  106.         bool CheckState()
  107.         {
  108.             bool result = false;
  109.  
  110.             for (int i = 0; i < StateBox.Items.Count; i++)
  111.                 if (StateBox.Text == StateBox.GetItemText(StateBox.Items[i]))
  112.                     result = true;
  113.  
  114.             return result;
  115.         }
  116.         #endregion
  117.  
  118.         #region CRUDFUNCs
  119.         bool AddWorker()
  120.         {
  121.        
  122.             string[] worker = new string[5];
  123.  
  124.             try
  125.             {
  126.                 worker[0] = StateBox.Text;
  127.                 worker[1] = NameBox.Text;
  128.                 worker[2] = SurnameBox.Text;
  129.                 worker[3] = AddressBox.Text;
  130.                 worker[4] = PhoneBox.Text;
  131.  
  132.    
  133.                 if (StateBox.Text.Length == 0)
  134.                     worker[0] = null;
  135.  
  136.                 if (NameBox.Text.Length == 0)
  137.                     worker[1] = null;
  138.  
  139.                 if (SurnameBox.Text.Length == 0)
  140.                     worker[2] = null;
  141.  
  142.                 if (AddressBox.Text.Length == 0)
  143.                     worker[3] = null;
  144.  
  145.                 if (PhoneBox.Text.Length == 0)
  146.                     worker[4] = null;
  147.  
  148.                var prac = new Pracownicy
  149.                 {
  150.                     Stanowisko = worker[0],
  151.                     Imie = worker[1],
  152.                     Nazwisko = worker[2],
  153.                     Adres = worker[3],
  154.                     Telefon = worker[4]
  155.                 };
  156.  
  157.  
  158.                 dc.GetTable<Pracownicy>().InsertOnSubmit(prac);
  159.                 dc.GetTable<Pracownicy>().Context.SubmitChanges();
  160.                 return true;
  161.             }
  162.             catch (System.Data.SqlClient.SqlException ex)
  163.             {
  164.                 ExcepitonHandler(ex.Number);
  165.                 return false;
  166.             }
  167.  
  168.  
  169.  
  170.  
  171.  
  172.          
  173.  
  174.         }
  175.  
  176.         bool UpdateWorker(int ID)
  177.         {
  178.  
  179.             bool inside = false;
  180.             try
  181.             {
  182.            
  183.                 var query =
  184.                     from prac in dc.GetTable<Pracownicy>()
  185.                     where prac.IdPracownika == ID
  186.                     select prac;
  187.  
  188.                 foreach (Pracownicy worker in query)
  189.                 {
  190.                     inside = true;
  191.                                        
  192.                         if (NameBox.Text.Length > 0)
  193.                             worker.Imie = NameBox.Text;
  194.                         else
  195.                             worker.Imie = null;
  196.  
  197.                         if (SurnameBox.Text.Length > 0)
  198.                             worker.Nazwisko = SurnameBox.Text;
  199.                         else
  200.                             worker.Nazwisko = null;
  201.  
  202.                         if (AddressBox.Text.Length > 0)
  203.                             worker.Adres = AddressBox.Text;
  204.                         else
  205.                             worker.Adres = null;
  206.  
  207.                         if (PhoneBox.Text.Length > 0)
  208.                             worker.Telefon = PhoneBox.Text;
  209.                         else
  210.                             worker.Telefon = null;
  211.  
  212.                         if (StateBox.Text.Length > 0)
  213.                             worker.Stanowisko = StateBox.Text;
  214.                         else
  215.                             worker.Stanowisko = null;
  216.                    
  217.                  
  218.                 }
  219.                 if (!inside) throw new Exception("0");
  220.                 dc.GetTable<Pracownicy>().Context.SubmitChanges();
  221.                 return true;
  222.  
  223.             }
  224.             catch (SqlException ex)
  225.             {  
  226.                 ExcepitonHandler(ex.Number);
  227.                 return false;
  228.             }
  229.             catch (Exception ex)
  230.             {          
  231.                 ExcepitonHandler(Convert.ToInt32(ex.Message));
  232.                 return false;
  233.             }
  234.  
  235.          
  236.         }
  237.  
  238.         bool DeleteWorker(int ID)
  239.         {
  240.             try
  241.             {
  242.             var delpatfile = dc.GetTable<Pracownicy>().SingleOrDefault(p => p.IdPracownika == ID);
  243.                
  244.             dc.GetTable<Pracownicy>().DeleteOnSubmit(delpatfile);
  245.  
  246.            
  247.             dc.GetTable<Pracownicy>().Context.SubmitChanges();
  248.                 return true;
  249.             }
  250.             catch (Exception)
  251.             {
  252.                 ExcepitonHandler(0);
  253.                 return false;
  254.             }
  255.  
  256.         }
  257.  
  258.  
  259.         bool AddWorkerProc()
  260.         {
  261.             string[] worker = new string[5];
  262.  
  263.             try
  264.             {
  265.                 worker[0] = StateBox.Text;
  266.                 worker[1] = NameBox.Text;
  267.                 worker[2] = SurnameBox.Text;
  268.                 worker[3] = AddressBox.Text;
  269.                 worker[4] = PhoneBox.Text;
  270.  
  271.  
  272.                 if (StateBox.Text.Length == 0)
  273.                     worker[0] = null;
  274.  
  275.                 if (NameBox.Text.Length == 0)
  276.                     worker[1] = null;
  277.  
  278.                 if (SurnameBox.Text.Length == 0)
  279.                     worker[2] = null;
  280.  
  281.                 if (AddressBox.Text.Length == 0)
  282.                     worker[3] = null;
  283.  
  284.                 if (PhoneBox.Text.Length == 0)
  285.                     worker[4] = null;
  286.  
  287.                 dc.InsertPracownik(null, worker[0], worker[1], worker[2], worker[3], worker[5]);
  288.                 return true;
  289.             }
  290.             catch (SqlException ex)
  291.             {
  292.                 ExcepitonHandler(ex.Number);
  293.                 return false;
  294.             }
  295.  
  296.         }
  297.  
  298.         bool UpdateWorkerProc(int ID)
  299.         {
  300.             try
  301.             {
  302.                 //dc.UpdatePracownik(ID, StateBox.Text, NameBox.Text, SurnameBox.Text, AddressBox.Text, PhoneBox.Text);
  303.                 return true;
  304.             }
  305.             catch(SqlException ex)
  306.             {
  307.                 ExcepitonHandler(ex.Number);
  308.                 return false;
  309.             }
  310.            
  311.         }
  312.  
  313.         bool DeleteWorkerProc(int ID)
  314.         {
  315.             try
  316.             {
  317.                 //dc.DeletePracownik(ID);
  318.                 return true;
  319.             }
  320.             catch (SqlException ex)
  321.             {
  322.                 ExcepitonHandler(ex.Number);
  323.                 return false;
  324.             }
  325.         }
  326.  
  327.         void SelectTable()
  328.         {
  329.             dc = new LinqDataContext();
  330.             DGV.DataSource = dc.GetTable<Pracownicy>();
  331.             DGVV.DataSource = dc.GetTable<prac_adre>();
  332.  
  333.         }
  334.  
  335.         void ClearCounter()
  336.         {
  337.             dc.ExecuteCommand("DBCC CHECKIDENT('Pracownicy', RESEED, 0);");
  338.         }
  339.      
  340.         #endregion
  341.    
  342.  
  343.         void ApplyAction(bool linq)
  344.         {
  345.             if (linq)
  346.             {
  347.                 switch (ActiveButton)
  348.                 {
  349.                     case 0: { if (AddWorker()) { SelectTable(); Backlight(DGV.Rows.Count - 2, false); } break; }
  350.                     case 1: { if (UpdateWorker(Convert.ToInt32(IDBox.Text))) { SelectTable(); Backlight(Convert.ToInt32(IDBox.Text), true); } break; }
  351.                     case 2: { if (DeleteWorker(Convert.ToInt32(IDBox.Text))) SelectTable(); break; }
  352.                 }
  353.                 ClearBoxes();
  354.             }
  355.             else
  356.             {
  357.                 switch (ActiveButton)
  358.                 {
  359.                     case 0: { if (AddWorkerProc()) { SelectTable(); Backlight(DGV.Rows.Count - 2, false); } break; }
  360.                     case 1: { if (UpdateWorkerProc(Convert.ToInt32(IDBox.Text))) { SelectTable(); Backlight(Convert.ToInt32(IDBox.Text), true); } break; }
  361.                     case 2: { if (DeleteWorkerProc(Convert.ToInt32(IDBox.Text))) SelectTable(); break; }
  362.                 }
  363.                 ClearBoxes();
  364.             }
  365.  
  366.  
  367.  
  368.         }
  369.  
  370.         void ExcepitonHandler(int exid)
  371.         {
  372.             string errorText = String.Empty;
  373.             switch (exid)
  374.             {
  375.  
  376.                 case 515: { errorText = "Nieprawidłowe dane. Żadne pole nie może być puste. "; break; }
  377.                 case 547:
  378.                     {
  379.                         if (CheckState())
  380.                             errorText = "Nieprawidłowa długość pola Telefon";
  381.                         else
  382.                             errorText = "Nieprawidłowa zawartość pola Stanowisko";
  383.                         break;
  384.                     }
  385.                 case 0: { errorText = "Brak rekordu o podanym ID w bazie"; break; }
  386.                 case 5132: { errorText = ""; break; }
  387.                 case 5142: { errorText = ""; break; }
  388.                 case 5152: { errorText = ""; break; }
  389.                 case 5162: { errorText = ""; break; }
  390.  
  391.             }
  392.             MessageBox.Show(exid.ToString() + " :: " + errorText);
  393.         }
  394.  
  395.  
  396.  
  397.         #region EVENTS
  398.         private void DB2_Load(object sender, EventArgs e)
  399.         {
  400.             AddButton.PerformClick();
  401.             SelectTable();
  402.  
  403.         }
  404.  
  405.         private void AddButton_Click(object sender, EventArgs e)
  406.         {
  407.             DisableButtons(true);
  408.             //sClearCounter();
  409.             ActiveButton = 0;
  410.             ClearButtons();
  411.             AddButton.BackColor = Color.DodgerBlue;
  412.             IDBox.Text = String.Empty;
  413.             IDBox.Enabled = false;
  414.         }
  415.  
  416.         private void EditButton_Click(object sender, EventArgs e)
  417.         {
  418.             ClearBoxes();
  419.             DisableButtons(true);
  420.             IDBox.Enabled = true;
  421.             ActiveButton = 1;
  422.             ClearButtons();
  423.             EditButton.BackColor = Color.DodgerBlue;
  424.         }
  425.  
  426.         private void DeleteButton_Click(object sender, EventArgs e)
  427.         {
  428.             ClearBoxes();
  429.             DisableButtons(false);
  430.             StateBox.Enabled = false;
  431.             IDBox.Enabled = true;
  432.             ActiveButton = 2;
  433.             ClearButtons();
  434.             DeleteButton.BackColor = Color.DodgerBlue;
  435.         }  
  436.    
  437.         private void IDBox_TextChanged(object sender, EventArgs e)
  438.         {
  439.            
  440.             try
  441.             {
  442.                 Backlight(Convert.ToInt32(IDBox.Text),true);
  443.                 var idData = ReadID(Convert.ToInt32(IDBox.Text));
  444.                 WriteToBoxes(idData);
  445.                
  446.             }
  447.             catch
  448.             {
  449.  
  450.             }
  451.         }
  452.  
  453.         private void ApplyProcButton_Click(object sender, EventArgs e)
  454.         {
  455.             ApplyAction(false);
  456.         }
  457.  
  458.         private void ApplyButton_Click(object sender, EventArgs e)
  459.         {
  460.             ApplyAction(true);
  461.         }
  462.  
  463.         #endregion
  464.  
  465.  
  466.     }
  467.  
  468.  
  469. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement