aDEFWEBSERVER
Los Angeles, CA *
 Webmaster@ADefWebserver.com

Back to: Creating a DotNetNuke® Module using LINQ to SQL For absolute beginners! Part 2

Category Administration

  • Show the number of items in each category.

  • Do not allow a category to be deleted if there are items using that category.

 
   

Show the number of items in each category

 

To implement this feature we need to complete these tasks:

  • Override the "Selecting" event handler to populate the GridView to display a custom collection of ThingsForSale_Category objects. This collection will contain an additional field called Count.
  • Alter the GridView to display the Count field.
 
Form more information see: Performing Custom Query Projections with the Selecting Event  
   

Override the "Selecting" event

 
Double-click on Categories.ascx to open it. Switch to Design view.

Select the LinqDataSource1 control. In it's Properties, click the yellow "lighting bolt" to view it's events. Double-click on the box to the right of Selecting.

This will wire-up a method. Alter the method so it reads:  
 protected void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
LinqThings4SaleDataContext LinqThings4SaleDataContext = new LinqThings4SaleDataContext();

var Things4Sale = from T in LinqThings4SaleDataContext.ThingsForSale_Categories
select new
{
T.ID,
T.Category,
Count = LinqThings4SaleDataContext.ThingsForSales.Count(c => c.Category == T.Category)
};

e.Result = Things4Sale;
}
(note: normally you would place data access code in a separate file. This code was placed inline only for brevity)

Click on the options for the GridView and select Edit Columns...


Click the Add button.

An extra ID and Category field will be added to the Selected fields box. Use the X button to remove the extra ID field. Click on the extra Category field and in the BoundField properties box:

  • Change  HeaderText to Count
  • Change  SortExpression to Count
  • Change  DataField to Count

Click the OK button.



When you view the Category administration in the website you will be able to see the count of items using that Category and still sort and page the GridView.

   

Do not allow a category to be deleted if there are items using that category

 

To implement this feature we need to complete these tasks:

  • Create a partial method to possibly cancel a delete and return an exception.
  • Detect that a possible exception was thrown.
 

Create a partial method to possibly cancel a delete and return an exception

 

In the Solution Explorer, right-click the LinqThings4Sale folder in the App_Code folder and select Add New Item...

Select the Class template.

Enter PartialMethods.cs for Name.

Select Visual C# for the Language.

Click the Add button.

Alter the the entire contents of the file so it reads:  

using System.Linq;
using System.Linq.Expressions;
using System.Data.Linq;
using System;

namespace LinqThings4Sale
{
public partial class LinqThings4SaleDataContext : System.Data.Linq.DataContext
{
partial void DeleteThingsForSale_Category(ThingsForSale_Category instance)
{
var Categories = from Category in ThingsForSales
where Category.Category == instance.Category
select Category;

if (Categories.Count() > 0)
{
throw new Exception("Cannot delete a Category that is being used");
}

this.ExecuteDynamicDelete(instance);
}
}
}
   

Detect that a possible exception was thrown

 
   

Double-click on Categories.ascx to open it. Switch to Design view.

Drag and drop a Label control on the page.


In it's Properties, set EnableViewState to False.

Clear the Text value.





In the events for the GridView, double-click on the box next to RowDeleted.

This will wire-up a method. Alter the method so it reads:  
 
protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
{
if (e.Exception != null)
{
Label1.Text = e.Exception.Message;
e.ExceptionHandled = true;
}
}

Save all the pages.
 

When you try to delete a category that has items using it, a error message will appear.

   

Back to: Creating a DotNetNuke® Module using LINQ to SQL For absolute beginners! Part 2

(C) by Michael Washington - ADefWebserver.com - Webmaster@ADefWebserver.com

DotNetNuke® is a registered trademark of the DotNetNuke Corporation