Tuesday, May 26, 2009

Virtual url to upload images

In order to get the physical address of a virtual directory use the following

1. if the page executing the request is directly under the root
MapPath("VirtualDirectoryName/");

2. If the page is somewhere under a child folder like

root/controls/upload.ascx

MapPath("../VirtualDirectoryName/");

COM+ Automation using C#

Accessing COM+ Catalog

The COM+ catalog is the underlying data store that holds all COM+ configuration data. Whenever you do any kind of COM+ administration, you are reading and writing data stored in the catalog. The only way that you can access the catalog is through the Component Services administrative tool or through the COMAdmin library.
Security
To change data on the COM+ catalog, you need to have authority as an administrator. An application using the COMAdmin objects must be running under a user account that is assigned to the Administrators role on the system application on the machine that it is trying to administer.



The Applications collection contains an item for each COM+ application that is installed on the machine. The collections occur in a hierarchical structure, the Components collection is subsumed under the Applications collection and holds the components installed into that particular application. In other words you need to step through several collections to get to the element you want. Each item in a collection exposes properties. These properties serve to hold configuration data for whatever element the item represents. Here are some of the collections that are available in COMAdminCatalog, you can find a complete list of collections on MSDN.

Collections Description
Applications Contains an object for each COM+ application installed on the local machine.
Components Holds an object for each component in the application to which it is related..
ErrorInfo Retrieves extended error information regarding methods that deal with multiple objects.
Roles Holds an object for each role assigned to the application to which it is related.
RolesForComponent Holds an object for each role assigned to the component to which the collection is related.
RolesForInterface Holds an object for each role assigned to the interface to which the collection is related.
RolesForMethod Holds an object for each role assigned to the method to which the collection is related.
RolesForPartition Holds an object for each role assigned to the partition to which the collection is related.
UsersInRole Holds an object for each user in the role to which the collection is related.

Retrieving COMAdminCatalog Application Collection



//Delegates
public delegate void DeleteEventHandler();
public delegate void ErrorMessageHandler(string s, Exception e);

//Events
/// Raised when the object is successfully deleted on the host.
public event DeleteEventHandler Delete;
/// Raised when the object throws an exception.
public event ErrorMessageHandler Error;

//Method(s) to invoke the event
protected virtual void OnDelete()
{

if(Delete != null)
Delete();
}

#region Get COM + Applications
// Get COM + Applications

internal COMAdminCatalogCollection GetCollection()
{
try
{
objAdmin = new COMAdmin.COMAdminCatalog();
objRoot = objAdmin.Connect("192.168.1.150");

objCollection =
(COMAdmin.COMAdminCatalogCollection) objAdmin.GetCollection("Applications");
}
catch(Exception ex)
{

System.Windows.Forms.MessageBox.Show("Error : " + ex);
}
return objCollection;
}
#endregion





Retrieving COMAdminCatalog Component Collection




#region Get COM+ Applications / Components
// Return all COM + Applications and their corresponding components

public NameValueCollection GetCOMApplications()
{
collection = new NameValueCollection();
try
{
objCollection = GetCollection();
objCollection.Populate();

foreach(COMAdmin.COMAdminCatalogObject objAppNames in objCollection)
{
COMAdmin.ICatalogCollection objComponents =
(COMAdmin.ICatalogCollection) objCollection.GetCollection("Components",objAppNames.Key);
objComponents.Populate();
foreach(COMAdmin.COMAdminCatalogObject Components in objComponents)
{
collection.Add(Components.Name.ToString(),objAppNames.Name.ToString());
}
}
}
catch(Exception e)
{
Error += new ErrorMessageHandler(Message);
Error("GetCOMAPPlications",e);
Dispose();
}
return collection;
}
#endregion






Delete existing COM+ Application



