Guest User

Untitled

a guest
Jan 6th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.15 KB | None | 0 0
  1. CREATE PROCEDURE AdminViewProducts
  2. @PageIndex INTEGER = 1,
  3. @PageSize INTEGER = 10,
  4. @RecordCount INTEGER OUTPUT
  5. AS
  6. BEGIN
  7. SET NOCOUNT ON;
  8. SELECT ROW_NUMBER()
  9. OVER
  10. (
  11. ORDER BY Product_Category_Name
  12. )AS RowNumber,
  13. Product_No,
  14. Product_Image,
  15. Product_Name,
  16. Barcode_No,
  17. Product_Category_Name,
  18. Product_Price,
  19. Product_Quantity,
  20. Grocery_Branch_Name
  21. INTO #Results
  22. FROM Product,Product_Category,Grocery_Branch
  23. WHERE Product_Category.Product_Category_No = Product.Product_Category_No
  24. AND Grocery_Branch.Grocery_Branch_No = Product.Grocery_Branch_No
  25.  
  26. SELECT * FROM #Results
  27. WHERE RowNumber BETWEEN(@PageIndex -1) * @PageSize + 1
  28. AND
  29. (((@PageIndex -1) * @PageSize + 1) + @PageSize) - 1
  30.  
  31. SELECT @RecordCount = COUNT(*) FROM #Results
  32.  
  33. DROP TABLE #Results
  34. END
  35.  
  36.  
  37. CREATE PROCEDURE AdminSearchProduct
  38. @ProductName VARCHAR(50),
  39. @PageIndex INTEGER = 1,
  40. @PageSize INTEGER = 10,
  41. @RecordCount INTEGER OUTPUT
  42. AS
  43. BEGIN
  44. SET NOCOUNT ON;
  45. SELECT ROW_NUMBER() OVER
  46. (
  47. ORDER BY Product_Name
  48. )
  49. AS RowNumber,
  50. Product_No,
  51. Product_Image,
  52. Product_Name,
  53. Barcode_No,
  54. Product_Category_Name,
  55. Product_Price,
  56. Product_Quantity,
  57. Grocery_Branch_Name
  58. INTO #Results
  59. FROM Product,Product_Category,Grocery_Branch
  60. WHERE Product_Category.Product_Category_No = Product.Product_Category_No
  61. AND Grocery_Branch.Grocery_Branch_No = Product.Grocery_Branch_No
  62. AND Product_Name LIKE '%' + @ProductName + '%'
  63.  
  64. SELECT * FROM #Results
  65. WHERE RowNumber BETWEEN(@PageIndex -1) * @PageSize + 1
  66. AND
  67. (((@PageIndex -1) * @PageSize + 1) + @PageSize) - 1
  68.  
  69. SELECT @RecordCount = COUNT(*) FROM #Results
  70.  
  71. DROP TABLE #Results
  72. END
  73.  
  74. <!DOCTYPE html>
  75.  
  76. <html xmlns="http://www.w3.org/1999/xhtml">
  77. <head runat="server">
  78. <title></title>
  79. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  80. <link href="CSS/searchtext.css" rel="stylesheet" />
  81. <link href="CSS/searchproducts.css" rel="stylesheet" />
  82.  
  83. <form id="form1" runat="server">
  84.  
  85. <main>
  86. <div class="content-wrap">
  87. <h4 class="searchtext">
  88. <strong>Search by Product Name</strong>
  89. </h4>
  90.  
  91. <div class="searchproducts">
  92. <asp:Table ID="Table1" runat="server" align="center">
  93. <asp:TableRow ID="TableRow1" runat="server">
  94. <asp:TableCell HorizontalAlign="Right">
  95. <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
  96. </asp:TableCell>
  97. <asp:TableCell HorizontalAlign="Left">
  98. <asp:Button ID="Button8" runat="server" OnClick="Search_Product" ValidationGroup="SearchByName" Text="Search Product" />
  99. <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ValidationGroup="SearchByName" ErrorMessage=" Please enter a product name" Font-Bold="True"></asp:RequiredFieldValidator>
  100. <asp:Label ID="Label3" runat="server" Font-Bold="True"></asp:Label>
  101. </asp:TableCell>
  102. </asp:TableRow>
  103. </asp:Table>
  104. </div>
  105.  
  106. <br />
  107. <br />
  108.  
  109. <div style="margin-left: 265px;">
  110. <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" HorizontalAlign="Center" HeaderStyle-BackColor="#00a400" HeaderStyle-ForeColor="White">
  111. <Columns>
  112. <asp:BoundField DataField="Product_No" HeaderText="Product No" />
  113. <asp:TemplateField HeaderText="Image">
  114. <ItemTemplate>
  115. <asp:Image ID="Image1" runat="server" Height="100px" Width="150px"
  116. ImageUrl='<%#"data:Image/png/jpg/jpeg/gif/bmp;base64," + Convert.ToBase64String((byte[])Eval("Product_Image")) %>' />
  117. </ItemTemplate>
  118. </asp:TemplateField>
  119. <asp:BoundField DataField="Product_Name" HeaderText="Product" />
  120. <asp:BoundField DataField="Barcode_No" HeaderText="Barcode" />
  121. <asp:BoundField DataField="Product_Category_Name" HeaderText="Category" />
  122. <asp:BoundField DataField="Product_Price" HeaderText="Price" DataFormatString="{0:0.00} AUD" />
  123. <asp:BoundField DataField="Product_Quantity" HeaderText="Quantity" />
  124. <asp:BoundField DataField="Grocery_Branch_Name" HeaderText="Branch" />
  125. </Columns>
  126. </asp:GridView>
  127.  
  128. <br />
  129. <table style="align-content:center; width:800px;">
  130. <tr>
  131. <td>
  132. <asp:Repeater ID="rptPager" runat="server">
  133. <ItemTemplate>
  134. <asp:LinkButton ID="lnkPage" runat="server" Text='<%#Eval("Text") %>' CommandArgument='<%# Eval("Value") %>' Enabled='<%# Eval("Enabled") %>' OnClick="lnkbtn_PageIndexChanged" ForeColor="#00a400"></asp:LinkButton>
  135. </ItemTemplate>
  136. </asp:Repeater>
  137. </td>
  138. </tr>
  139. </table>
  140.  
  141. </div>
  142.  
  143. </div>
  144. </main>
  145.  
  146. </body>
  147. </html>
  148.  
  149. using System;
  150. using System.Collections.Generic;
  151. using System.Web.UI;
  152. using System.Web.UI.WebControls;
  153. using System.Data.SqlClient;
  154. using System.Configuration;
  155. using System.Data;
  156.  
  157. namespace Grocery_Demo
  158. {
  159. public partial class AdminViewProducts : System.Web.UI.Page
  160. {
  161. int PageSize = 10;
  162.  
  163. protected void Page_Load(object sender, EventArgs e)
  164. {
  165.  
  166. if (Session["UsernameAdmin"] != null && Session["PasswordAdmin"] != null && Session["BranchAdmin"] != null)
  167. {
  168. string username = Session["UsernameAdmin"].ToString();
  169. string password = Session["PasswordAdmin"].ToString();
  170. string branch = Session["BranchAdmin"].ToString();
  171. string CS = ConfigurationManager.ConnectionStrings["Grocery_DemoConnectionString"].ConnectionString;
  172. SqlConnection con = new SqlConnection(CS);
  173. SqlCommand cmd = new SqlCommand("AdminValidation", con);
  174. cmd.CommandType = System.Data.CommandType.StoredProcedure;
  175.  
  176. cmd.Parameters.AddWithValue("@AdministratorUsername", username);
  177. cmd.Parameters.AddWithValue("@AdministratorPassword", password);
  178. cmd.Parameters.AddWithValue("@GroceryBranchName", branch);
  179. con.Open();
  180. SqlDataReader read = cmd.ExecuteReader();
  181. read.Read();
  182. if (read.HasRows == false)
  183. {
  184. Response.Redirect("SignIn.aspx");
  185. }
  186. con.Close();
  187. }
  188.  
  189. else
  190. {
  191. Response.Redirect("SignIn.aspx");
  192. }
  193.  
  194. Label1.Text = Session["BranchAdmin"].ToString();
  195. Label2.Text = Session["UsernameAdmin"].ToString();
  196. {
  197. if (!Page.IsPostBack)
  198. DisplayProducts(1, PageSize);
  199. }
  200.  
  201. }
  202.  
  203. SqlDataAdapter da;
  204. DataSet ds = new DataSet();
  205. private void DisplayProducts(int PageIndex, int PageSize)
  206. {
  207. string CS = ConfigurationManager.ConnectionStrings["Grocery_DemoConnectionString"].ConnectionString;
  208.  
  209. using (SqlConnection con = new SqlConnection(CS))
  210. {
  211. using (SqlCommand cmd = new SqlCommand("AdminViewProducts", con))
  212. {
  213. cmd.CommandType = CommandType.StoredProcedure;
  214. cmd.Parameters.AddWithValue("@PageIndex", PageIndex);
  215. cmd.Parameters.AddWithValue("@PageSize", PageSize);
  216. cmd.Parameters.Add("@RecordCount", SqlDbType.Int, 4);
  217. cmd.Parameters["@RecordCount"].Direction = ParameterDirection.Output;
  218. da = new SqlDataAdapter(cmd);
  219. da.Fill(ds);
  220. con.Open();
  221. cmd.ExecuteNonQuery();
  222.  
  223. if (ds.Tables[0].Rows.Count > 0)
  224. {
  225. GridView1.DataSource = ds.Tables[0];
  226. GridView1.DataBind();
  227. }
  228. int recordCount = Convert.ToInt32(cmd.Parameters["@RecordCount"].Value);
  229. this.PopulatePager(recordCount, PageIndex);
  230. }
  231. }
  232.  
  233.  
  234. }
  235.  
  236.  
  237. private void PopulatePager(int recordCount, int currentPage)
  238. {
  239. double dblPageCount = (double)((decimal)recordCount / (PageSize));
  240. int pageCount = (int)Math.Ceiling(dblPageCount);
  241. List<ListItem> pages = new List<ListItem>();
  242. if (pageCount > 0)
  243. {
  244. pages.Add(new ListItem("FIRST >> ", "1", currentPage > 1));
  245. for (int i = 1; i <= pageCount; i++)
  246. {
  247. pages.Add(new ListItem(i.ToString(), i.ToString(), i != currentPage));
  248. }
  249. pages.Add(new ListItem(" << LAST", pageCount.ToString(), currentPage < pageCount));
  250. }
  251. rptPager.DataSource = pages;
  252. rptPager.DataBind();
  253. }
  254.  
  255. protected void lnkbtn_PageIndexChanged(object sender, EventArgs e)
  256. {
  257. int pageIndex = int.Parse((sender as LinkButton).CommandArgument);
  258. DisplayProducts(pageIndex, PageSize);
  259. }
  260.  
  261. protected void Search_Product(object sender, EventArgs e)
  262. {
  263. int PageIndex = 1;
  264. string CS = ConfigurationManager.ConnectionStrings["Grocery_DemoConnectionString"].ConnectionString;
  265.  
  266. using (SqlConnection con = new SqlConnection(CS))
  267. {
  268. using (SqlCommand cmd = new SqlCommand("AdminSearchProduct", con))
  269. {
  270.  
  271. cmd.CommandType = CommandType.StoredProcedure;
  272. cmd.Parameters.AddWithValue("@ProductName", TextBox1.Text + "%");
  273. cmd.Parameters.AddWithValue("@PageIndex", PageIndex);
  274. cmd.Parameters.AddWithValue("@PageSize", PageSize);
  275. cmd.Parameters.Add("@RecordCount", SqlDbType.Int, 4);
  276. cmd.Parameters["@RecordCount"].Direction = ParameterDirection.Output;
  277. da = new SqlDataAdapter(cmd);
  278. da.Fill(ds);
  279. con.Open();
  280. cmd.ExecuteNonQuery();
  281.  
  282. if (ds.Tables[0].Rows.Count > 0)
  283. {
  284. GridView1.DataSource = ds.Tables[0];
  285. GridView1.DataBind();
  286. }
  287.  
  288. else
  289. {
  290. Label3.Text = " Couldn't find your product";
  291. }
  292.  
  293. int recordCount = Convert.ToInt32(cmd.Parameters["@RecordCount"].Value);
  294. this.PopulatePager(recordCount, PageIndex);
  295. }
  296. }
  297. }
  298.  
  299. }
  300. }
  301.  
  302. FIRST >> 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 << LAST
  303.  
  304. FIRST >> 1 2 3 4 5 6 7 8 9 << LAST
  305.  
  306. FIRST >> 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 << LAST
  307.  
  308. FIRST >> 1 2 3 4 5 6 7 8 9 << LAST
Add Comment
Please, Sign In to add comment