hipzi

Supermarket Produk

Apr 19th, 2021
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.85 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 System.Data.SqlClient;
  11.  
  12. namespace Supermarket
  13. {
  14.     public partial class ProdukForm : Form
  15.     {
  16.         public ProdukForm()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.         private void button1_Click(object sender, EventArgs e)
  21.         {
  22.             SellerForm produk = new SellerForm();
  23.             produk.Show();
  24.             this.Hide();
  25.         }
  26.         SqlConnection connect = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\USERS\ASUS\DOCUMENTS\SUPERMARKETDB.mdf;Integrated Security=True;Connect Timeout=30");
  27.         //SqlConnection connect = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\TOSHIBA\Documents\supermarketdb.mdf;Integrated Security=True;Connect Timeout=30");
  28.         private void label5_Click(object sender, EventArgs e)
  29.         {
  30.             Application.Exit();
  31.         }
  32.  
  33.         private void bunifuMaterialTextbox2_OnValueChanged(object sender, EventArgs e)
  34.         {
  35.  
  36.         }
  37.  
  38.         private void bunifuMaterialTextbox1_OnValueChanged(object sender, EventArgs e)
  39.         {
  40.  
  41.         }
  42.  
  43.         private void bunifuMaterialTextbox3_OnValueChanged(object sender, EventArgs e)
  44.         {
  45.  
  46.         }
  47.  
  48.         private void bunifuMaterialTextbox4_OnValueChanged(object sender, EventArgs e)
  49.         {
  50.  
  51.         }
  52.  
  53.         private void fillCombo()
  54.         {
  55.             connect.Open();
  56.             SqlCommand cmd = new SqlCommand("select NamaKategori from Kategori", connect);
  57.             SqlDataReader rdr;
  58.             rdr = cmd.ExecuteReader();
  59.             DataTable dt = new DataTable();
  60.             dt.Columns.Add("NamaKategori", typeof(string));
  61.             dt.Load(rdr);
  62.             KategoriCb.ValueMember = "NamaKategori";
  63.             KategoriCb.DataSource = dt;
  64.             connect.Close();
  65.         }
  66.  
  67.         private void ProdukForm_Load(object sender, EventArgs e)
  68.         {
  69.             fillCombo();
  70.             populate();
  71.         }
  72.  
  73.         private void button2_Click(object sender, EventArgs e)
  74.         {
  75.             CategoryForm cat = new CategoryForm();
  76.             cat.Show();
  77.             this.Hide();
  78.         }
  79.         private void button10_Click(object sender, EventArgs e)
  80.         {
  81.             try
  82.             {
  83.                 connect.Open();
  84.                 string query = "insert into Produk values (" + IDProduk.Text + ",'" + NamaProduk.Text + "'," + QuantProduk.Text + ","+ priceProduk.Text +", '"+KategoriCb.SelectedValue.ToString()+"')";
  85.                 SqlCommand cmd = new SqlCommand(query, connect);
  86.                 cmd.ExecuteNonQuery();
  87.                 MessageBox.Show("Product Added Successfully");
  88.                 connect.Close();
  89.                 populate();
  90.                 IDProduk.Text = "";
  91.                 NamaProduk.Text = "";
  92.                 QuantProduk.Text = "";
  93.                 priceProduk.Text = "";
  94.                 KategoriCb.Text = "";
  95.             }
  96.             catch (Exception ex)
  97.             {
  98.                 MessageBox.Show(ex.Message);
  99.             }
  100.         }
  101.  
  102.         private void populate()
  103.         {
  104.             connect.Open();
  105.             string query = "select * from Produk";
  106.             SqlDataAdapter sda = new SqlDataAdapter(query, connect);
  107.             SqlCommandBuilder builder = new SqlCommandBuilder(sda);
  108.             var dataset = new DataSet();
  109.             sda.Fill(dataset);
  110.             ProdukGDV.DataSource = dataset.Tables[0];
  111.             connect.Close();
  112.         }
  113.  
  114.         private void button12_Click(object sender, EventArgs e)
  115.         {
  116.             try
  117.             {
  118.                 if (IDProduk.Text == "" || NamaProduk.Text == "" || QuantProduk.Text == "" || priceProduk.Text == "")
  119.                 {
  120.                     MessageBox.Show("Missing Information");
  121.                 }
  122.                 else
  123.                 {
  124.                     connect.Open();
  125.                     string query = "update Produk set NamaProduk='" + NamaProduk.Text + "',QuantProduk='" + QuantProduk.Text + "', KategoriProduk='" + KategoriCb.SelectedValue.ToString() + "' where IDProduk=" + IDProduk.Text + ";";
  126.                     SqlCommand cmd = new SqlCommand(query, connect);
  127.                     cmd.ExecuteNonQuery();
  128.                     MessageBox.Show("Product Successfully Updated");
  129.                     connect.Close();
  130.                     populate();
  131.                     IDProduk.Text = "";
  132.                     NamaProduk.Text = "";
  133.                     QuantProduk.Text = "";
  134.                     priceProduk.Text = "";
  135.                     KategoriCb.Text = "";
  136.                 }
  137.             }
  138.             catch (Exception ex)
  139.             {
  140.                 MessageBox.Show(ex.Message);
  141.             }
  142.         }
  143.  
  144.         private void ProdukGDV_CellContentClick(object sender, DataGridViewCellEventArgs e)
  145.         {
  146.             IDProduk.Text = ProdukGDV.SelectedRows[0].Cells[0].Value.ToString();
  147.             NamaProduk.Text = ProdukGDV.SelectedRows[0].Cells[1].Value.ToString();
  148.             QuantProduk.Text = ProdukGDV.SelectedRows[0].Cells[2].Value.ToString();
  149.             priceProduk.Text = ProdukGDV.SelectedRows[0].Cells[3].Value.ToString();
  150.             KategoriCb.Text = ProdukGDV.SelectedRows[0].Cells[4].Value.ToString();
  151.         }
  152.  
  153.         private void button11_Click(object sender, EventArgs e)
  154.         {
  155.             try
  156.             {
  157.                 if (IDProduk.Text == "")
  158.                 {
  159.  
  160.                     MessageBox.Show("Select The Product to Delete");
  161.                 }
  162.                 else
  163.                 {
  164.                     connect.Open();
  165.                     string query = "delete from Produk where IDProduk=" + IDProduk.Text + "";
  166.                     SqlCommand cmd = new SqlCommand(query, connect);
  167.                     cmd.ExecuteNonQuery();
  168.                     MessageBox.Show("Product Deleted Successfully");
  169.                     connect.Close();
  170.                     populate();
  171.                     IDProduk.Text = "";
  172.                     NamaProduk.Text = "";
  173.                     QuantProduk.Text = "";
  174.                     priceProduk.Text = "";
  175.                     KategoriCb.Text = "";
  176.                 }
  177.             }
  178.             catch (Exception ex)
  179.             {
  180.                 MessageBox.Show(ex.Message);
  181.             }
  182.         }
  183.  
  184.         private void button3_Click(object sender, EventArgs e)
  185.         {
  186.             this.Hide();
  187.             Form1 login = new Form1();
  188.             login.Show();
  189.         }
  190.  
  191.         private void button13_Click(object sender, EventArgs e)
  192.         {
  193.             populate();
  194.         }
  195.  
  196.         private void button4_Click(object sender, EventArgs e)
  197.         {
  198.  
  199.         }
  200.     }
  201. }
  202.  
Advertisement
Add Comment
Please, Sign In to add comment