#region Delete exisiting COM+ Application
public void DeleteCOMApplication(string appName)
{
try
{
long l_Count = 0;
ICatalogCollection pCollection = GetCollection();
ICatalogObject pCatalog;
pCollection.Populate();
l_Count = pCollection.Count;
if(l_Count == 0)
return;

for(int i= 0; i < l_Count; i ++)
{
pCatalog = (ICatalogObject) pCollection.get_Item(i);
if(appName == (string) pCollection.get_Value("Name"))
{
pCollection.Remove(i);
pCollection.SaveChanges();
OnDelete();
return;
}
}
}
catch(Exception e)
{
Error += new ErrorMessageHandler(Message);
Error("Unable to delete the COM+ Application :" , e);
Dispose();
}
}
#endregion





Creating a new COM+ Application
Create a new COM+ application by adding a new item to the collection. When you set properties on an item, no changes are actually recorded to the COM+ catalog until you explicitly save changes. You do this using the SaveChanges() method on the COMAdminCatalogCollection object for the collection containing the item.



#region CreateCOMApplication
///
/// Create a new COM+ application
///

public void CreateCOMApplication()
{
bool Exists = false;
try
{
Exists = ApplicationExists();// Method to check whether the application exists in Catalog
if(!Exists)
{
objCollection = GetCollection();
COMAdmin.COMAdminCatalogObject objObject =
(COMAdmin.COMAdminCatalogObject) objCollection.Add();
objObject.set_Value("Name",props.COM_Name);
objObject.set_Value("Description",props.COM_Description);
objObject.set_Value("Activation",props.COM_Type);
objObject.set_Value("Identity",props.COM_User);
objCollection.SaveChanges();
OnCreate();
}
else
{
System.Windows.Forms.MessageBox.Show("An application with same name already exixts in registry !.","Nexsure COM + Help",
System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Error);
}
}
catch(Exception ex)
{
Error += new ErrorMessageHandler(Message);
Error("Error creating COM+ Application :" , ex);
Dispose();
}
}
#endregion

internal void Message(string message,Exception e)
{
System.Windows.Forms.MessageBox.Show("Error occured " + message + " error description : " + e ,"Nexsure COM + ",
System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Error);
}





Starting and Shutting Down COM+ Applications


#region Starting COM+ Application
public bool StartCOMApplication()
{
bool Startup = false;
try
{
objCollection = GetCollection(); //Method return COM+ Applications
objAdmin.StartApplication(applicationName);
System.Windows.Forms.MessageBox.Show("Application was successfully started.");
Startup = true;
}
catch
{
System.Windows.Forms.MessageBox.Show("Unable to start the application.");
}
return Startup;
}
#endregion

#region Shutting Down COM+ Application
public bool ShutDownCOMApplication()
{
bool ShuttingDown = false;
try
{
objCollection = GetCollection();
objAdmin.ShutdownApplication(applicationName);
System.Windows.Forms.MessageBox.Show("Application was successfully shutdown.");
ShuttingDown = true;
}
catch
{
System.Windows.Forms.MessageBox.Show("Unable to shutdown the application.");
}
return ShuttingDown;
}
#endregion

IIS Administration using ADSI and .NET DirectoryServices

The System.DirectoryServices namespace provides access to the Active Directory. These classes make it possible to do the following Web-site administration tasks programmatically:


• Verifying that a virtual directory exists
• Creating and deleting virtual directories
• Setting and updating any of the properties on IIS
• Invoking any of the IIS-specific methods

Getting Virtual Directories


public StringCollection GetIISVirtualDirectories(string provider)
{
provider = "IIS://192.168.1.150/W3SVC/1/Root";
DirectoryEntries dirEntries = null;
try
{
dirEntry = new DirectoryEntry(provider);
dirEntry.RefreshCache();
dirEntries = dirEntry.Children;
strCollection = new StringCollection();
foreach(DirectoryEntry objDir in dirEntries)
{
strCollection.Add(objDir.Name);
}
dirEntry.Dispose();
}
catch(Exception ex)
{
System.Windows.Forms.MessageBox.Show("Error getting virtual director: " + ex);
return null;
}
return strCollection;
}





