Code Behind Page Page /ASP.NETSamples/DisplayProducts.aspx.cs (C#)
Used by Page /ASP.NETSamples/DisplayProducts.aspx (C#)
1: using System;
2: using System.Collections;
3: using System.ComponentModel;
4: using System.Data;
5: using System.Data.SqlClient;
6: using System.Drawing;
7: using System.Web;
8: using System.Web.SessionState;
9: using System.Web.UI;
10: using System.Web.UI.WebControls;
11: using System.Web.UI.HtmlControls;
12:
13: namespace BRETTB
14: {
15: /// <summary>
16: /// Displays Products in a DataGrid
17: /// </summary>
18: public class DisplayProducts : System.Web.UI.Page
19: {
20: protected System.Web.UI.WebControls.DataGrid ProductsDataGrid;
21: protected System.Web.UI.WebControls.HyperLink HyperLinkHomePage;
22: protected System.Web.UI.WebControls.Label LabelTitle;
23:
24: /// <summary>
25: /// Subroutine executes when the page is loaded
26: /// </summary>
27: /// <param name="sender"></param>
28: /// <param name="e"></param>
29: private void Page_Load(object sender, System.EventArgs e)
30: {
31: // Put user code to initialize the page here
32: LabelTitle.Text = "View Products";
33:
34: HyperLinkHomePage.NavigateUrl = "../Default.aspx";
35:
36: //Retrieve data from database
37: string SQL = "EXEC sp_GetProducts";
38: SqlConnection DataConnection = new SqlConnection(Application["ConnectionString"].ToString());
39: SqlDataAdapter ProductsDataAdapter = new SqlDataAdapter(SQL, DataConnection);
40: DataSet ProductsDataSet = new DataSet();
41:
42: try
43: {
44: ProductsDataAdapter.Fill(ProductsDataSet, "Products");
45: }
46: catch
47: {
48: ProductsDataSet = null;
49: }
50: finally
51: {
52: if (DataConnection != null)
53: {
54: if (DataConnection.State == ConnectionState.Open)
55: {
56: DataConnection.Close();
57: }
58: }
59:
60: }
61:
62: ProductsDataGrid.DataSource = ProductsDataSet;
63: ProductsDataGrid.DataBind();
64:
65: }
66:
67: #region Web Form Designer generated code
68: override protected void OnInit(EventArgs e)
69: {
70: //
71: // CODEGEN: This call is required by the ASP.NET Web Form Designer.
72: //
73: InitializeComponent();
74: base.OnInit(e);
75: }
76:
77: /// <summary>
78: /// Required method for Designer support - do not modify
79: /// the contents of this method with the code editor.
80: /// </summary>
81: private void InitializeComponent()
82: {
83: this.Load += new System.EventHandler(this.Page_Load);
84:
85: }
86: #endregion
87: }
88: }