Exercise: Implementing the Cart Admin Page

Một phần của tài liệu Beginning ASP.NET 2.0 E-Commerce in C# 2005 From Novice to Professional PHẦN 6 docx (Trang 20 - 24)

1. Add the ShoppingCartRemoveOldCarts stored procedure to the database. It receives as a parameter the maximum number of days for a shopping cart age. All shopping carts older than that are deleted.

CREATE PROCEDURE ShoppingCartDeleteOldCarts (@Days smallint)

AS

DELETE FROM ShoppingCart WHERE CartID IN

(SELECT CartID FROM ShoppingCart GROUP BY CartID

HAVING MIN(DATEDIFF(dd,DateAdded,GETDATE())) >= @Days)

2. Add ShoppingCartCountOldCarts, which returns the number of shopping cart elements that would be deleted by a ShoppingCartCountOldCarts call:

CREATE PROCEDURE ShoppingCartCountOldCarts (@Days smallint)

AS

SELECT COUNT(CartID) FROM ShoppingCart WHERE CartID IN (SELECT CartID FROM ShoppingCart GROUP BY CartID

HAVING MIN(DATEDIFF(dd,DateAdded,GETDATE())) >= @Days)

3. Add these methods to the ShoppingCartAccess class (located in ShoppingCartAccess.cs). They are used to interact with the two stored procedures you wrote earlier.

// Counts old shopping carts

public static int CountOldCarts(byte days) {

// get a configured DbCommand object

DbCommand comm = GenericDataAccess.CreateCommand();

// set the stored procedure name

comm.CommandText = "ShoppingCartCountOldCarts";

// create a new parameter

DbParameter param = comm.CreateParameter();

param.ParameterName = "@Days";

param.Value = days;

param.DbType = DbType.Byte;

comm.Parameters.Add(param);

// execute the procedure and return number of old shopping carts try

{

return Byte.Parse(GenericDataAccess.ExecuteScalar(comm));

} catch {

return -1;

} }

// Deletes old shopping carts

public static bool DeleteOldCarts(byte days) {

// get a configured DbCommand object

DbCommand comm = GenericDataAccess.CreateCommand();

// set the stored procedure name

comm.CommandText = "ShoppingCartDeleteOldCarts";

// create a new parameter

DbParameter param = comm.CreateParameter();

param.ParameterName = "@Days";

param.Value = days;

param.DbType = DbType.Byte;

comm.Parameters.Add(param);

// execute the procedure and return true if no problem occurs try

{

GenericDataAccess.ExecuteNonQuery(comm);

return true;

} catch {

return false;

} }

4. Create a new Web Form at the root of the BalloonShop project, named ShoppingCartAdmin.aspx, based on the Admin.master Master Page.

5. While in Source View, add this code to the first place holder:

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"

Runat="Server">

<span class="AdminTitle">Shopping Cart Admin</span>

</asp:Content>

6. Add the following content to the second place holder:

8213592a117456a340854d18cee57603

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2"

runat="Server">

<asp:Label ID="countLabel" runat="server" CssClass="AdminPageText">

Hello!

</asp:Label><br />

<span class="AdminPageText">How many days?</span>

<asp:DropDownList ID="daysList" runat="server">

<asp:ListItem Value="0">All shopping carts</asp:ListItem>

<asp:ListItem Value="1">One</asp:ListItem>

<asp:ListItem Value="10" Selected="True">Ten</asp:ListItem>

<asp:ListItem Value="20">Twenty</asp:ListItem>

<asp:ListItem Value="30">Thirty</asp:ListItem>

<asp:ListItem Value="90">Ninety</asp:ListItem>

</asp:DropDownList><br />

<br />

<asp:Button ID="countButton" runat="server" Text="Count Old Shopping Carts"

CssClass="Button" />

<asp:Button ID="deleteButton" runat="server" Text="Delete Old Shopping Carts" CssClass="Button" />

</asp:Content>

Now if you switch to Design View, you should see a form like the one shown in Figure 9-12.

Figure 9-12. ShoppingCartAdmin.aspx in Design View

7. Double-click the Delete Old Shopping Carts button, and complete its Click event handler with the following code:

// deletes old shopping carts

protected void deleteButton_Click(object sender, EventArgs e) {

byte days = byte.Parse(daysList.SelectedItem.Value);

ShoppingCartAccess.DeleteOldCarts(days);

countLabel.Text = "The old shopping carts were removed from the database";

}

8. Double-click the Count Old Shopping Carts button and complete its Click event handler with the fol- lowing code:

// counts old shopping carts

protected void countButton_Click(object sender, EventArgs e) {

byte days = byte.Parse(daysList.SelectedItem.Value);

int oldItems = ShoppingCartAccess.CountOldCarts(days);

if (oldItems == -1)

countLabel.Text = "Could not count the old shopping carts!";

else if (oldItems == 0)

countLabel.Text = "There are no old shopping carts.";

else

countLabel.Text = "There are " + oldItems.ToString() + " old shopping carts.";

}

9. Add this code to Page_Load:

protected void Page_Load(object sender, EventArgs e) {

// Set the title of the page

this.Title = BalloonShopConfiguration.SiteName + " : Shopping Cart Admin";

}

10. To restrict this page to administrator-only access, open web.config and add the following block, after the one that deals with CatalogAdmin.aspx:

<!-- Only administrators are allowed to access ShoppingCartAdmin.aspx -->

<location path="ShoppingCartAdmin.aspx">

<system.web>

<authorization>

<allow roles="Administrators" />

<deny users="*" />

</authorization>

</system.web>

</location>

11. Finally, add a link to this new page. Open UserInfo.ascx in Source View and add a link to the shopping cart admin page for the Administrators role group, just after the link to the catalog admin page:

<tr>

<td>

&nbsp;&raquo;

<a class="UserInfoLink" href="CatalogAdmin.aspx">Catalog Admin</a>

&nbsp;&laquo;

</td>

</tr>

<tr>

<td>

&nbsp;&raquo;

<a class="UserInfoLink" href="ShoppingCartAdmin.aspx">Shopping Cart Admin</a>

&nbsp;&laquo;

</td>

</tr>

</ContentTemplate>

How It Works: The Shopping Cart Admin Page Congratulations, you’re done! Your new shopping cart admin page should work as expected.

Một phần của tài liệu Beginning ASP.NET 2.0 E-Commerce in C# 2005 From Novice to Professional PHẦN 6 docx (Trang 20 - 24)

Tải bản đầy đủ (PDF)

(70 trang)