-
Notifications
You must be signed in to change notification settings - Fork 0
/
Laptopinventory.aspx.cs
309 lines (234 loc) · 11.1 KB
/
Laptopinventory.aspx.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
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace DB_Prpject
{
public partial class Laptopinventory : System.Web.UI.Page
{
static string global_filepath;
protected void Page_Load(object sender, EventArgs e)
{
}
// // go button click
protected void Button4_Click(object sender, EventArgs e)
{
getLaptopByID();
}
// // update button click
protected void Button3_Click(object sender, EventArgs e)
{
updateLaptopByID();
}
// // delete button click
protected void Button2_Click(object sender, EventArgs e)
{
deleteLaptopByID();
}
// // add button click
protected void Button1_Click(object sender, EventArgs e)
{
if (checkIfLaptopExists())
{
Response.Write("<script>alert('Laptop Already Exists, try some other Laptop ID');</script>");
}
else
{
addNewLaptop();
}
}
// // user defined functions
//void Viewlaptop()
//{
// string strconnect = "Data Source=AKKHAN;Initial Catalog=dbproject;Integrated Security=True";
// SqlConnection con = new SqlConnection(strconnect);
// con.Open();
// SqlCommand cmd = new SqlCommand ("SELECT pid as Product_ID,Laptop_ID,Laptop_Name,Generation, Brand,Proceesor,RAM,SSD,HDD,i.Stock,Price,Laptop_Pic,sellerID from laptop as l join Inventory as i on l.pid = i.productID WHERE l.Laptop_Id ='" + TextBox1.Text.Trim() + "' AND i.sellerID=@lid;", con);
// cmd.Parameters.AddWithValue("@lid", Session["login_id"].ToString());
// SqlDataAdapter da = new SqlDataAdapter(cmd);
// DataTable dt = new DataTable();
// da.Fill(dt);
// cmd.ExecuteNonQuery();
// GridView1.DataBind();
// con.Close();
//}
void deleteLaptopByID()
{
if (checkIfLaptopExists())
{
try
{
// string strconnect = "Data Source=AKKHAN;Initial Catalog=dbproject;Integrated Security=True";
string strconnect = "Data Source=AKKHAN;Initial Catalog=dbproject;Integrated Security=True";
SqlConnection con = new SqlConnection(strconnect);
con.Open();
SqlCommand cmd = new SqlCommand("DELETE from laptop WHERE Laptop_ID='" + TextBox1.Text.Trim() + "'", con);
cmd.ExecuteNonQuery();
con.Close();
Response.Write("<script>alert('Laptops Deleted Successfully');</script>");
// GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write("<script>alert('" + ex.Message + "');</script>");
}
}
else
{
Response.Write("<script>alert('Invalid Laptop ID');</script>");
}
}
void updateLaptopByID()
{
if (checkIfLaptopExists())
{
try
{
string filepath = "~/laptopimajes/laptop";
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
if (filename == "" || filename == null)
{
filepath = global_filepath;
}
else
{
FileUpload1.SaveAs(Server.MapPath("laptopimajes/" + filename));
filepath = "~/laptopimajes/" + filename;
}
string strconnect = "Data Source=AKKHAN;Initial Catalog=dbproject;Integrated Security=True";
SqlConnection con = new SqlConnection(strconnect);
con.Open();
SqlCommand cmd = new SqlCommand("UPDATE laptop set Laptop_Name=@Laptop_Name, Stock=@Stock, Brand=@Brand, Generation=@Generation, Proceesor=@Proceesor, RAM=@RAM, HDD=@HDD, SSD=@SSD, Price=@Price where Laptop_ID='" + TextBox1.Text.Trim() + "'", con);
cmd.Parameters.AddWithValue("@Laptop_ID", TextBox1.Text.Trim());
cmd.Parameters.AddWithValue("@Laptop_Name", TextBox2.Text.Trim());
cmd.Parameters.AddWithValue("@Stock", TextBox3.Text.Trim());
cmd.Parameters.AddWithValue("@Brand", DropDownList2.SelectedItem.Value);
cmd.Parameters.AddWithValue("@Generation", DropDownList1.SelectedItem.Value);
cmd.Parameters.AddWithValue("@Proceesor", TextBox8.Text.Trim());
cmd.Parameters.AddWithValue("@RAM", TextBox4.Text.Trim());
cmd.Parameters.AddWithValue("@HDD", TextBox9.Text.Trim());
cmd.Parameters.AddWithValue("@SSD", TextBox10.Text.Trim());
cmd.Parameters.AddWithValue("@Price", TextBox6.Text.Trim());
cmd.Parameters.AddWithValue("@Laptop_Pic", filepath);
cmd.Parameters.AddWithValue("@linklaptop", "NULL");
//Response.Write("<script>alert('Laptop Updated Successfully 1');</script>");
cmd.ExecuteNonQuery();
SqlCommand cnd = new SqlCommand("update inventory set Stock = @st where productID = (select pid from laptop where Laptop_ID= @sid)", con);
cnd.Parameters.AddWithValue("@st", TextBox3.Text.Trim());
cnd.Parameters.AddWithValue("@sid", TextBox1.Text.Trim());
cnd.ExecuteNonQuery();
con.Close();
// GridView1.DataBind();
Response.Write("<script>alert('Laptop Updated Successfully');</script>");
Response.Redirect("Laptopinventory.aspx");
}
catch (Exception ex)
{
Response.Write("<script>alert('" + ex.Message + "');</script>");
}
}
else
{
Response.Write("<script>alert('Invalid Laptop ID');</script>");
}
}
void getLaptopByID()
{
try
{
string strconnect = "Data Source=AKKHAN;Initial Catalog=dbproject;Integrated Security=True";
SqlConnection con = new SqlConnection(strconnect);
con.Open();
SqlCommand cmd = new SqlCommand("SELECT pid as Product_ID,Laptop_ID,Laptop_Name,Generation, Brand,Proceesor,RAM,SSD,HDD,i.Stock,Price,Laptop_Pic,sellerID from laptop as l join Inventory as i on l.pid = i.productID WHERE l.Laptop_Id ='" + TextBox1.Text.Trim() + "' AND i.sellerID=@lid;", con);
cmd.Parameters.AddWithValue("@lid", Session["login_id"].ToString());
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count >= 1)
{
TextBox2.Text = dt.Rows[0]["Laptop_name"].ToString();
TextBox8.Text = dt.Rows[0]["Proceesor"].ToString();
TextBox3.Text = dt.Rows[0]["Stock"].ToString();
TextBox9.Text = dt.Rows[0]["HDD"].ToString();
TextBox10.Text = dt.Rows[0]["SSD"].ToString();
TextBox6.Text = dt.Rows[0]["Price"].ToString();
TextBox4.Text = dt.Rows[0]["RAM"].ToString();
DropDownList1.SelectedValue = dt.Rows[0]["Generation"].ToString().Trim();
DropDownList2.SelectedValue = dt.Rows[0]["Brand"].ToString().Trim();
global_filepath = dt.Rows[0]["Laptop_Pic"].ToString();
}
else
{
Response.Write("<script>alert('Invalid Laptop ID');</script>");
}
}
catch (Exception ex)
{
}
}
bool checkIfLaptopExists()
{
try
{
string strconnect = "Data Source=AKKHAN;Initial Catalog=dbproject;Integrated Security=True";
SqlConnection con = new SqlConnection(strconnect);
con.Open();
SqlCommand cmd = new SqlCommand("SELECT * from laptop where Laptop_ID='" + TextBox1.Text.Trim() + "' ;", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count >= 1)
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
Response.Write("<script>alert('" + ex.Message + "');</script>");
return false;
}
}
void addNewLaptop()
{
string strconnect = "Data Source=AKKHAN;Initial Catalog=dbproject;Integrated Security=True";
SqlConnection con = new SqlConnection(strconnect);
con.Open();
string filepath = "~/laptopimajes/laptop.jpg";
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
//FileUpload1.SaveAs(Server.MapPath("laptopimajes/" + filename));
filepath = "~/laptopimajes/" + filename;
SqlCommand cmd = new SqlCommand("AddToinventory", con);
cmd.Parameters.AddWithValue("@Laptop_ID", TextBox1.Text.Trim());
cmd.Parameters.AddWithValue("@Laptop_Name", TextBox2.Text.Trim());
cmd.Parameters.AddWithValue("@Stock", TextBox3.Text.Trim());
cmd.Parameters.AddWithValue("@Brand", DropDownList2.SelectedItem.Value);
cmd.Parameters.AddWithValue("@Generation", DropDownList1.SelectedItem.Value);
cmd.Parameters.AddWithValue("@Proceesor", TextBox8.Text.Trim());
cmd.Parameters.AddWithValue("@RAM", TextBox4.Text.Trim());
cmd.Parameters.AddWithValue("@HDD", TextBox9.Text.Trim());
cmd.Parameters.AddWithValue("@SSD", TextBox10.Text.Trim());
cmd.Parameters.AddWithValue("@Price", TextBox6.Text.Trim());
cmd.Parameters.AddWithValue("@Laptop_Pic", filepath);
cmd.Parameters.AddWithValue("@sellerID", Session["login_id"].ToString());
cmd.CommandType = CommandType.StoredProcedure;
cmd.ExecuteNonQuery();
con.Close();
Response.Write("<script>alert('Laptop added successfully.');</script>");
// GridView1.DataBind();
}
protected void LinkButton14_Click(object sender, EventArgs e)
{
Response.Redirect("admininventory.aspx");
}
}
}