using System;
using System. Collections.Generic ; using System. Componentltodel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System. hlindows. Forms;
usi.ng System. Data. 5qlC1ient;
using System.IO;
using System . Security. Cryptography;
nillespace Sales_anrl_Inventory_Systen
{ public partial class
frmProduct:
Formt
SqlDataReaderrdr
=nulli
DataTable
dtable
= neu DataTableo;SqlConnection con =
nulli
SglCommand cmd
= nullj
DataTab1e
dt =
n€w DataTable0;ConnectionString cs
=
neu ConnectionStringO;public
frmProducto-
{ InitializeComponentO;private void ) autoo
{ txtProductlD.Text
= -P-' + Cettlniqe(ey(6);
public ) static string Geflhique(ey(irrt u.{irg)
{
char[]
chars =lm
char[62];chars
=' t2y56'r89'.fdrrlrrey0;
byte[]
data= ne Se[l];
R'ECryptoserriceFrari"der
crypto
=ns
ffiCr?IrtoSerwiceProvider ( ) ; cryyto -&thrZeruBytes
(data );
data
= tE btrte[E!6ize];
cqrpto. CeilonZero$rtes (data ) ;
StringBuilder r-sult = ll€r
StrlngBuilder(;axStze) ; for=ach (b3fteb in
data){
result.Append(charsIbX
(chars. Length] I);
return ) result.TostringO
;private l void
frmProduct_Load(object sender, EventArgs e){
btnSave.Enabled =falsej
FiJ.lCombo(
);
Autocomplete(
);
) public void
FillComboo {con = new SqlConnectlon(cs.DBConn);
con.openO;
string ct =
"SELECT RTRII4(CategoryName) from T_Categoryorder
by cmd=
new SqlComand(ct);54
try t
Categoryt{aile";
coagwhtlzed Sa rts fln{ rnve ntory
CmpanlName";
)
con. Close(
);
/ / / I / / / / / I / I / / / I I I / / / / / / I / I
/ select supplier
/ / / / / I / / I / //
con
=
new SqlConnection(cs.DB€onn);con. Open(
);
string
sp=
"SELECT SupplierlD,CompanyName FROM T_SUPPLIERS ORDER BY cmd = new SqlCommand(sp);cmd.Connection
=
con;rdr =
cmd.ExecuteReader0;while
(rdr.Read()){ txtSupplierlD.Text = rdr[9].ToStringO.TrimO;
cmbSupplier. Items. Add ( rdr [ 1 ] ) ;
)
con. Close(
);
catch )
(Exception ex){
ItessageBox. Show (ex. lGssage, "
Error",
llessageBoxButtons-fi ,
l{essageBoxlcon. Error) ;
) )
private void
Reseto {txtProductName.Text
= "";
cmbSubCategory.Text
= "';
cmbCategorY.Text
= "!
txtFeatures.Text
= ";
pictureBoxl.Irage = Pr"operties.lesarces.
Ul;c*SubGategory. Enabled =
fafsci hr0elete.Enabled
=false;
btnllpdate-En$led =
falsc;
frnSave-En*lad =
trr:;
ffioductn-.focusO;
l
prlvate Etu @Cl,'ct(6rcct
sender, EventArgs e){ nescto;
)
private void @Cl-ict(object
sender, EventArgs e)t if (ffiPro*rctlE.Text : -')
{
lbssageBox.Shol('Pleaseenter
product name","Error",
MessageBoxButtons . OX, lhssage8oxlcan . Err.or) ; txtProductllae . Focus ( )
; return;
) if
(crbCategory,Text ==")
{
f,lessageBox.Shor(-Pleaseselect category", "Error",
l,lessageBoxButtons.oK, ItiessageBoxlcon . Error)
;cmbCategory. Focus O ;
return;
55
Coryrutinzd Sa 6s And lnuertorlr
return;
if )
(cmbSuPPlier.Text =="")
{
MessageBox.Shu('Please select Supplier", "Error",
HessageBoxButtons. OK, MessageBoxlcon. Error)
;cmbSupplier. FocusO;
return;
coo =
nff
SqlConnection(cs.DBConn);cm.openO;
string ct =
-SELECT ProductName FRO,I T_PR@UCT urhere Productl{ame='"
+txtProductllae.Text +''';
cd =
neu SqlConmand(ct);cpd.Connection
=
con;rdr
= rmd.ExecuteReadero;if
(rdr.Read()){
MessageBox.Show("Product Name AlreadyExists", "Error",
llessageBoxButtons . OK, MessageBoxf con . E rror ) ; txtProductName.Text
= "";
txtProductName. Focus ( ) ;
if
{
] return;
)
auto();
con = new SqlConnection(cs.DBConn);
con.open(
);
string
cb=
"INSERT INTOT_PRODUCT(ProductfD, Productilame, CategoryID, SubCategorylDr SupplierlD, Featunes, ProductEntry Date,InagerStatus) VALUES
("'+
txtProductlD.Textr ''r"'+
txtProductName.Text* "'r" *
txtCategorylD.Text* "r" *
txtSubCategorylD.Text* o,''
+txtsupplierlD.Text.TrimO
+'
' ,€d1, ' "+DateTime. Now. Date. To5tring( )+" ' ,@2r7)"i
cmd = new SqlComnand(cb);
cmd.Connection
=
conicad . Parameters . Addt'lithValue ( "@d1
", txt
F eatures . Text ) ; HeiloryStream ms=
new HemoryStneamO;Bitnap bmplmage
=
new Bitmap(plctureBoxl.Image);brplrage.Save(ns, System.Drawing. fmaging. fnageFormat. Jpeg) ;
byte[]
data = ms.GetBuffenO;SqlParareter P
= nel SqlParaaeter('ed2',
SqlDbType.I;age);p.Value = datai
nd.p3pafers.Add(p);
d.ErecutetreaderO;
cm.LloseO;
lhssageSor
"
Stu('Successfully saved-,'Record-,
lGssageBoxButtons . OK,MessageBoxlcon .
Inforratim
); turtocapleteO;
btn5ave.Enabled =
false;
)
cat€h (Exception ex)t
l,lessageBox. Shor(ex.ilessage,' Error',
l4essageBoxButtons. OK, MessageBoxlcon. ErPor) ;)
56
) try {
((rdr t=
nuLL)) rdr. Close();
Comf u tnzd Sa rts A nd twn tory
if
(MessageBox.Show("Do youreally xant to delete this recordi",
"Confirmation",
MessageBoxButtons.YesNo, lGssageBoxlcon. Question) ==System .lrlindows . Foms . DialogResult . Yes )
{
delete_records();
l )
' private void
delete_recordso{
+ ...;
int RrsAffected
= g;€on =
neI
SqlConnection(cs.DB€onn)i con.OpenO;string
cq=
'DELETE FROM T_PRODUCT WHERE productlD="' + txtProductlD.Text cnd = neu SqlComrand(cq);cmd.Csnnection
-
con;RusAffected
=
cnd.ExecuteilonQuery0;if
(RowsAffected > 0){
MessageBox.Show("Successfullydeleted"r
"Record", llessageBoxButtons . OK, MessageBoxf con. In'formation ) ;ResetO;
Autocomplete(
);
else
) {
MessageBox. Show( "No Record "found",
"Sorry",
l{essageBoxButtons.fi, }lessageBoxlcon . Information ) ;ResetO;
AutocompleteO;
) if
(con.State == ConnectionState.Open)t
con. CloseO;)
) catch
(Exception ex)t
liessageBox. Sho*( er.llessage,' Eppor',
lhssagsSoGrttms-fi ,
thssageBoxlcon. Error) ;
) )
private void ArtocoTleteo
t tqr
{ cdr = lE Sqfcf,rEstifil(cs.Ern);
cm.OpcnO;
SqlCEd d
=tE
SqIC0Ed('SELECTdistinct
Producttlame FROMT_PROD||CT', con);
DataSet ds =
rE Dat6ct0;
SqfDataMryter da =
rx
SqUfatafdapter(crd);da.Fill(ds, 'ffirt');
AutoCorpleteStringCollectlon Gol
=
n€r Auto(ompleteStringCollectlon ( ) ;inti=6;
for (i
= O;i
<= ds.Tables[O].Rus.Count- 1;
i++){
col.Md (ds.Tables[o] . Rous[1] ['productname" ] .ToStrine( ));
57
try
{
Corryrutiizdsafi"s fln{ lrwmtoryr
] catch
(Exception ex){ llessageBox.Show(ex.tlessage,'Error", MessageBoxButtons.OK, I'iessageBoxlcon.
Eror)
;l )
private void
btnUpdate_Click(object sender, EventArgs e){ if
(txtProductilane.Text == oo){
llessageBox.Sho,tr("Pleaseenter
product name","Error",
tles sageBordutton s -
O(,
ilessageBoxl con . E rror ) ; trGProductllare. Foc us ( ) ; retuPn;if )
(cabCategorY.Text =="*)
{
l,lessageBox.Shoul("Pleaseselect category"r "Error",
MessageBoxButtons.il, HessageBoxlcon. Error) ;cmbCategory. FocusO;
return;
) if
(cmbSubCategorY.Text =="")
{
MessageBox.show("Pleaseselect
subcategory", "Error',
l4essageBoxButtons . OK, Mess ageBoxlcon . E
rror)
;cmbSubCategory. Focus( ) ;
return;
if )
(cmbSuPPlier.Text =="")
t
MessageBox.Show("Pleaseselect Suppller','Emor',
kssaEeBo)Outtons.0(, MessageBoxlcon. Emor) ;cmbSupplier. Focus (
);
return;
con
=
nes SqlConnection(cs.lEfonn);con.@enO;
string
cb=
'UPDATE T_PflfinCTset Prod{rctilc''
+ ffiPrcductilaee.Text'',Categoryllts'
+txtcategorylD.Text + ',SubGategoryllt'
+ffiubCategoryID.Text
+',Supplierlf "+txtSupplierlD. Text+'',
Features=fdl,Iegefl IErc MuctIIts' -
+ffiProdrctlD.Tert + ''';
r-l
=ner Sqtcrard(cb);
d.Csnectisr =
cqr;d
-Par-ters-ldfliUrflal,:('$n', ftFGrtrlres.Text)
;lhryftr€n
Gs= rEr ,ffitrer0;
Bifap bTIrye = rEl B:ir?(pictrffixl.Lage);
hTr:.
- SaG(rs,
Slrstet -fadig. rryi
ng. IrageForrat . I peg ) ;EetI
data =r.€ctLrFfr0;
SqLParGr
P= rE 5{fP1r2'16j('0d2',
SqlllbType.lage);p.Value = data;
crd. Parreters.Add(p)
;
cnd. ExecuteRea&rO;
con.CloseO;
tilessageBor.Stra('Successfully
updated',
"Record", l.,tessageBoxButtons.OK, MessageBoxlcon. f nfonaation) ;Autocorplete(
);
btnUpdate.Enabled =
false;
I
aa+ti lEvtca+iaa avt58