This paste brought to you by Pastebin. View Raw

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Data.SqlClient;
  9.  
  10. namespace Supermarket
  11. {
  12.     public partial class ProductForm : Form
  13.     {
  14.         public ProductForm()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.  
  19.         private void ProductButton2_Click(object sender, EventArgs e)
  20.         {
  21.             this.Hide();
  22.             ProductForm log = new ProductForm();
  23.             log.Show();
  24.         }
  25.  
  26.         private void CategoryButton2_Click(object sender, EventArgs e)
  27.         {
  28.             this.Hide();
  29.             CategoriesForm log = new CategoriesForm();
  30.             log.Show();
  31.         }
  32.  
  33.         private void SellerButton2_Click(object sender, EventArgs e)
  34.         {
  35.             this.Hide();
  36.             SellerForm log = new SellerForm();
  37.             log.Show();
  38.         }
  39.  
  40.         private void populate()
  41.         {
  42.             Con.Open();
  43.             string query = "SELECT * FROM ProductData";
  44.             SqlDataAdapter sda = new SqlDataAdapter(query, Con);
  45.             SqlCommandBuilder builder = new SqlCommandBuilder(sda);
  46.             var ds = new DataSet();
  47.             sda.Fill(ds);
  48.             ProductDatagrid.DataSource = ds.Tables[0];
  49.             Con.Close();
  50.         }
  51.  
  52.         SqlConnection Con = new SqlConnection(@"[Your Data Location]");
  53.         private void AddButton2_Click(object sender, EventArgs e)
  54.         {
  55.             try
  56.             {
  57.                 Con.Open();
  58.                 string query = "INSERT INTO ProductData VALUES(" + ProductId.Text + ", '" + ProductName.Text + "', '" + ProductQuantity.Text + "', '" + ProductPrice.Text + "', '" + CategoryComboBox1.SelectedValue.ToString() + "')";
  59.                 SqlCommand cmd = new SqlCommand(query, Con);
  60.                 cmd.ExecuteNonQuery();
  61.                 MessageBox.Show("New Product Added Successfully");
  62.                 Con.Close();
  63.                 populate();
  64.             }
  65.             catch (Exception ex)
  66.             {
  67.                 MessageBox.Show(ex.Message);
  68.             }
  69.         }
  70.  
  71.         private void EditButton2_Click(object sender, EventArgs e)
  72.         {
  73.             try
  74.             {
  75.                 Con.Open();
  76.                 string query = "UPDATE ProductData SET ProductName='" + ProductName.Text + "', ProductQuantity='" + ProductQuantity.Text + "', ProductPrice='" + ProductPrice.Text + "', ProductCategory='" + CategoryComboBox1.SelectedValue.ToString() + "' WHERE ProductId='" + ProductId.Text+ "'";
  77.                 SqlCommand cmd = new SqlCommand(query, Con);
  78.                 cmd.ExecuteNonQuery();
  79.                 MessageBox.Show("Product Updated Successfully");
  80.                 Con.Close();
  81.                 populate();
  82.             }
  83.             catch (Exception ex)
  84.             {
  85.                 MessageBox.Show(ex.Message);
  86.             }
  87.         }
  88.  
  89.         private void DeleteButton2_Click(object sender, EventArgs e)
  90.         {
  91.             try
  92.             {
  93.                 Con.Open();
  94.                 string query = "DELETE FROM ProductData WHERE ProductId=" + ProductId.Text + "";
  95.                 SqlCommand cmd = new SqlCommand(query, Con);
  96.                 cmd.ExecuteNonQuery();
  97.                 MessageBox.Show("Product Deleted Successfully");
  98.                 Con.Close();
  99.                 populate();
  100.             }
  101.             catch (Exception ex)
  102.             {
  103.                 MessageBox.Show(ex.Message);
  104.             }
  105.         }
  106.  
  107.         private void ProductDatagrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
  108.         {
  109.            
  110.                 ProductId.Text = ProductDatagrid.SelectedRows[0].Cells[0].Value.ToString();
  111.                 ProductName.Text = ProductDatagrid.SelectedRows[0].Cells[1].Value.ToString();
  112.                 ProductQuantity.Text = ProductDatagrid.SelectedRows[0].Cells[2].Value.ToString();
  113.                 ProductPrice.Text = ProductDatagrid.SelectedRows[0].Cells[3].Value.ToString();
  114.                 CategoryComboBox1.SelectedValue = ProductDatagrid.SelectedRows[0].Cells[4].Value.ToString();
  115.         }
  116.  
  117.         private void Logout2_Click(object sender, EventArgs e)
  118.         {
  119.             this.Hide();
  120.             Login log = new Login();
  121.             log.Show();
  122.         }
  123.  
  124.         private void Fillcombo()
  125.         {
  126.             Con.Open();
  127.             SqlCommand cmd = new SqlCommand("SELECT CategoryName FROM CategoryData", Con);
  128.             SqlDataReader rdr;
  129.             rdr = cmd.ExecuteReader();
  130.             DataTable dt = new DataTable();
  131.             dt.Columns.Add("CategoryName", typeof(string));
  132.             dt.Load(rdr);
  133.             CategoryComboBox1.ValueMember="CategoryName";
  134.             CategoryComboBox1.DataSource = dt;
  135.             CategoryComboBox2.ValueMember = "CategoryName";
  136.             CategoryComboBox2.DataSource = dt;
  137.             Con.Close();
  138.         }
  139.  
  140.         private void RefreshButton1_Click(object sender, EventArgs e)
  141.         {
  142.             Con.Open();
  143.             string query = "SELECT * FROM ProductData WHERE ProductCategory='"+CategoryComboBox2.SelectedValue.ToString()+"'";
  144.             SqlDataAdapter sda = new SqlDataAdapter(query, Con);
  145.             SqlCommandBuilder builder = new SqlCommandBuilder(sda);
  146.             var ds = new DataSet();
  147.             sda.Fill(ds);
  148.             ProductDatagrid.DataSource = ds.Tables[0];
  149.             Con.Close();
  150.         }
  151.  
  152.         private void ProductForm_Load(object sender, EventArgs e)
  153.         {
  154.             populate();
  155.             Fillcombo();
  156.         }
  157.  
  158.         private void outbutton_Click(object sender, EventArgs e)
  159.         {
  160.             Application.Exit();
  161.         }
  162.     }
  163. }
  164.