using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace Supermarket { public partial class ProductForm : Form { public ProductForm() { InitializeComponent(); } private void ProductButton2_Click(object sender, EventArgs e) { this.Hide(); ProductForm log = new ProductForm(); log.Show(); } private void CategoryButton2_Click(object sender, EventArgs e) { this.Hide(); CategoriesForm log = new CategoriesForm(); log.Show(); } private void SellerButton2_Click(object sender, EventArgs e) { this.Hide(); SellerForm log = new SellerForm(); log.Show(); } private void populate() { Con.Open(); string query = "SELECT * FROM ProductData"; SqlDataAdapter sda = new SqlDataAdapter(query, Con); SqlCommandBuilder builder = new SqlCommandBuilder(sda); var ds = new DataSet(); sda.Fill(ds); ProductDatagrid.DataSource = ds.Tables[0]; Con.Close(); } SqlConnection Con = new SqlConnection(@"[Your Data Location]"); private void AddButton2_Click(object sender, EventArgs e) { try { Con.Open(); string query = "INSERT INTO ProductData VALUES(" + ProductId.Text + ", '" + ProductName.Text + "', '" + ProductQuantity.Text + "', '" + ProductPrice.Text + "', '" + CategoryComboBox1.SelectedValue.ToString() + "')"; SqlCommand cmd = new SqlCommand(query, Con); cmd.ExecuteNonQuery(); MessageBox.Show("New Product Added Successfully"); Con.Close(); populate(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void EditButton2_Click(object sender, EventArgs e) { try { Con.Open(); string query = "UPDATE ProductData SET ProductName='" + ProductName.Text + "', ProductQuantity='" + ProductQuantity.Text + "', ProductPrice='" + ProductPrice.Text + "', ProductCategory='" + CategoryComboBox1.SelectedValue.ToString() + "' WHERE ProductId='" + ProductId.Text+ "'"; SqlCommand cmd = new SqlCommand(query, Con); cmd.ExecuteNonQuery(); MessageBox.Show("Product Updated Successfully"); Con.Close(); populate(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void DeleteButton2_Click(object sender, EventArgs e) { try { Con.Open(); string query = "DELETE FROM ProductData WHERE ProductId=" + ProductId.Text + ""; SqlCommand cmd = new SqlCommand(query, Con); cmd.ExecuteNonQuery(); MessageBox.Show("Product Deleted Successfully"); Con.Close(); populate(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void ProductDatagrid_CellContentClick(object sender, DataGridViewCellEventArgs e) { ProductId.Text = ProductDatagrid.SelectedRows[0].Cells[0].Value.ToString(); ProductName.Text = ProductDatagrid.SelectedRows[0].Cells[1].Value.ToString(); ProductQuantity.Text = ProductDatagrid.SelectedRows[0].Cells[2].Value.ToString(); ProductPrice.Text = ProductDatagrid.SelectedRows[0].Cells[3].Value.ToString(); CategoryComboBox1.SelectedValue = ProductDatagrid.SelectedRows[0].Cells[4].Value.ToString(); } private void Logout2_Click(object sender, EventArgs e) { this.Hide(); Login log = new Login(); log.Show(); } private void Fillcombo() { Con.Open(); SqlCommand cmd = new SqlCommand("SELECT CategoryName FROM CategoryData", Con); SqlDataReader rdr; rdr = cmd.ExecuteReader(); DataTable dt = new DataTable(); dt.Columns.Add("CategoryName", typeof(string)); dt.Load(rdr); CategoryComboBox1.ValueMember="CategoryName"; CategoryComboBox1.DataSource = dt; CategoryComboBox2.ValueMember = "CategoryName"; CategoryComboBox2.DataSource = dt; Con.Close(); } private void RefreshButton1_Click(object sender, EventArgs e) { Con.Open(); string query = "SELECT * FROM ProductData WHERE ProductCategory='"+CategoryComboBox2.SelectedValue.ToString()+"'"; SqlDataAdapter sda = new SqlDataAdapter(query, Con); SqlCommandBuilder builder = new SqlCommandBuilder(sda); var ds = new DataSet(); sda.Fill(ds); ProductDatagrid.DataSource = ds.Tables[0]; Con.Close(); } private void ProductForm_Load(object sender, EventArgs e) { populate(); Fillcombo(); } private void outbutton_Click(object sender, EventArgs e) { Application.Exit(); } } }