Advertisement
mamamaria

ReportTable

Apr 25th, 2022 (edited)
718
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.47 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 ReportTable : Form
  16.     {
  17.         NpgsqlConnection con;
  18.         int clientID;
  19.         private void Update()
  20.         {
  21.  
  22.             DataSet ds = new DataSet();
  23.             DataTable dt = new DataTable();
  24.             SqlDataReader dr;
  25.             NpgsqlDataAdapter da;
  26.             string sql = "Select * from clients";
  27.             da = new NpgsqlDataAdapter(sql, con);
  28.             ds.Reset();
  29.             da.Fill(ds);
  30.             dt = ds.Tables[0];
  31.             comboBoxClientName.DataSource = dt;
  32.             comboBoxClientName.DisplayMember = "name";
  33.             comboBoxClientName.ValueMember = "clientID";
  34.             //comboBoxClientName.SelectedValue = clientID.ToString();
  35.             UpdateorderEntries();
  36.  
  37.         }
  38.         private void UpdateorderEntries()
  39.         {
  40.  
  41.             DataSet ds = new DataSet();
  42.             DataTable dt = new DataTable();
  43.             SqlDataReader dr;
  44.             NpgsqlDataAdapter da;
  45.             string sqlorderEntries = "SELECT orders.orderID, goods.name, price*amount AS cost FROM orders LEFT JOIN orderEntries ON orders.clientID = :clientID AND orderEntries.orderID = orders.orderID JOIN goods ON goods.goodID = orderEntries.goodID WHERE delivered = false";
  46.             NpgsqlCommand command = new NpgsqlCommand(sqlorderEntries, con);
  47.             command.Parameters.AddWithValue("clientID", comboBoxClientName.SelectedValue);
  48.             da = new NpgsqlDataAdapter(command);
  49.             ds.Reset();
  50.             da.Fill(ds);
  51.             dt = ds.Tables[0];
  52.             dataGridView1.DataSource = dt;
  53.         }
  54.         public ReportTable()
  55.         {
  56.             InitializeComponent();
  57.             con = new NpgsqlConnection("Server=localhost;Port=5432;User Id=postgres;Password=masterkey;Database=lemmaITDB");
  58.             con.Open();
  59.  
  60.         }
  61.  
  62.         private void ReportTable_Load(object sender, EventArgs e)
  63.         {
  64.             Update();
  65.         }
  66.  
  67.         private void ReportTable_FormClosed(object sender, FormClosedEventArgs e)
  68.         {
  69.  
  70.         }
  71.  
  72.         private void comboBoxClientName_SelectedIndexChanged(object sender, EventArgs e)
  73.         {
  74.             Update();
  75.         }
  76.     }
  77. }
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement