-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfmProduct.cs
461 lines (371 loc) · 14.9 KB
/
fmProduct.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
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;
using System.Drawing;
namespace Nazam_pos
{
public partial class fmProduct : Form
{
SqlConnection cn = new SqlConnection();
SqlCommand cm = new SqlCommand();
DBconnection dbcon = new DBconnection();
SqlDataReader dr;
fmproductlist flist;
string title = "Inventory Products";
public fmProduct(fmproductlist frm)
{
InitializeComponent();
cn = new SqlConnection(dbcon.Myconnection());
flist = frm;
}
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 fmProduct_Load(object sender, EventArgs e)
{
LinkLabelLinkClickedEventArgs ex = new LinkLabelLinkClickedEventArgs(linkLabel1.Links[0]);
linkLabel1_LinkClicked(sender, ex);
pcode_increment();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
this.Dispose();
}
private void BTNSave_Click(object sender, EventArgs e)
{
bool b = false;
try
{
errorProvider1.Clear();
if (textpurchase.Text == string.Empty)
{
//MessageBox.Show("Please Enter Purchase Price ", title, MessageBoxButtons.OK, MessageBoxIcon.Warning);
errorProvider1.SetError(this.textpurchase, "Please Enter Purchase Price");
textpurchase.Focus();
return;
}
if (textqty.Text == string.Empty)
{
MessageBox.Show("Please Enter Product Quantity ", title, MessageBoxButtons.OK, MessageBoxIcon.Warning);
textqty.Focus();
return;
}
cn.Open();
cm = new SqlCommand("select barcode from tblProducts where barcode ='" + textbarcode.Text + "'", cn);
dr = cm.ExecuteReader();
dr.Read();
if (dr.HasRows)
{
b = true;
}
else
{
b = false;
}
cn.Close();
dr.Close();
if (b == true)
{
MessageBox.Show("Already Product exist ?", title, MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
if (MessageBox.Show("Are you sure you want to save this Product? ", "Saving Product ", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
string bid = " ";
string 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();
// category id as fetch in category table
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 tblProducts (pcode,barcode,pdesc,bid, cid,pname ,purchase,price, qty ,reorder) VALUES(@pcode, @barcode, @pdesc,@bid, @cid,@pname ,@purchase,@price, @qty ,@reorder)", cn);
cm.Parameters.AddWithValue("@pcode", textpcode.Text);
cm.Parameters.AddWithValue("@barcode", textbarcode.Text);
cm.Parameters.AddWithValue("@pdesc", textpdesc.Text);
cm.Parameters.AddWithValue("@bid", bid);
cm.Parameters.AddWithValue("@cid", cid);
cm.Parameters.AddWithValue("@pname", textpname.Text);
cm.Parameters.AddWithValue("@purchase", double.Parse(textpurchase.Text));
cm.Parameters.AddWithValue("@price", double.Parse(textprice.Text));
cm.Parameters.AddWithValue("@qty", int.Parse(textqty.Text));
cm.Parameters.AddWithValue("@reorder", int.Parse(txtReorder.Text));
//cm.Parameters.AddWithValue("@piece", int.Parse(txtpiece.Text));
// cm.Parameters.AddWithValue("@issuedate", dateTimePicker1.Value);
// cm.Parameters.AddWithValue("@expirydate", dateTimePicker2.Value);
cm.ExecuteNonQuery();
cn.Close();
MessageBox.Show(" Product has beeen Successfully saved .");
Clear();
flist.LoadRecords();
}
}
}
catch (Exception ex)
{
cn.Close();
MessageBox.Show(ex.Message, title, MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
public void Clear()
{
textprice.Clear();
textpdesc.Clear();
textpcode.Clear();
textbarcode.Clear();
textpurchase.Clear();
textqty.Clear();
cboBrand.Text = " ";
cboCategory.Text = " ";
textpcode.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? ", "Saving Product ", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
string bid = " ";
string 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();
// category id as fetch in category table
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();
// new Statement
cn.Open();
cm = new SqlCommand("UPDATE tblProducts SET barcode=@barcode, pdesc=@pdesc,bid=@bid, cid=@cid, pname=@pname,purchase=@purchase ,price=@price , qty=@qty , reorder=@reorder where pcode like @pcode", cn);
cm.Parameters.AddWithValue("@pcode", textpcode.Text);
cm.Parameters.AddWithValue("@barcode", textbarcode.Text);
cm.Parameters.AddWithValue("@pdesc", textpdesc.Text);
cm.Parameters.AddWithValue("@bid", bid);
cm.Parameters.AddWithValue("@cid", cid);
cm.Parameters.AddWithValue("@pname", textpname.Text);
cm.Parameters.AddWithValue("@barcode", textbarcode.Text);
cm.Parameters.AddWithValue("@purchase", double.Parse(textpurchase.Text));
cm.Parameters.AddWithValue("@price", double.Parse(textprice.Text));
cm.Parameters.AddWithValue("@qty", int.Parse(textqty.Text));
cm.Parameters.AddWithValue("@reorder",int.Parse( txtReorder.Text));
// cm.Parameters.AddWithValue("@piece", int.Parse(txtpiece.Text));
//cm.Parameters.AddWithValue("@issuedate", dateTimePicker1.Value);
//cm.Parameters.AddWithValue("@expirydate", dateTimePicker2.Value);
cm.ExecuteNonQuery();
cn.Close();
MessageBox.Show(" Product has beeen Successfully Updated.");
Clear();
flist.LoadRecords();
this.Dispose();
}
}
catch (Exception ex)
{
cn.Close();
MessageBox.Show(ex.Message, title, MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void button1_Click(object sender, EventArgs e)
{
Clear();
}
private void textprice_KeyPress(object sender, KeyPressEventArgs e)
{
if(e.KeyChar== 48)
{
//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;
}
}
private void textprice_TextChanged(object sender, EventArgs e)
{
}
private void label8_Click(object sender, EventArgs e)
{
}
private void txtReorder_TextChanged(object sender, EventArgs e)
{
}
private void textqty_TextChanged(object sender, EventArgs e)
{
}
private void textpurchase_TextChanged(object sender, EventArgs e)
{
}
private void cboBrand_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = cboBrand.SelectedItem.ToString();
}
private void cboCategory_SelectedIndexChanged(object sender, EventArgs e)
{
textBox2.Text = cboCategory.SelectedItem.ToString();
}
private void label3_Click(object sender, EventArgs e)
{
}
private void textpurchase_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsNumber(e.KeyChar) & (Keys)e.KeyChar != Keys.Back & e.KeyChar != '.')
{
e.Handled = true;
}
}
private void textqty_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsNumber(e.KeyChar) & (Keys)e.KeyChar != Keys.Back & e.KeyChar != '.')
{
e.Handled = true;
}
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Random rnd = new Random();
textpcode.Clear();
// for (int i=100;i< 500000000;i++)
//{
// textpcode.Text += i;
textpcode.Text += rnd.Next(1000, 500000000);
//}
}
private void textbarcode_Leave(object sender, EventArgs e)
{
if(string.IsNullOrEmpty(textbarcode.Text))
{
textbarcode.Focus();
errorProvider1.SetError(this.textbarcode, "Please Enter Barcode");
//MessageBox.Show("Please Enter Barcode");
}
else
{
//errorProvider1.Icon= Properties.Resources.icons8_checked_checkbox_24x24;
errorProvider1.Clear();
}
//if (textbarcode.Text == string.Empty)
//{
//MessageBox.Show("Please Enter Barcode No ", title, MessageBoxButtons.OK, MessageBoxIcon.Warning);
//textbarcode.Focus();
// textbarcode.Focus();
//errorProvider1.SetError(this.textbarcode, "Please Enter Barcode No");
//}
//else
//{
// errorProvider1.Clear();
//}
}
private void textbarcode_TextChanged(object sender, EventArgs e)
{
}
private void textbarcode_KeyPress(object sender, KeyPressEventArgs e)
{
// if (!char.IsNumber(e.KeyChar) & (Keys)e.KeyChar != Keys.Back & e.KeyChar != '.')
//{
// e.Handled = true;
//}
}
private void textpurchase_Leave(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textpurchase.Text))
{
textpurchase.Focus();
errorProvider1.SetError(this.textpurchase, "Please Enter price in number format[1000] ");
//MessageBox.Show("Please Enter Barcode");
}
else
{
//errorProvider1.Icon= Properties.Resources.icons8_checked_checkbox_24x24;
errorProvider1.Clear();
}
}
public void pcode_increment()
{
string sdate = DateTime.Now.ToString("yyyyMMdd");
string transno;
int count;
transno = "pcoo";
//transno = dr[0].ToString();
//count = int.Parse(transno.Substring(8, 4));
count = 0;
textpcode.Text = sdate + (count + 1);
//transno = sdate + "1001";
//lblTransno.Text = transno;
}
private void textbarcode_Enter(object sender, EventArgs e)
{
//if(textpcode.Text== "scan product here")
//{
// textpcode.Text="";
//}
//else
//{
//}
}
}
}