-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViewProducts.cs
140 lines (128 loc) · 4.52 KB
/
ViewProducts.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace KHALAQPROOF
{
public partial class ViewProducts : Form
{
public ViewProducts()
{
InitializeComponent();
DisplayProducts();
}
private void ViewProducts_Load(object sender, EventArgs e)
{
}
private void label3_Click(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
MainMenu viewProducts = new MainMenu();
this.Hide();
viewProducts.ShowDialog();
this.Hide();
}
private void pictureBox2_Click(object sender, EventArgs e)
{
this.Close();
}
SqlConnection Con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\HP\Documents\POSDb.mdf;Integrated Security=True;Connect Timeout=30");
private void DisplayProducts()
{
Con.Open();
string Query = "Select * from ProductTbl";
SqlDataAdapter sda = new SqlDataAdapter(Query, Con);
SqlCommandBuilder Builder = new SqlCommandBuilder(sda);
var ds = new DataSet();
sda.Fill(ds);
ProductsDGV.DataSource = ds.Tables[0];
Con.Close();
}
private void button2_Click(object sender, EventArgs e)
{
if ( Key == 0)
{
MessageBox.Show("Select The Product");
}
else
{
try
{
Con.Open();
SqlCommand cmd = new SqlCommand("delete from ProductTbl where PId=@PKey", Con);
cmd.Parameters.AddWithValue("@PKey", Key);
cmd.ExecuteNonQuery(); ;
MessageBox.Show("Product Deleted");
Con.Close();
DisplayProducts();
Reset();
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
}
int Key = 0;
private void ProductsDGV_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
PnameTb.Text = ProductsDGV.SelectedRows[0].Cells[1].Value.ToString();
PCatCb.SelectedItem = ProductsDGV.SelectedRows[0].Cells[2].Value.ToString();
PriceTb.Text = ProductsDGV.SelectedRows[0].Cells[3].Value.ToString();
QtyTb.Text = ProductsDGV.SelectedRows[0].Cells[4].Value.ToString();
if(PnameTb.Text == "")
{
Key = 0;
}
else
{
Key = Convert.ToInt32(ProductsDGV.SelectedRows[0].Cells[0].Value.ToString());
}
}
private void Reset()
{
PnameTb.Text = "";
QtyTb.Text = "";
PriceTb.Text = "";
PCatCb.SelectedIndex = -1;
Key = 0;
}
private void button1_Click(object sender, EventArgs e)
{
if (PnameTb.Text == "" || PCatCb.SelectedIndex == -1 || PriceTb.Text == "" || QtyTb.Text == "")
{
MessageBox.Show("Missing Information");
}
else
{
try
{
Con.Open();
SqlCommand cmd = new SqlCommand("Update ProductTbl set PName=@PN,PCat=@PC,Pprice=@PP,PQty=@PQ where PId=@PKey", Con);
cmd.Parameters.AddWithValue("@PN", PnameTb.Text);
cmd.Parameters.AddWithValue("@PC", PCatCb.SelectedItem.ToString());
cmd.Parameters.AddWithValue("@PP", PriceTb.Text);
cmd.Parameters.AddWithValue("@PQ", QtyTb.Text);
cmd.Parameters.AddWithValue("PKey", Key);
cmd.ExecuteNonQuery(); ;
MessageBox.Show("Product Updated");
Con.Close();
DisplayProducts();
Reset();
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
}
}
}