Metabase
In physical terms, the metabase is a combination of the MetaBase.xml and MBSchema.xml files and the in-memory metabase. The IIS configuration information is stored in the MetaBase.xml file, while the metabase schema is stored in the MBSchema.xml file. When IIS is started, these files are read by the storage layer and then written to the in-memory metabase through Admin Base Objects (ABO). All configuration settings that are made using the resources in this list are written to the in-memory metabase through Admin Base Objects (ABOs). The in-memory metabase is a copy of the MetaBase.xml and MBSchema.xml files in the IIS file cache.

IIS ADSI Object Hierarchy

The IIS ADSI objects are organized in a hierarchical structure that mirrors the structures defined in the IIS metabase configuration file. Objects contain other objects to create the object structure. Most objects inherit characteristics or properties from the parent object, but child objects can contain unique characteristics implemented by extending the schema. You can use this object hierarchy to access configuration settings for specific IIS elements.

Creating Virtual Directories



public int createVirtualDirectory(string ipaddress,string strRootPath, string schema , string sitename , string path)
{
int success = 0;
DirectoryEntry dirRoot = new DirectoryEntry("IIS://" + ipaddress + strRootPath.Trim());
//Check whether the directory already exists on the server
if(DirectoryEntry.Exists("IIS://" + ipaddress + strRootPath + "/" +sitename))
return 0;//Return 0 to display error message
try
{
//If not exists, add a new virtual directoty
dirRoot.RefreshCache();
DirectoryEntry dirNew = dirRoot.Children.Add (sitename,schema);
dirNew.Properties["Path"].Insert(0,path);
dirNew.CommitChanges();
dirRoot.CommitChanges();
//If it is web virtaul directory
if(schema == "IIsWebVirtualDir")
{
//dirNew.Invoke("AppCreate",true);
dirNew.Invoke("AppCreate",new object[]{true});
dirNew.CommitChanges();
dirRoot.CommitChanges();
dirNew.Close();
dirRoot.Close();
}
success = 1;
}
catch
{
throw new IISPropertiesException("Unable to create the virtual directory");
}
return success;
}


public class IISPropertiesException : Exception
{
///
/// Constructs a new IISPropertiesException class
///

/// Speicifes the error description
public IISPropertiesException(string description): base(description){}
public override string ToString()
{
return "IISProperties Exception : " + Message + " " + StackTrace;
}
}






Delete a virtaul directory

public void deleteVirtualDirectory(string ipaddress, string provider, string sitename, string schema)
{
try
{
DirectoryEntry delDir = new DirectoryEntry("IIS://" + ipaddress + provider);
DirectoryEntry removeDir = new DirectoryEntry("IIS://" + ipaddress + provider +"/"+ sitename);
delDir.RefreshCache();
delDir.Children.Remove(removeDir);
delDir.CommitChanges();
delDir.Close();

}
catch(Exception e)
{
//throw new IISPropertiesException("Unable to delete the virtual directory ");
System.Windows.Forms.MessageBox.Show("Error deleting virtual directory : " + e);
}
}

Tuesday, November 4, 2008

The Controls collection cannot be modified because the control contains code blocks . AJAX Calendar Extender

If you are using AJAX Calendar Extendar on the page and get this error message.

Do the following to fix the issue.

1. Master Page add a <asp:placeholder> control between <head> tags and move <script>

or

<script type="text/javascript" src="<%=ResolveClientUrl(">/jquery.min.js"></script>


tags within the <asp:placeholder>

So it should look like this


<asp:contentplaceholder id="myPlaceholder" runat="server">

<script type="text/javascript" src="<%=ResolveClientUrl("/javascript/")%>/jquery.min.js"></script>

</asp:ContentPlaceHolder>

2. Add another placeholder control within the <head> tags and name it head.

<asp:contentplaceholder id="head" runat="server"></asp:ContentPlaceHolder>


It should look like this:


<head runat="server">
<asp:contentplaceholder id="myPlaceholder" runat="server">

<script type="text/javascript" src="<%=ResolveClientUrl("/javascript/")%>/jquery.min.js"></script>

</asp:ContentPlaceHolder>

<asp:contentplaceholder id="head" runat="server"></asp:ContentPlaceHolder>


</head>