/
BrowseProduct.aspx.cs
324 lines (257 loc) · 11.3 KB
/
BrowseProduct.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.ServiceModel.Web;
using System.Web.Services;
using System.Web.UI.HtmlControls;
public partial class BrowseProduct : System.Web.UI.Page
{
string rows;
string rows1;
public double max = 0.00;
int nop = 3;
protected void Page_Load(object sender, EventArgs e)
{
getCountries();
if (Session["userid"] != null)
{
int customerId = (int)Session["userid"];
Product[] productCart = ShopDataAccess.GetCart(customerId);
Session["productCart"] = productCart;
}
if (Session["product"] != null)
{
Session.Remove("product");
}
if (Session["country"] != null)
{
CreateProductTable();
// string country=(string)Session["country"];
// getCategories(country);
}
else
{
Session["datatable"]=CreateAllProductTable();
Session.Remove("datatable");
}
}
private DataTable CreateAllProductTable()
{
DbCommand comm = GenericDataAccess.CreateCommand();
string country = (string)(Session["country"]);
string countryn = (string)(Session["countryname"]);
// set the stored procedure name
comm.CommandText = "SELECT Product.productName,Product.productShortDesc,Product.productPrice,Product.productImagePath,Country.countryName,Vendor.vendorName,Product.productID FROM dbo.Product INNER JOIN dbo.Country ON Product.productCountryId=Country.countryId INNER JOIN dbo.Vendor ON Product.vendorId=Vendor.vendorId;";
comm.CommandType = CommandType.Text;
comm.Connection.Open();
// Execute the command and save the results in a DataTable
DbDataReader reader = comm.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(reader);
DbDataReader reader1 = dt.CreateDataReader();
String table = "<h3 style='margin-left:200px'>Products from All over the World</h3>";
int nop1 = nop;
while (reader1.Read())
{
if (nop1 == 0)
{
nop1 = nop;
rows += "<br />";
}
string PName = reader1.GetString(0);
string PSDesc = reader1.GetString(1);
double PPrice = reader1.GetDouble(2);
if (PPrice > max)
{
max = PPrice;
}
string PImg = reader1.GetString(3);
string vendor = reader1.GetString(5);
int Pid = reader1.GetInt32(6);
rows += "<div class='prodtable'><img src=" + PImg + " height=100 width=100 /><br /><b>Name: </b>" + PName + " <br /><b>Vendor: </b>" + vendor + " <br /><b>Price: </b>" + PPrice + "<br /><br /><a class=hButton href='Shopping.aspx?productId="+Pid+"'>View Product</a><br /></div>";
nop1--;
}
reader.Close();
tableproduct.InnerHtml =table+rows ;
return dt;
}
private void CreateProductTable()
{
// DataTable dt1 = new DataTable();
string country = (string)(Session["country"]);
string countryn = (string)(Session["countryname"]);
// set the stored procedure name
/* comm.CommandText = "SELECT productName,productShortDesc,productPrice,productImagePath FROM dbo.Product where productCountryId='" + country + "';";
comm.CommandType = CommandType.Text;
comm.Connection.Open();*/
// Execute the command and save the results in a DataTable
DataTable table1 = ShopDataAccess.GetProducts(country);
DbDataReader reader = table1.CreateDataReader();
String table = "<h3 style='margin-left:200px'>Products from " + countryn + "</h3>";
int nop1 = nop;
string rows = null;
while (reader.Read())
{
if (nop1 == 0)
{
nop1 = nop;
rows += "<br />";
}
string PName = reader.GetString(0);
string PSDesc = reader.GetString(1);
double PPrice = reader.GetDouble(2);
if (PPrice > max)
{
max = PPrice;
}
string PImg = reader.GetString(3);
string vendor = reader.GetString(5);
int Pid = reader.GetInt32(6);
rows += "<div class='prodtable'><img src=" + PImg + " height=100 width=100 /><br /><b>Name: </b>" + PName + " <br /><b>Vendor: </b>" + vendor + " <br /><b>Price: </b>" + PPrice + "<br /><br /><a class='hButton' href='Shopping.aspx?productId=" + Pid + "'>View Product</a><br /></div>";
nop1--;
}
tableproduct.InnerHtml = table + rows;
Session.Remove("country");
Session.Remove("countryname");
}
public string getCountries()
{
DataTable table1 = ShopDataAccess.GetCountries();
DbDataReader reader = table1.CreateDataReader();
String dt = null;
while (reader.Read())
{
int countryId = reader.GetInt32(0);
string countryName = reader.GetString(1);
if (countryId.Equals((string)Session["country"]))
{
rows1 += "<tr class=prodtable><td style='width:10%;'><input type=radio id=country runat=server selected=selected name=selcountry value=" + countryId + " /></td><td>" + countryName + "</td></tr>";
}
else
{
rows1 += "<tr class=prodtable><td style='width:10%;'><input type=radio id=country runat=server name=selcountry value=" + countryId + " /></td><td>" + countryName + "</td></tr>";
}
}
string table=" <table runat=server id=countryTable align=center style='border: thin solid black;width:800px;color:black;'>" + rows1+"</table>";
Literalcountry.InnerHtml = " <table runat=server id=countryTable align=center style='border: thin solid black;width:200px;color:black;'>" + rows1+"</table>";
return table;
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string getCategories(String countryid)
{
DataTable table1 = ShopDataAccess.GetCategories(countryid);
DbDataReader reader = table1.CreateDataReader();
String dt = null;
string rows2 = null;
while (reader.Read())
{
int categoryId = reader.GetInt32(0);
string categoryName = reader.GetString(1);
rows2 += "<tr class=prodtable><td style='width:10%;'><input type=radio id=category runat=server name=selcategory value=" + categoryId + " onchange='onCatSelect()' /></td><td>" + categoryName + "</td></tr>";
}
// tablecategory.InnerHtml = " <table runat=server id=categoryTable align=center style='border: thin solid black;width:600px;color:black;'>" + rows2 + "</table>";
string table2 = "<p style='font-size:medium;background-color:#FF6600'> Select a Category</p> <table runat=server id=categoryTable align=center style='border: thin solid black;width:200px;color:black;'>" + rows2 + "</table>";
return table2;
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string getProducts(String countryid)
{
DataTable tableproducts = ShopDataAccess.GetProducts(countryid);
DbDataReader reader = tableproducts.CreateDataReader();
int nop = 3;
string countryn = null;
string rows2 = null;
int nop1 = nop;
while (reader.Read())
{
if (nop1 == 0)
{
nop1 = nop;
rows2 += "<br />";
}
string PName = reader.GetString(0);
string PSDesc = reader.GetString(1);
double PPrice = reader.GetDouble(2);
string PImg = reader.GetString(3);
countryn = reader.GetString(4);
string vendor = reader.GetString(5);
int Pid = reader.GetInt32(6);
rows2 += "<div class='prodtable'><img src=" + PImg + " height=100 width=100 /><br /><b>Name: </b>" + PName + " <br /><b>Vendor: </b>" + vendor + " <br /><b>Price: </b>" + PPrice + "<br /><br /><a class=hButton href='Shopping.aspx?productId=" + Pid + "'>View Product</a><br /></div>";
nop1--;
}
String table = "<h3 style='margin-left:200px'>Products from " + countryn + "</h3>";
table = table + rows2;
Console.WriteLine(table);
// HttpContext.Current.Response.ContentType = "application/json";
// JavaScriptSerializer JavaScriptConvert = new JavaScriptSerializer();
// string json = JavaScriptConvert.Serialize(table);
return table;
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string getProductsConCat(String concat)
{
string[] concatnew = concat.Split(',');
string query = null;
int nop = 3;
if (concatnew[0]!="")
{
query = "Product.productCountryId='" + concatnew[0]+"'";
}
if (concatnew[1] != "")
{
if (concatnew[1]!=null && concatnew[0]!=null)
{
query = query +" AND Product.categoryId='" + concatnew[1]+"'";
}
}
query = query + " AND Product.productPrice<" + concatnew[2];
DataTable tableproducts = ShopDataAccess.GetProductsConCat(query);
DbDataReader reader = tableproducts.CreateDataReader();
string countryn = null;
string rows2 = null;
String table = null;
if (tableproducts!=null)
{
int nop1 = nop;
while (reader.Read())
{
if (nop1 == 0)
{
nop1 = nop;
rows2 += "<br />";
}
string PName = reader.GetString(0);
string PSDesc = reader.GetString(1);
double PPrice = reader.GetDouble(2);
string PImg = reader.GetString(3);
countryn = reader.GetString(4);
string vendor = reader.GetString(5);
int Pid = reader.GetInt32(6);
rows2 += "<div class='prodtable'><img src=" + PImg + " height=100 width=100 /><br /><b>Name: </b>" + PName + " <br /><b>Vendor: </b>" + vendor + " <br /><b>Price: </b>" + PPrice + "<br /><br /><a class=hButton href='Shopping.aspx?productId=" + Pid + "'>View Product</a><br /></div>";
nop1--;
}
table = "<h3 style='margin-left:200px'>Products from " + countryn + "</h3>";
table = table + rows2;
}
else
{
table = "No Results";
}
Console.WriteLine(table);
// HttpContext.Current.Response.ContentType = "application/json";
// JavaScriptSerializer JavaScriptConvert = new JavaScriptSerializer();
// string json = JavaScriptConvert.Serialize(table);
return table;
}
}