Guest User

Untitled

a guest
Jan 25th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 30.82 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using MySql.Data.MySqlClient;
  11.  
  12. namespace DEV3_5_Assignment1
  13. {
  14.     public partial class Assigment1 : Form
  15.     {
  16.         public Assigment1()
  17.         {
  18.             InitializeComponent();
  19.             LoadData();
  20.         }
  21.  
  22.         private void button1_Click(object sender, EventArgs e)
  23.         {
  24.             string ConnectionString = "Server=localhost;Database=mydb;Uid=root;Pwd=root";
  25.             MySqlConnection connection = new MySqlConnection(ConnectionString);
  26.             MySqlCommand cmd;
  27.             connection.Open();
  28.  
  29.             try
  30.             {
  31.                 cmd = connection.CreateCommand();
  32.  
  33.                 if (lbTempEmpID.Text != "")
  34.                 {  
  35.                     cmd.CommandText = "UPDATE employees SET bsn=@bsn, name=@name, surname=@surname WHERE employeeID=@idd";
  36.                     cmd.Parameters.AddWithValue("@idd", lbTempEmpID.Text);
  37.                     cmd.Parameters.AddWithValue("@bsn", int.Parse(tbAbsn.Text));
  38.                     cmd.Parameters.AddWithValue("@name", tbAfirstname.Text);
  39.                     cmd.Parameters.AddWithValue("@surname", tbAsurname.Text);
  40.  
  41.                     tbAbsn.Text = "";
  42.                     tbAfirstname.Text = "";
  43.                     tbAsurname.Text = "";
  44.  
  45.                     lbTempEmpID.Text = "";
  46.                 }
  47.                 else
  48.                 {
  49.                     cmd.CommandText = "INSERT INTO employees(bsn,name,surname)VALUES(@bsn,@name,@surname)";
  50.                     cmd.Parameters.AddWithValue("@bsn", int.Parse(tbAbsn.Text));
  51.                     cmd.Parameters.AddWithValue("@name", tbAfirstname.Text);
  52.                     cmd.Parameters.AddWithValue("@surname", tbAsurname.Text);
  53.  
  54.                     lbTempEmpID.Text = "";
  55.                 }
  56.  
  57.                 cmd.ExecuteNonQuery();
  58.             }
  59.             catch (Exception)
  60.             {
  61.                 throw;
  62.             }
  63.             finally
  64.             {
  65.                 if(connection.State == ConnectionState.Open)
  66.                 {
  67.                     connection.Close();
  68.                     LoadData();
  69.                 }
  70.             }
  71.         }
  72.  
  73.         private void LoadData() //Add visitor here
  74.         {
  75.             string ConnectionString = "Server=localhost;Database=mydb;Uid=root;Pwd=root";
  76.            
  77.             MySqlConnection connection = new MySqlConnection(ConnectionString);
  78.             connection.Open();
  79.             try
  80.             {
  81.                 MySqlCommand cmd = connection.CreateCommand();
  82.                 cmd.CommandText = "SELECT * FROM employees";
  83.                 MySqlDataAdapter adapt = new MySqlDataAdapter(cmd);
  84.                 DataSet ds = new DataSet();
  85.                 adapt.Fill(ds);
  86.                 Console.WriteLine(ds);
  87.                 dataGridView1.DataSource = ds.Tables[0].DefaultView;
  88.  
  89.                 MySqlCommand cmd2 = connection.CreateCommand();
  90.                 cmd2.CommandText = "SELECT * FROM employeeaddress";
  91.                 MySqlDataAdapter adapt2 = new MySqlDataAdapter(cmd2);
  92.                 DataSet ds2 = new DataSet();
  93.                 adapt2.Fill(ds2);
  94.                 dgEAdresses.DataSource = ds2.Tables[0].DefaultView;
  95.  
  96.                 MySqlCommand cmd3 = connection.CreateCommand();
  97.                 cmd3.CommandText = "SELECT * FROM employeedegree";
  98.                 MySqlDataAdapter adapt3 = new MySqlDataAdapter(cmd3);
  99.                 DataSet ds3 = new DataSet();
  100.                 adapt3.Fill(ds3);
  101.                 dgvEDegrees.DataSource = ds3.Tables[0].DefaultView;
  102.                
  103.                 MySqlCommand cmd4 = connection.CreateCommand();
  104.                 cmd4.CommandText = "SELECT * FROM employeeposition";
  105.                 MySqlDataAdapter adapt4 = new MySqlDataAdapter(cmd4);
  106.                 DataSet ds4 = new DataSet();
  107.                 adapt4.Fill(ds4);
  108.                 dgvEPosition.DataSource = ds4.Tables[0].DefaultView;
  109.  
  110.                 MySqlCommand cmd5 = connection.CreateCommand();
  111.                 cmd5.CommandText = "SELECT * FROM projects";
  112.                 MySqlDataAdapter adapt5 = new MySqlDataAdapter(cmd5);
  113.                 DataSet ds5 = new DataSet();
  114.                 adapt5.Fill(ds5);
  115.                 dgvProjects.DataSource = ds5.Tables[0].DefaultView;
  116.  
  117.                 MySqlCommand cmd6 = connection.CreateCommand();
  118.                 cmd6.CommandText = "SELECT * FROM employeeproject";
  119.                 MySqlDataAdapter adapt6 = new MySqlDataAdapter(cmd6);
  120.                 DataSet ds6 = new DataSet();
  121.                 adapt6.Fill(ds6);
  122.                 dgvProMembers.DataSource = ds6.Tables[0].DefaultView;
  123.             }
  124.             catch (Exception)
  125.             {
  126.                 throw;
  127.             }
  128.             finally
  129.             {
  130.                 if(connection.State == ConnectionState.Open)
  131.                 {
  132.                     connection.Clone();
  133.                 }
  134.             }
  135.  
  136.         }
  137.  
  138.         private void btDelete_Click(object sender, EventArgs e)
  139.         {
  140.             if (dataGridView1.SelectedCells.Count > 0)
  141.             {
  142.                 string id = dataGridView1.SelectedCells[0].Value.ToString();
  143.  
  144.                 foreach (DataGridViewRow item in this.dataGridView1.SelectedRows)
  145.                 {
  146.                     string ConnectionString = "Server=localhost;Database=mydb;Uid=root;Pwd=root";
  147.                     MySqlConnection connection = new MySqlConnection(ConnectionString);
  148.                     connection.Open();
  149.                     try
  150.                     {
  151.                         int selID = dataGridView1.SelectedCells[0].RowIndex;
  152.                         MySqlCommand cmd = connection.CreateCommand();
  153.                         cmd.CommandText = "DELETE FROM employees WHERE employeeID=@id";
  154.                         cmd.Parameters.AddWithValue("@id", id);
  155.                         MySqlDataAdapter adapt = new MySqlDataAdapter(cmd);
  156.                         DataSet ds = new DataSet();
  157.                         adapt.Fill(ds);
  158.                         //dataGridView1.DataSource = ds.Tables[0].DefaultView;
  159.                     }
  160.                     catch (Exception)
  161.                     {
  162.                         throw;
  163.                     }
  164.                     finally
  165.                     {
  166.                         if (connection.State == ConnectionState.Open)
  167.                         {
  168.                             connection.Clone();
  169.                             LoadData();
  170.                         }
  171.                     }
  172.                 }
  173.             }
  174.         }
  175.  
  176.         private void button4_Click(object sender, EventArgs e)
  177.         {
  178.             string ConnectionString = "Server=localhost;Database=mydb;Uid=root;Pwd=root";
  179.             MySqlConnection connection = new MySqlConnection(ConnectionString); ;
  180.             MySqlCommand cmd;
  181.             connection.Open();
  182.  
  183.             try
  184.             {
  185.                 cmd = connection.CreateCommand();
  186.                 if (lbTempAdressID.Text != "")
  187.                 {
  188.                     string streetChk = dgEAdresses.SelectedCells[3].Value.ToString();
  189.                     cmd.CommandText = "UPDATE employees SET country=@country, city=@city, street=@street, number=@number, zip=@zip WHERE employeeID=@idd AND street=@streetChk";
  190.                     cmd.Parameters.AddWithValue("@idd", lbTempAdressID.Text);
  191.                     cmd.Parameters.AddWithValue("@country", tbCountry.Text);
  192.                     cmd.Parameters.AddWithValue("@city", tbCity.Text);
  193.                     cmd.Parameters.AddWithValue("@street", tbStreet.Text);
  194.                     cmd.Parameters.AddWithValue("@number", nudHouseNumber.Value);
  195.                     cmd.Parameters.AddWithValue("@zip", tbZIP.Text);
  196.  
  197.                     cmd.Parameters.AddWithValue("@streetChk", streetChk);
  198.  
  199.                     tbCountry.Text = "";
  200.                     tbCity.Text = "";
  201.                     tbStreet.Text = "";
  202.                     tbZIP.Text = "";
  203.                     nudHouseNumber.Value = 0;
  204.  
  205.                     lbTempEmpID.Text = "";
  206.                 }
  207.                 else
  208.                 {
  209.                     cmd.CommandText = "INSERT INTO addresses(employeeID, country, city, street, number, zip)VALUES(@empID, @country, @city, @street, @number, @zip)";
  210.                     cmd.Parameters.AddWithValue("@empID", nudEmpID.Value);
  211.                     cmd.Parameters.AddWithValue("@country", tbCountry.Text);
  212.                     cmd.Parameters.AddWithValue("@city", tbCity.Text);
  213.                     cmd.Parameters.AddWithValue("@street", tbStreet.Text);
  214.                     cmd.Parameters.AddWithValue("@number", nudHouseNumber.Value);
  215.                     cmd.Parameters.AddWithValue("@zip", tbZIP.Text);
  216.  
  217.                     lbTempEmpID.Text = "";
  218.                 }
  219.                 cmd.ExecuteNonQuery();
  220.             }
  221.             catch (Exception)
  222.             {
  223.                 throw;
  224.             }
  225.             finally
  226.             {
  227.                 if (connection.State == ConnectionState.Open)
  228.                 {
  229.                     connection.Close();
  230.                     LoadData();
  231.                 }
  232.             }
  233.         }
  234.  
  235.         private void btDeleteAddress_Click(object sender, EventArgs e)
  236.         {
  237.             if (dgEAdresses.SelectedCells.Count > 0)
  238.             {
  239.                 string id = dgEAdresses.SelectedCells[0].Value.ToString();
  240.                 string street = dgEAdresses.SelectedCells[3].Value.ToString();
  241.  
  242.                 foreach (DataGridViewRow item in this.dgEAdresses.SelectedRows)
  243.                 {
  244.                     string ConnectionString = "Server=localhost;Database=mydb;Uid=root;Pwd=root";
  245.                     MySqlConnection connection = new MySqlConnection(ConnectionString);
  246.                     connection.Open();
  247.                     try
  248.                     {
  249.                         int selID = dgEAdresses.SelectedCells[0].RowIndex;
  250.                         MySqlCommand cmd = connection.CreateCommand();
  251.                         cmd.CommandText = "DELETE FROM addresses WHERE employeeID=@id AND street=@street";
  252.                         cmd.Parameters.AddWithValue("@id", id);
  253.                         cmd.Parameters.AddWithValue("@street", street);
  254.                         MySqlDataAdapter adapt = new MySqlDataAdapter(cmd);
  255.                         DataSet ds = new DataSet();
  256.                         adapt.Fill(ds);
  257.                         //dgEAdresses.DataSource = ds.Tables[0].DefaultView;
  258.                     }
  259.                     catch (Exception)
  260.                     {
  261.                         throw;
  262.                     }
  263.                     finally
  264.                     {
  265.                         if (connection.State == ConnectionState.Open)
  266.                         {
  267.                             connection.Clone();
  268.                             LoadData();
  269.                         }
  270.                     }
  271.                 }
  272.             }
  273.         }
  274.  
  275.         private void btEditEmployee_Click(object sender, EventArgs e)
  276.         {
  277.             if (dataGridView1.SelectedCells.Count > 0)
  278.             {
  279.                 string id = dataGridView1.SelectedCells[0].Value.ToString();
  280.                 string bsn = dataGridView1.SelectedCells[1].Value.ToString();
  281.                 string name = dataGridView1.SelectedCells[2].Value.ToString();
  282.                 string surname = dataGridView1.SelectedCells[3].Value.ToString();
  283.  
  284.                 tbAbsn.Text = bsn;
  285.                 tbAfirstname.Text = name;
  286.                 tbAsurname.Text = surname;
  287.  
  288.                 lbTempEmpID.Text = id;
  289.             }
  290.         }
  291.  
  292.         private void tbEditAddress_Click(object sender, EventArgs e)
  293.         {
  294.             if (dgEAdresses.SelectedCells.Count > 0)
  295.             {
  296.                 string id = dgEAdresses.SelectedCells[0].Value.ToString();
  297.                 string country = dgEAdresses.SelectedCells[1].Value.ToString();
  298.                 string city = dgEAdresses.SelectedCells[2].Value.ToString();
  299.                 string street = dgEAdresses.SelectedCells[3].Value.ToString();
  300.                 string number = dgEAdresses.SelectedCells[4].Value.ToString();
  301.                 string zip = dgEAdresses.SelectedCells[5].Value.ToString();
  302.  
  303.                 nudEmpID.Value = int.Parse(id);
  304.                 tbCountry.Text = country;
  305.                 tbCity.Text = city;
  306.                 tbStreet.Text = street;
  307.                 nudHouseNumber.Value = int.Parse(number);
  308.                 tbZIP.Text = zip;
  309.  
  310.                 lbTempAdressID.Text = id;
  311.             }
  312.         }
  313.  
  314.         private void btDegAdd_Click(object sender, EventArgs e)
  315.         {
  316.             string ConnectionString = "Server=localhost;Database=mydb;Uid=root;Pwd=root";
  317.             MySqlConnection connection = new MySqlConnection(ConnectionString); ;
  318.             MySqlCommand cmd;
  319.             connection.Open();
  320.  
  321.             try
  322.             {
  323.                 cmd = connection.CreateCommand();
  324.  
  325.                 if (lbDegTempID.Text != "")
  326.                 {
  327.                     string courseChk = dgvEDegrees.SelectedCells[1].Value.ToString();
  328.                     cmd.CommandText = "UPDATE employee_degree SET employeeID=@empid, course=@course, school=@school, level=@level WHERE employeeID=@empid AND course=@courseChk";
  329.  
  330.                     cmd.Parameters.AddWithValue("@empid", nudDegEmpID.Value);
  331.                     cmd.Parameters.AddWithValue("@course", tbDegCourse.Text);
  332.                     cmd.Parameters.AddWithValue("@school", tbDegSchool.Text);
  333.                     cmd.Parameters.AddWithValue("@level", tbDegLevel.Text);
  334.                     cmd.Parameters.AddWithValue("@courseChk", courseChk);
  335.  
  336.                     tbDegCourse.Text = "";
  337.                     tbDegSchool.Text = "";
  338.                     tbDegLevel.Text = "";
  339.  
  340.                     lbDegTempID.Text = "";
  341.                 }
  342.                 else
  343.                 {
  344.                     cmd.CommandText = "INSERT INTO employee_degree(employeeID,course,school,level)VALUES(@empid,@course,@school,@level)";
  345.                     cmd.Parameters.AddWithValue("@empid", nudDegEmpID.Value);
  346.                     cmd.Parameters.AddWithValue("@course", tbDegCourse.Text);
  347.                     cmd.Parameters.AddWithValue("@school", tbDegSchool.Text);
  348.                     cmd.Parameters.AddWithValue("@level", tbDegLevel.Text);
  349.  
  350.                     lbDegTempID.Text = "";
  351.                 }
  352.  
  353.                 cmd.ExecuteNonQuery();
  354.             }
  355.             catch (Exception)
  356.             {
  357.                 throw;
  358.             }
  359.             finally
  360.             {
  361.                 if (connection.State == ConnectionState.Open)
  362.                 {
  363.                     connection.Close();
  364.                     LoadData();
  365.                 }
  366.             }
  367.         }
  368.  
  369.         private void btDegEdit_Click(object sender, EventArgs e)
  370.         {
  371.             if (dgvEDegrees.SelectedCells.Count > 0)
  372.             {
  373.                 string id = dgvEDegrees.SelectedCells[0].Value.ToString();
  374.                 string course = dgvEDegrees.SelectedCells[1].Value.ToString();
  375.                 string school = dgvEDegrees.SelectedCells[2].Value.ToString();
  376.                 string level = dgvEDegrees.SelectedCells[3].Value.ToString();
  377.  
  378.                 nudDegEmpID.Value = int.Parse(id);
  379.                 tbDegCourse.Text = course;
  380.                 tbDegSchool.Text = school;
  381.                 tbDegLevel.Text = level;
  382.  
  383.                 lbDegTempID.Text = id;
  384.             }
  385.         }
  386.  
  387.         private void btDegDelete_Click(object sender, EventArgs e)
  388.         {
  389.             if (dgvEDegrees.SelectedCells.Count > 0)
  390.             {
  391.                 string id = dgvEDegrees.SelectedCells[0].Value.ToString();
  392.                 string coursee = dgvEDegrees.SelectedCells[1].Value.ToString();
  393.  
  394.                 foreach (DataGridViewRow item in this.dgvEDegrees.SelectedRows)
  395.                 {
  396.                     string ConnectionString = "Server=localhost;Database=mydb;Uid=root;Pwd=root";
  397.                     MySqlConnection connection = new MySqlConnection(ConnectionString);
  398.                     connection.Open();
  399.                     try
  400.                     {
  401.                         int selID = dgvEDegrees.SelectedCells[0].RowIndex;
  402.                         MySqlCommand cmd = connection.CreateCommand();
  403.                         cmd.CommandText = "DELETE FROM employee_degree WHERE employeeID=@id AND course=@course";
  404.                         cmd.Parameters.AddWithValue("@id", id);
  405.                         cmd.Parameters.AddWithValue("@course", coursee);
  406.  
  407.                         MySqlDataAdapter adapt = new MySqlDataAdapter(cmd);
  408.                         DataSet ds = new DataSet();
  409.                         adapt.Fill(ds);
  410.                         //dataGridView1.DataSource = ds.Tables[0].DefaultView;
  411.                     }
  412.                     catch (Exception)
  413.                     {
  414.                         throw;
  415.                     }
  416.                     finally
  417.                     {
  418.                         if (connection.State == ConnectionState.Open)
  419.                         {
  420.                             connection.Clone();
  421.                             LoadData();
  422.                         }
  423.                     }
  424.                 }
  425.             }
  426.         }
  427.  
  428.         private void btPosAdd_Click(object sender, EventArgs e)
  429.         {
  430.             string ConnectionString = "Server=localhost;Database=mydb;Uid=root;Pwd=root";
  431.             MySqlConnection connection = new MySqlConnection(ConnectionString); ;
  432.             MySqlCommand cmd;
  433.             connection.Open();
  434.  
  435.             try
  436.             {
  437.                 cmd = connection.CreateCommand();
  438.  
  439.                 if (lbPosEmpID.Text != "")
  440.                 {
  441.                     cmd.CommandText = "UPDATE employee_position SET employeeID=@empid, description=@description, hourFee=@hourFee, locationHQID=@locationHQID WHERE employeeID=@empid";
  442.  
  443.                     cmd.Parameters.AddWithValue("@empid", nudPosEmpID.Value);
  444.                     cmd.Parameters.AddWithValue("@description", tbPosDesc.Text);
  445.                     cmd.Parameters.AddWithValue("@hourFee", tbPosHour.Text);
  446.                     cmd.Parameters.AddWithValue("@locationHQID", tbPosHQID.Text);
  447.  
  448.                     tbPosHQID.Text = "";
  449.                     tbPosHour.Text = "";
  450.                     tbPosDesc.Text = "";
  451.  
  452.                     lbPosEmpID.Text = "";
  453.                 }
  454.                 else
  455.                 {
  456.                     cmd.CommandText = "INSERT INTO employee_position(employeeID,description,hourFee,locationHQID)VALUES(@empid,@description,@hourFee,@locationHQID)";
  457.                     cmd.Parameters.AddWithValue("@empid", nudPosEmpID.Value);
  458.                     cmd.Parameters.AddWithValue("@description", tbPosDesc.Text);
  459.                     cmd.Parameters.AddWithValue("@hourFee", tbPosHour.Text);
  460.                     cmd.Parameters.AddWithValue("@locationHQID", tbPosHQID.Text);
  461.  
  462.                     lbPosEmpID.Text = "";
  463.                 }
  464.  
  465.                 cmd.ExecuteNonQuery();
  466.             }
  467.             catch (Exception)
  468.             {
  469.                 throw;
  470.             }
  471.             finally
  472.             {
  473.                 if (connection.State == ConnectionState.Open)
  474.                 {
  475.                     connection.Close();
  476.                     LoadData();
  477.                 }
  478.             }
  479.         }
  480.  
  481.         private void btPosDelete_Click(object sender, EventArgs e)
  482.         {
  483.             if (dgvEPosition.SelectedCells.Count > 0)
  484.             {
  485.                 string id = dgvEPosition.SelectedCells[0].Value.ToString();
  486.  
  487.                 foreach (DataGridViewRow item in this.dgvEPosition.SelectedRows)
  488.                 {
  489.                     string ConnectionString = "Server=localhost;Database=mydb;Uid=root;Pwd=root";
  490.                     MySqlConnection connection = new MySqlConnection(ConnectionString);
  491.                     connection.Open();
  492.                     try
  493.                     {
  494.                         int selID = dgvEPosition.SelectedCells[0].RowIndex;
  495.                         MySqlCommand cmd = connection.CreateCommand();
  496.                         cmd.CommandText = "DELETE FROM employee_position WHERE employeeID=@id";
  497.                         cmd.Parameters.AddWithValue("@id", id);
  498.  
  499.                         MySqlDataAdapter adapt = new MySqlDataAdapter(cmd);
  500.                         DataSet ds = new DataSet();
  501.                         adapt.Fill(ds);
  502.                         //dataGridView1.DataSource = ds.Tables[0].DefaultView;
  503.                     }
  504.                     catch (Exception)
  505.                     {
  506.                         throw;
  507.                     }
  508.                     finally
  509.                     {
  510.                         if (connection.State == ConnectionState.Open)
  511.                         {
  512.                             connection.Clone();
  513.                             LoadData();
  514.                         }
  515.                     }
  516.                 }
  517.             }
  518.         }
  519.  
  520.         private void tabPage4_Click(object sender, EventArgs e)
  521.         {
  522.  
  523.         }
  524.  
  525.         private void btPosEdit_Click(object sender, EventArgs e)
  526.         {
  527.             if (dgvEPosition.SelectedCells.Count > 0)
  528.             {
  529.                 string id = dgvEPosition.SelectedCells[0].Value.ToString();
  530.                 string desc = dgvEPosition.SelectedCells[1].Value.ToString();
  531.                 string hourfee = dgvEPosition.SelectedCells[2].Value.ToString();
  532.                 string locationid = dgvEPosition.SelectedCells[3].Value.ToString();
  533.  
  534.                 nudPosEmpID.Value = int.Parse(id);
  535.                 tbPosDesc.Text = desc;
  536.                 tbPosHour.Text = hourfee;
  537.                 tbPosHQID.Text = locationid;
  538.  
  539.                 lbPosEmpID.Text = id;
  540.             }
  541.         }
  542.  
  543.         private void btProAdd_Click(object sender, EventArgs e)
  544.         {
  545.             string ConnectionString = "Server=localhost;Database=mydb;Uid=root;Pwd=root";
  546.             MySqlConnection connection = new MySqlConnection(ConnectionString); ;
  547.             MySqlCommand cmd;
  548.             connection.Open();
  549.  
  550.             try
  551.             {
  552.                 cmd = connection.CreateCommand();
  553.  
  554.                 if (lbProTempEmpID.Text != "")
  555.                 {
  556.                     cmd.CommandText = "UPDATE projects SET name=@name, budget=@budget, allocatedHours=@hours WHERE id=@id";
  557.                     cmd.Parameters.AddWithValue("@id", lbProTempEmpID.Text);
  558.                     cmd.Parameters.AddWithValue("@name", tbProName.Text);
  559.                     cmd.Parameters.AddWithValue("@budget", nudProBudget.Value);
  560.                     cmd.Parameters.AddWithValue("@hours", nudProHours.Value);
  561.  
  562.                     tbProName.Text = "";
  563.                     nudProHours.Value = 0;
  564.                     nudProBudget.Value = 0;
  565.                     lbProTempEmpID.Text = "";
  566.                 }
  567.                 else
  568.                 {
  569.                     cmd.CommandText = "INSERT INTO projects(name,budget,allocatedHours)VALUES(@name,@budget,@hours)";
  570.                  
  571.                     cmd.Parameters.AddWithValue("@name", tbProName.Text);
  572.                     cmd.Parameters.AddWithValue("@budget", nudProBudget.Value);
  573.                     cmd.Parameters.AddWithValue("@hours", nudProHours.Value);
  574.  
  575.                     lbProTempEmpID.Text = "";
  576.                 }
  577.  
  578.                 cmd.ExecuteNonQuery();
  579.             }
  580.             catch (Exception)
  581.             {
  582.                 throw;
  583.             }
  584.             finally
  585.             {
  586.                 if (connection.State == ConnectionState.Open)
  587.                 {
  588.                     connection.Close();
  589.                     LoadData();
  590.                 }
  591.             }
  592.         }
  593.  
  594.         private void btProEdit_Click(object sender, EventArgs e)
  595.         {
  596.             if (dgvProjects.SelectedCells.Count > 0)
  597.             {
  598.                 string id = dgvProjects.SelectedCells[0].Value.ToString();
  599.                 string budget = dgvProjects.SelectedCells[1].Value.ToString();
  600.                 string hours = dgvProjects.SelectedCells[2].Value.ToString();
  601.                 string name = dgvProjects.SelectedCells[3].Value.ToString();
  602.  
  603.                
  604.                 nudProBudget.Value = int.Parse(budget);
  605.                 nudProHours.Value = int.Parse(hours);
  606.                 tbProName.Text = name;
  607.  
  608.                 lbProTempEmpID.Text = id;
  609.             }
  610.         }
  611.  
  612.         private void btProDelete_Click(object sender, EventArgs e)
  613.         {
  614.             if (dgvProjects.SelectedCells.Count > 0)
  615.             {
  616.                 string id = dgvProjects.SelectedCells[0].Value.ToString();
  617.  
  618.                 foreach (DataGridViewRow item in this.dgvProjects.SelectedRows)
  619.                 {
  620.                     string ConnectionString = "Server=localhost;Database=mydb;Uid=root;Pwd=root";
  621.                     MySqlConnection connection = new MySqlConnection(ConnectionString);
  622.                     connection.Open();
  623.                     try
  624.                     {
  625.                         int selID = dgvProjects.SelectedCells[0].RowIndex;
  626.                         MySqlCommand cmd = connection.CreateCommand();
  627.                         cmd.CommandText = "DELETE FROM projects WHERE id=@id";
  628.                         cmd.Parameters.AddWithValue("@id", id);
  629.  
  630.                         MySqlDataAdapter adapt = new MySqlDataAdapter(cmd);
  631.                         DataSet ds = new DataSet();
  632.                         adapt.Fill(ds);
  633.                         //dataGridView1.DataSource = ds.Tables[0].DefaultView;
  634.                     }
  635.                     catch (Exception)
  636.                     {
  637.                         throw;
  638.                     }
  639.                     finally
  640.                     {
  641.                         if (connection.State == ConnectionState.Open)
  642.                         {
  643.                             connection.Clone();
  644.                             LoadData();
  645.                         }
  646.                     }
  647.                 }
  648.             }
  649.         }
  650.  
  651.         private void btProMAdd_Click(object sender, EventArgs e)
  652.         {
  653.             string ConnectionString = "Server=localhost;Database=mydb;Uid=root;Pwd=root";
  654.             MySqlConnection connection = new MySqlConnection(ConnectionString); ;
  655.             MySqlCommand cmd;
  656.             connection.Open();
  657.  
  658.             try
  659.             {
  660.                 cmd = connection.CreateCommand();
  661.  
  662.                 if (lbPMTempID.Text != "")
  663.                 {
  664.                     cmd.CommandText = "UPDATE projectmembers SET employeeID=@ide, projectID=@idp WHERE employeeID=@id AND projectID=@pid";
  665.                     cmd.Parameters.AddWithValue("@id", lbPMTempID.Text);
  666.                     cmd.Parameters.AddWithValue("@pid", label23.Text);
  667.  
  668.                     cmd.Parameters.AddWithValue("@ide", nudPMEID.Value);
  669.                     cmd.Parameters.AddWithValue("@idp", nudPMPID.Value);
  670.  
  671.                     nudPMEID.Value = 0;
  672.                     nudPMPID.Value = 0;
  673.                     lbPMTempID.Text = "";
  674.                 }
  675.                 else
  676.                 {
  677.                     cmd.CommandText = "INSERT INTO projectmembers(employeeID,projectID)VALUES(@ide,@idp)";
  678.  
  679.                     cmd.Parameters.AddWithValue("@ide", nudPMEID.Value);
  680.                     cmd.Parameters.AddWithValue("@idp", nudPMPID.Value);
  681.  
  682.                     lbPMTempID.Text = "";
  683.                 }
  684.  
  685.                 cmd.ExecuteNonQuery();
  686.             }
  687.             catch (Exception)
  688.             {
  689.                 throw;
  690.             }
  691.             finally
  692.             {
  693.                 if (connection.State == ConnectionState.Open)
  694.                 {
  695.                     connection.Close();
  696.                     LoadData();
  697.                 }
  698.             }
  699.         }
  700.  
  701.         private void btProMEdit_Click(object sender, EventArgs e)
  702.         {
  703.             if (dgvProMembers.SelectedCells.Count > 0)
  704.             {
  705.                 string id = dgvProMembers.SelectedCells[0].Value.ToString();
  706.                 string projID = dgvProMembers.SelectedCells[1].Value.ToString();
  707.  
  708.                 nudPMEID.Text = id;
  709.                 nudPMPID.Text = projID;
  710.  
  711.                 lbPMTempID.Text = id;
  712.                 label23.Text = projID;
  713.             }
  714.         }
  715.  
  716.         private void btProMDelete_Click(object sender, EventArgs e)
  717.         {
  718.             if (dgvProMembers.SelectedCells.Count > 0)
  719.             {
  720.                 string id = dgvProMembers.SelectedCells[0].Value.ToString();
  721.  
  722.                 foreach (DataGridViewRow item in this.dgvProMembers.SelectedRows)
  723.                 {
  724.                     string ConnectionString = "Server=localhost;Database=mydb;Uid=root;Pwd=root";
  725.                     MySqlConnection connection = new MySqlConnection(ConnectionString);
  726.                     connection.Open();
  727.                     try
  728.                     {
  729.                         int selID = dgvProMembers.SelectedCells[0].RowIndex;
  730.                         MySqlCommand cmd = connection.CreateCommand();
  731.                         cmd.CommandText = "DELETE FROM projectmembers WHERE employeeID=@id AND projectID=@pid";
  732.                         cmd.Parameters.AddWithValue("@id", id);
  733.                         cmd.Parameters.AddWithValue("@pid", dgvProMembers.SelectedCells[1].Value.ToString());
  734.  
  735.                         MySqlDataAdapter adapt = new MySqlDataAdapter(cmd);
  736.                         DataSet ds = new DataSet();
  737.                         adapt.Fill(ds);
  738.                         //dataGridView1.DataSource = ds.Tables[0].DefaultView;
  739.                     }
  740.                     catch (Exception)
  741.                     {
  742.                         throw;
  743.                     }
  744.                     finally
  745.                     {
  746.                         if (connection.State == ConnectionState.Open)
  747.                         {
  748.                             connection.Clone();
  749.                             LoadData();
  750.                         }
  751.                     }
  752.                 }
  753.             }
  754.         }
  755.  
  756.         private void button1_Click_1(object sender, EventArgs e)
  757.         {
  758.             string ConnectionString = "Server=localhost;Database=mydb;Uid=root;Pwd=root";
  759.             MySqlConnection connection = new MySqlConnection(ConnectionString);
  760.             connection.Open();
  761.  
  762.             try
  763.             {
  764.                 MySqlCommand cmd = connection.CreateCommand();
  765.                 cmd.CommandText = "SELECT p.projectID FROM projects p, headquarters h, ProjectHeadquarter ph WHERE p.projectID = ph.ProjectID AND h.HQID = p.HQID AND p.budget* 0,1 < h.rent";
  766.                 MySqlDataAdapter adapt = new MySqlDataAdapter(cmd);
  767.                 DataSet ds = new DataSet();
  768.                 adapt.Fill(ds);
  769.                 dataGridView2.DataSource = ds.Tables[0].DefaultView;
  770.             }
  771.             catch (Exception)
  772.             {
  773.                 throw;
  774.             }
  775.         }
  776.     }
  777. }
Advertisement
Add Comment
Please, Sign In to add comment