Exemple #1
0
        protected void Page_Load(object sender, EventArgs e)
        {
            // Open workspace on page load
            workspace = WebShopServicesSingleton.Instance.OpenWorkspace(IsolationLevel.ReadOnly);
            var orders = WebShopServicesSingleton.Instance.GetAllOrders(workspace);

            OrderListRepeater.DataSource = orders;
            OrderListRepeater.DataBind();
        }
        protected void LoadOrderList()
        {
            // We select all orders from the "Orders" table where the CustomerID equals the CustomerID stored in the
            // hidden field "HiddenCustomerID" and binds the data to the "OrderListRepeater" control.

            // Declare variables for a connection string and a SELECT statement.
            string ConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
            string sql        = "SELECT * FROM Orders WHERE CustomerID = @CustomerID ORDER BY OrderID ASC";

            // Create a SqlConnection. The using block is used to call dispose (close) automatically even
            // if there are an exception.
            using (SqlConnection cn = new SqlConnection(ConnString))
            {
                // Create a SqlCommand.
                SqlCommand cmd = new SqlCommand(sql, cn);

                // Create a SqlDataReader.
                SqlDataReader reader = null;

                // Add parameters.
                cmd.Parameters.AddWithValue("@CustomerID", HiddenCustomerID.Value);

                // The Try/Catch/Finally block is used to handle exceptions.
                try
                {
                    // Open the connection.
                    cn.Open();

                    // Execute the SELECT statement and fill the reader with data.
                    reader = cmd.ExecuteReader();

                    // Bind data to the OrderListRepeater with the reader as datasource.
                    OrderListRepeater.DataSource = reader;
                    OrderListRepeater.DataBind();
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }
                finally
                {
                    // Dispose the SqlCommand.
                    cmd.Dispose();

                    // Close the reader.
                    if (reader != null)
                    {
                        reader.Close();
                    }
                }
            }
        }