mamamaria

ChangeOrderForm

Apr 25th, 2022 (edited)
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.18 KB | None | 0 0
  1. using Npgsql;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  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 WindowsFormsApp2
  14. {
  15.     public partial class ChangeOrderForm : Form
  16.     {
  17.         NpgsqlConnection con;
  18.  
  19.         int id;
  20.         int clientID;
  21.  
  22.         private void Update()
  23.         {
  24.  
  25.             DataSet ds = new DataSet();
  26.             DataTable dt = new DataTable();
  27.             SqlDataReader dr;
  28.             NpgsqlDataAdapter da;
  29.             string sql = "Select * from clients";
  30.             da = new NpgsqlDataAdapter(sql, con);
  31.             ds.Reset();
  32.             da.Fill(ds);
  33.             dt = ds.Tables[0];
  34.             comboBoxClientName.DataSource = dt;
  35.             comboBoxClientName.DisplayMember = "name";
  36.             comboBoxClientName.ValueMember = "clientID";
  37.             comboBoxClientName.SelectedValue = clientID.ToString();
  38.  
  39.         }
  40.         private void UpdateorderEntries()
  41.         {
  42.  
  43.             DataSet ds = new DataSet();
  44.             DataTable dt = new DataTable();
  45.             SqlDataReader dr;
  46.             NpgsqlDataAdapter da;
  47.             string sqlorderEntries = "Select orderEntriesID, orderEntries.orderID, orderEntries.goodID, name, amount FROM  orderEntries JOIN goods on orderID = :id AND orderEntries.goodID = goods.goodID";
  48.             NpgsqlCommand command = new NpgsqlCommand(sqlorderEntries, con);
  49.             command.Parameters.AddWithValue("id", id);
  50.             da = new NpgsqlDataAdapter(command);
  51.             ds.Reset();
  52.             da.Fill(ds);
  53.             dt = ds.Tables[0];
  54.             dataGridView1.DataSource = dt;
  55.         }
  56.        
  57.            
  58.            
  59.        
  60.         public ChangeOrderForm(NpgsqlConnection con, int id, int clientID, DateTime orderdate, bool delivered)
  61.         {
  62.             InitializeComponent();
  63.             //comboBoxClientName.SelectedValue = clientID.ToString();
  64.             dateTimePicker.Value = orderdate;
  65.             checkBoxDelivered.Checked = delivered;
  66.             this.con = con;
  67.             this.id = id;
  68.             this.clientID = clientID;
  69.            
  70.  
  71.         }
  72.  
  73.        
  74.  
  75.         private void ChangeOrderForm_Load(object sender, EventArgs e)
  76.         {
  77.             Update();
  78.             UpdateorderEntries();
  79.         }
  80.  
  81.         private void ChangeOrderForm_FormClosed(object sender, FormClosedEventArgs e)
  82.         {
  83.             Close();
  84.         }
  85.  
  86.         private void buttonSave_Click(object sender, EventArgs e)
  87.         {
  88.             try
  89.             {
  90.                 NpgsqlCommand command = new NpgsqlCommand("UPDATE orders SET clientID = :clientID, orderdate =:orderdate, delivered = :delivered WHERE orderID = :id", con);
  91.                 command.Parameters.AddWithValue("id", id);
  92.                 command.Parameters.AddWithValue("clientID", comboBoxClientName.SelectedValue);
  93.                 command.Parameters.AddWithValue("orderdate", dateTimePicker.Value);
  94.                 command.Parameters.AddWithValue("delivered", checkBoxDelivered.Checked);
  95.                 command.ExecuteNonQuery();
  96.                 Close();
  97.             }
  98.             catch { }
  99.         }
  100.  
  101.         private void buttonChange_Click(object sender, EventArgs e)
  102.         {
  103.             try
  104.  
  105.             {
  106.                 int orderEntriesID = (int)dataGridView1.CurrentRow.Cells["orderEntriesID"].Value;
  107.                 int orderID = (int)dataGridView1.CurrentRow.Cells["orderID"].Value;
  108.                 int goodID = (int)dataGridView1.CurrentRow.Cells["goodID"].Value;
  109.                 int amount = (int)dataGridView1.CurrentRow.Cells["amount"].Value;
  110.                 ChangeGoodInOrderForm f = new ChangeGoodInOrderForm(con, orderEntriesID, orderID, goodID, amount);
  111.                 f.ShowDialog();
  112.                 Update();
  113.             }
  114.             catch { }
  115.  
  116.         }
  117.  
  118.         private void buttonAdd_Click(object sender, EventArgs e)
  119.         {
  120.             AddGoodInOrderForm f = new AddGoodInOrderForm(con, id);
  121.             f.ShowDialog();
  122.             Update();
  123.             UpdateorderEntries();
  124.         }
  125.     }
  126. }
  127.  
Add Comment
Please, Sign In to add comment