

Failed to access IIS metabase
System.Web.Hosting.HostingEnvironmentException: Failed to access IIS metabase
I solved this by Reinstalling the .NET 2.0 Framework via aspnet_regiis -i
Mutex could not be created
System.InvalidOperationException: Mutex could not be created
The Solution for this error is bit more complicate:
- Close all opened Visual Studios.
- Navigate to C:\Windows\Microsoft.NET\Framework\v2[....]\Temporary ASPNET pages.
- Delete the folder for your application (You can delete the temporary folder completley)
- Perform IISReset via command line or via inetmgr.exe
- Browse your application directly from IIS and not from any where else.
- You should see your application correctly now.
Good Luck !
Executing text queries with LINQ is simple as that...
Assuming you have Adventure Works Database installed.
public void DoExecuteQuery()
{
AdventureWorksDataContext aw = new AdventureWorksDataContext();
IEnumerable<Employee> employees =
aw.ExecuteQuery<Employee>("SELECT * FROM HumanResources.Employee");
foreach (Employee e in employees)
{
Console.WriteLine(e.Title);
}
}
Its amazing how simple it is to create inner join queries with LINQ.
Assuming you have the AdventureWorks Database installed.
public void GetEmployeeByID(int employeeID)
{
AdventureWorksDataContext aw = new AdventureWorksDataContext();
aw.Log = Console.Out;
var entities = from e in aw.Employees
join ea in aw.EmployeeAddresses on e.EmployeeID equals ea.EmployeeID
join a in aw.Addresses on ea.AddressID equals a.AddressID
join c in aw.Contacts on e.ContactID equals c.ContactID
where e.EmployeeID == employeeID
select new
{
Title = e.Title,
FirstName = c.FirstName,
LastName = c.LastName,
City = a.City,
AddressID = ea.AddressID
};
foreach (var e in entities)
{
Console.WriteLine("Title = {0}, FirstName = {1}, LastName = {2}",
e.Title, e.FirstName, e.LastName);
}
}
The text query and the results:

Usually when we perform a LINQ Query on a SQL table, we don't see the actual query.
To see the query text, use the Log method: aw.Log = Console.Out
public List<Employee> GetEmployees()
{
AdventureWorksDataContext aw = new AdventureWorksDataContext();
var employees = from emps in aw.Employees
select emps;
aw.Log = Console.Out;
return employees.ToList();
}
The result:

