-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrmProduct.cs
197 lines (175 loc) · 6.6 KB
/
frmProduct.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace pos_and_inventory_csharp
{
public partial class frmProduct : Form
{
SqlConnection cn = new SqlConnection();
SqlCommand cm = new SqlCommand();
DBConnection dbcon = new DBConnection();
SqlDataReader dr;
frmProductList flist;
public frmProduct(frmProductList frm)
{
InitializeComponent();
cn = new SqlConnection(dbcon.MyConnection());
flist = frm;
}
private void frmProduct_Load(object sender, EventArgs e)
{
}
public void LoadCategory()
{
cboCategory.Items.Clear();
cn.Open();
cm = new SqlCommand("select category from tblcategory", cn);
dr = cm.ExecuteReader();
while (dr.Read())
{
cboCategory.Items.Add(dr[0].ToString());
}
dr.Close();
cn.Close();
}
public void LoadBrand()
{
cboBrand.Items.Clear();
cn.Open();
cm = new SqlCommand("select brand from tblbrand", cn);
dr = cm.ExecuteReader();
while (dr.Read())
{
cboBrand.Items.Add(dr[0].ToString());
}
dr.Close();
cn.Close();
}
private void btnCancel_Click(object sender, EventArgs e)
{
Clear();
this.Dispose();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
this.Dispose();
}
private void btnSave_Click(object sender, EventArgs e)
{
try
{
if(MessageBox.Show("Are you sure you want to save this product?", "Save Product", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
string bid = "", cid ="";
cn.Open();
cm = new SqlCommand("Select id from tblBrand where brand like '" + cboBrand.Text + "'", cn);
dr = cm.ExecuteReader();
dr.Read();
if (dr.HasRows) { bid = dr[0].ToString(); }
dr.Close();
cn.Close();
cn.Open();
cm = new SqlCommand("Select id from tblCategory where category like '" + cboCategory.Text + "'", cn);
dr = cm.ExecuteReader();
dr.Read();
if (dr.HasRows) { cid = dr[0].ToString(); }
dr.Close();
cn.Close();
cn.Open();
cm = new SqlCommand("INSERT INTO tblProduct (pcode, barcode, pdesc, bid, cid, price) VALUES (@pcode, @barcode, @pdesc, @bid, @cid, @price)", cn);
cm.Parameters.AddWithValue("@pcode", txtPCODE.Text);
cm.Parameters.AddWithValue("@barcode", txtBarcode.Text);
cm.Parameters.AddWithValue("@pdesc", txtPdesc.Text);
cm.Parameters.AddWithValue("@bid", bid);
cm.Parameters.AddWithValue("@cid", cid);
cm.Parameters.AddWithValue("@price", txtPrice.Text);
cm.ExecuteNonQuery();
cn.Close();
MessageBox.Show("Product has been successfully saved. ");
Clear();
flist.LoadRecords();
}
}
catch (Exception ex)
{
cn.Close();
MessageBox.Show(ex.Message);
}
}
public void Clear()
{
txtPrice.Clear();
txtPdesc.Clear();
txtPCODE.Clear();
txtBarcode.Clear();
cboBrand.Text = "";
cboCategory.Text = "";
txtPCODE.Focus();
btnSave.Enabled = true;
btnUpdate.Enabled = false;
}
private void btnUpdate_Click(object sender, EventArgs e)
{
try
{
if (MessageBox.Show("Are you sure you want to update this product?", "Save Product", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
string bid = "", cid = "";
cn.Open();
cm = new SqlCommand("Select id from tblBrand where brand like '" + cboBrand.Text + "'", cn);
dr = cm.ExecuteReader();
dr.Read();
if (dr.HasRows) { bid = dr[0].ToString(); }
dr.Close();
cn.Close();
cn.Open();
cm = new SqlCommand("Select id from tblCategory where category like '" + cboCategory.Text + "'", cn);
dr = cm.ExecuteReader();
dr.Read();
if (dr.HasRows) { cid = dr[0].ToString(); }
dr.Close();
cn.Close();
cn.Open();
cm = new SqlCommand("UPDATE tblProduct SET barcode = @barcode. pdesc = @pdesc, bid = @bid, cid = @cid, price=@price where pcode like @pcode", cn);
cm.Parameters.AddWithValue("@pcode", txtPCODE.Text);
cm.Parameters.AddWithValue("@barcode", txtBarcode.Text);
cm.Parameters.AddWithValue("@pdesc", txtPdesc.Text);
cm.Parameters.AddWithValue("@bid", bid);
cm.Parameters.AddWithValue("@cid", cid);
cm.Parameters.AddWithValue("@price", txtPrice.Text);
cm.ExecuteNonQuery();
cn.Close();
MessageBox.Show("Product has been successfully updated. ");
Clear();
flist.LoadRecords();
this.Dispose();
}
}
catch (Exception ex)
{
cn.Close();
MessageBox.Show(ex.Message);
}
}
private void txtPrice_KeyPress(object sender, KeyPressEventArgs e)
{
if(e.KeyChar == 46)
{
//accept . character
}else if (e.KeyChar == 8){
//accept backspace
}
else if ((e.KeyChar < 48) || (e.KeyChar> 57)) //ascii code 48 - 57 between 0 - 9
{
e.Handled = true;
}
}
}
}