Whenever possible, i rather to use UserControl instead of WebControl, since WebControls are more complicate to create and later on to maintain.
But in case that i have to build a WebControl, i rather build a CompositeControl.
Way CompositeControl?
CompositeControl eventually inherit from WebControl, but each control inside the CompositeControl manage its own life cycle events and the ViewState / PostBack data.
This means that we going to save a lots of code writing by not handling this events our self's.
Implementing the CompositeControl
Create the following code in a new Class Library:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace BaseType.Composite.Controls
{
public class LoginControl : CompositeControl
{
/// <summary>
/// Occurs when [login submit].
/// </summary>
public event EventHandler LoginSubmit;
/// <summary>
/// Gets or sets the name of the user.
/// </summary>
/// <value>The name of the user.</value>
public string UserName
{
get { return ((TextBox)this.FindControl("UserName")).Text; }
set { ((TextBox)this.FindControl("UserName")).Text = value; }
}
/// <summary>
/// Gets or sets the password.
/// </summary>
/// <value>The password.</value>
public string Password
{
get { return ((TextBox)this.FindControl("Password")).Text; }
set { ((TextBox)this.FindControl("Password")).Text = value; }
}
/// <summary>
/// Called by the ASP.NET page framework to notify server controls that use composition-based
/// implementation to create any child controls they contain in preparation for posting back or
/// rendering.
/// </summary>
protected override void CreateChildControls()
{
TextBox tbUserName = new TextBox();
TextBox tbPassword = new TextBox();
tbUserName.ID = "UserName";
tbPassword.ID = "Password";
Button btLogin = new Button();
btLogin.Text = "Login";
btLogin.Click += new EventHandler(btLogin_Click);
HtmlTable table = new HtmlTable();
HtmlTableRow tr;
HtmlTableCell td;
//Add the UserName textbox
tr = new HtmlTableRow();
td = new HtmlTableCell();
td.InnerHtml = "UserName:";
tr.Controls.Add(td);
td = new HtmlTableCell();
td.Controls.Add(tbUserName);
tr.Controls.Add(td);
table.Controls.Add(tr);
//Add the Password textbox
tr = new HtmlTableRow();
td = new HtmlTableCell();
td.InnerHtml = "Password:";
tr.Controls.Add(td);
td = new HtmlTableCell();
td.Controls.Add(tbPassword);
tr.Controls.Add(td);
table.Controls.Add(tr);
//Add the Login button
tr = new HtmlTableRow();
td = new HtmlTableCell();
td.InnerHtml = " ";
tr.Controls.Add(td);
td = new HtmlTableCell();
td.Controls.Add(btLogin);
tr.Controls.Add(td);
table.Controls.Add(tr);
//Add the Table to this control
this.Controls.Add(table);
}
/// <summary>
/// Handles the Click event of the btLogin control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
void btLogin_Click(object sender, EventArgs e)
{
if (LoginSubmit != null) LoginSubmit(this, e);
}
}
}
Once compiled, you can add the Control to your Toolbox by adding the .DLL file.
Using the LoginControl
After adding the control to your toolbox, all you need to do is to drag him to you designer:
<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="Test._Default" %>
<%@ Register Assembly="BaseType.CompositeControls"
Namespace="BaseType.Composite.Controls" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<cc1:LoginControl ID="LoginControl1" runat="server"
OnLoginSubmit="LoginControl1_LoginSubmit" />
</div>
</form>
</body>
</html>
You can hook to the LoginSubmit event from designer:
<cc1:LoginControl ID="LoginControl1" runat="server"
OnLoginSubmit="LoginControl1_LoginSubmit" />
After posting the form, you can get the UserName and Password for the LoginControl class already exposed properties, since CompositeControl already managing the ViewState and Postback data for us:
protected void LoginControl1_LoginSubmit(object sender, EventArgs e)
{
Response.Write(LoginControl1.UserName);
}
The Control in action
Enjoy!
ASP.NET offers some nice tools to use with our common web sites development.
Some of them is the Menu and TreeView controls.
Consider the following SQL Table:
lets say I want to display all the child categories under there parent categories in a nice DHTML menu.
First Step - Add a Menu control to the ASPX file.
<asp:Menu runat="server" ID="Menu1">
<DataBindings>
<asp:MenuItemBinding TextField="CategoryName" />
</DataBindings>
</asp:Menu>
Select the data from the table.
//Create new connection
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
//Create new adapter
using (SqlDataAdapter adapter = new SqlDataAdapter(sqlQuery, connectionString))
{
adapter.Fill(dsCategories);
}
}
Fill the created DataSet with the data from the table, no recursive operation here (yet).
Create DataRelations between the CategoryID and ParentID.
//Name the dataset and the datatable within the dataset.
dsCategories.DataSetName = "Categories";
dsCategories.Tables[0].TableName = "Category";
//Create new data relation between the CategoryID and the ParentID
DataRelation relation = new DataRelation("ChildToParent",
dsCategories.Tables["Category"].Columns["CategoryID"],
dsCategories.Tables["Category"].Columns["ParentID"],
true
);
First I named the DataSet and the DataTable within the DataSet for later use with the XSLT script and the DataRelation object.
Then I create a DataRelation between the CategoryID and the ParentID.
The last true operator means that each child must have a parent id that exists in the table.
Nest the child's under there parents.
//Set this to true for nest child categories under parent categories
relation.Nested = true;
This is very important!, It tells the DataSet to nest the child under his parent.
If we don't set it to true, each category will be at the same level like above category.
This result the following XML:
<Categories>
<Category>
<CategoryID>1</CategoryID>
<CategoryName>Root</CategoryName>
<Category>
<CategoryID>2</CategoryID>
<ParentID>1</ParentID>
<CategoryName>Menu1</CategoryName>
<Category>
<CategoryID>9</CategoryID>
<ParentID>2</ParentID>
<CategoryName>Menu7</CategoryName>
</Category>
</Category>
<Category>
<CategoryID>3</CategoryID>
<ParentID>1</ParentID>
<CategoryName>Menu2</CategoryName>
<Category>
<CategoryID>10</CategoryID>
<ParentID>3</ParentID>
<CategoryName>Menu8</CategoryName>
<Category>
<CategoryID>8</CategoryID>
<ParentID>10</ParentID>
<CategoryName>Menu6</CategoryName>
</Category>
</Category>
</Category>
<Category>
<CategoryID>4</CategoryID>
<ParentID>1</ParentID>
<CategoryName>Menu3</CategoryName>
<Category>
<CategoryID>12</CategoryID>
<ParentID>4</ParentID>
<CategoryName>Menu10</CategoryName>
</Category>
</Category>
<Category>
<CategoryID>6</CategoryID>
<ParentID>1</ParentID>
<CategoryName>Menu4</CategoryName>
</Category>
</Category>
<Category>
<CategoryID>7</CategoryID>
<CategoryName>Menu5</CategoryName>
<Category>
<CategoryID>11</CategoryID>
<ParentID>7</ParentID>
<CategoryName>menu9</CategoryName>
</Category>
<Category>
<CategoryID>13</CategoryID>
<ParentID>7</ParentID>
<CategoryName>Menu11</CategoryName>
</Category>
</Category>
</Categories>
Notice the nested child categories under there parents.
Reformat the XML to feet the Menu.
I created a new XmlDataSource in the code to translate the DataSet XML by the following XSLT script I saved as a file XSLTFile1.xslt - recursive operation.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" encoding="utf-8"/>
<xsl:template match="/Categories">
<MenuItems>
<xsl:call-template name="CategoriesListing" />
</MenuItems>
</xsl:template>
<xsl:template name="CategoriesListing">
<xsl:apply-templates select="Category" />
</xsl:template>
<xsl:template match="Category">
<MenuItem>
<xsl:attribute name="CategoryName">
<xsl:value-of select="CategoryName"/>
</xsl:attribute>
<xsl:attribute name="ParentID">
<xsl:value-of select="ParentID"/>
</xsl:attribute>
<xsl:if test="count(Category) >0">
<xsl:call-template name="CategoriesListing" />
</xsl:if>
</MenuItem>
</xsl:template>
</xsl:stylesheet>
//Get the xml created by dataset with nested categories by relation
xmlDataSource.Data = dsCategories.GetXml();
//Reformat the xml from the dataset to feet menu xml format
xmlDataSource.TransformFile = Server.MapPath("~/XSLTFile1.xslt");
//Tells the menu to start read all MenuItem under MenuItems
xmlDataSource.XPath = "MenuItems/MenuItem";
First we need to get the xml from the DataSet as a string and assign it to the XmlDataSource Data property.
Then choose the XSLT file that will transform our DataSet xml to more convenient xml string.
Last, we set the XPath to select all category [as MenuItem] from the new xml created by the XSLT script.
Resulting the following XML:
</Categories>
<MenuItems>
<MenuItem CategoryName="Root" ParentID="">
<MenuItem CategoryName="Menu1" ParentID="1">
<MenuItem CategoryName="Menu7" ParentID="2"></MenuItem>
</MenuItem>
<MenuItem CategoryName="Menu2" ParentID="1">
<MenuItem CategoryName="Menu8" ParentID="3">
<MenuItem CategoryName="Menu6" ParentID="10"></MenuItem>
</MenuItem>
</MenuItem>
<MenuItem CategoryName="Menu3" ParentID="1">
<MenuItem CategoryName="Menu10" ParentID="4"></MenuItem>
</MenuItem>
<MenuItem CategoryName="Menu4" ParentID="1"></MenuItem>
</MenuItem>
<MenuItem CategoryName="Menu5" ParentID="">
<MenuItem CategoryName="menu9" ParentID="7"></MenuItem>
<MenuItem CategoryName="Menu11" ParentID="7"></MenuItem>
</MenuItem>
</MenuItems>
Finally, we bind the data to the Menu control.
//Finally, bind the source to the Menu1 control
Menu1.DataSource = xmlDataSource;
Menu1.DataBind();
The complete procedure:
private void BindMenuCategories()
{
string connectionString = @"Data Source=GILAD-LAPTOP\SQLEXPRESS;Initial Catalog=BaseType.Commerce;Integrated Security=True;Pooling=True";
string sqlQuery = string.Format("SELECT {0} FROM {1}", "CategoryID, ParentID, CategoryName", "tblCategories");
DataSet dsCategories = new DataSet();
//This is the datasource i use to load MenuItems
XmlDataSource xmlDataSource = new XmlDataSource();
//Create new connection
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
//Create new adapter
using (SqlDataAdapter adapter = new SqlDataAdapter(sqlQuery, connectionString))
{
adapter.Fill(dsCategories);
}
}
//Name the dataset and the datatable within the dataset.
dsCategories.DataSetName = "Categories";
dsCategories.Tables[0].TableName = "Category";
//Create new data relation between the CategoryID and the ParentID
DataRelation relation = new DataRelation("ParentToChild",
dsCategories.Tables["Category"].Columns["CategoryID"],
dsCategories.Tables["Category"].Columns["ParentID"],
true
);
//Set this to true for nest child categories under parent categories
relation.Nested = true;
dsCategories.Relations.Add(relation);
//Get the xml created by dataset with nested categories by relation
xmlDataSource.Data = dsCategories.GetXml();
//Reformat the xml from the dataset to feet menu xml format
xmlDataSource.TransformFile = Server.MapPath("~/XSLTFile1.xslt");
//Tells the menu to start read all MenuItem under MenuItems
xmlDataSource.XPath = "MenuItems/MenuItem";
//Finally, bind the source to the Menu1 control
Menu1.DataSource = xmlDataSource;
Menu1.DataBind();
}
Enjoy!
Soooo, its been very busy 2 months for me. I've started a new project for one of my company customers. I also passed 2 MCP exams 70-356 / 70-528 which makes me an MCTS - "Microsoft Certified Technology Specialist", and still I have few more test to become an MCSD.
In the new project I started, I needed to map all the references dependencies between the 52 (!!!) class libraries exists in the solution - Its very hard to map this manually, and probably will take me forever.
I found that .NET Reflector can answer to my question, and do the work for me by using one of its great add-ins, the Graph.
All you need to do is to drag the DLL for your project into the reflector window, right click on it, and select the "Peli's Assembly Graph".
The results are great!
Enjoy!