Monday, April 27, 2009

VS ASP.NET Error: it is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level.

Error
Error while compiling solution in Visual Studio .NET: It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level. This error can be caused by a virtual directory not being configured as an application in IIS

Resolution
If there are any web.config files in the sub folders that make sure you don't duplicate the entries under of web.config in the root folder, in the web.config in the sub-folder.

For instance if the folder structure of your ASP.NET application is like this...
Login.aspx
Web.config
(In web.config
system.web
authentication mode="Windows"
system.web
)
Secure (folder)
Home.aspx
Web.config
(In subfolder web.config I Add the same I get error
system.web
authentication mode="Windows"
system.web
)
(In subfolder web.config I remove those entries, error goes off
system.web
!-- comment it
authentication mode="Windows"
system.web
)

Tuesday, April 21, 2009

How to deep copy an object in .NET

Introduction
This article gives a quick look at how to deep copy your objects. The idea is not to give an insight on why cloning is required and which is the best method to do it, as there are numerous articles on cloning, this article gives the implementation of deep copying in .NET
Implementation
In a shallow copy the value types are copied, whereas the reference types refer to the same shared memory. With a deep copy the memory references by the object is also copied (cloned) and each object references its own instance of data. There are different ways of deep copying an object, one method is to use memory stream, where in object is serialized into a memory stream and then we de-serialize it into a new object. Another approach is to use the Reflections concepts of .NET, article on which can be read in Code Project at this link
http://www.codeproject.com/KB/cs/cloneimpl_class.aspx .
So in this article the approach would be to use the first one and understand the implementation and complexities involved. Please note that this approach requires us to serialize the object that needs to be cloned.
Complexities are involved where in you might need to include or exclude some fields of the object, which can be achieved using the
Conclusion
This article brings together the different approaches, to deep copy a .NET object, for reference. We also get to look at what happens in the default copy wherein Member wise copy is done.

Code
///
/// Abstract class that implement Deep Cloning using Memory stream
///

[Serializable]
public abstract class MyBaseObject : ICloneable
{
public object Clone()
{
// deep-copying

MemoryStream memoryStream = new MemoryStream();

BinaryFormatter binaryFormatter = new BinaryFormatter();

binaryFormatter.Serialize(memoryStream, this);

memoryStream.Position = 0;

object clone = binaryFormatter.Deserialize(memoryStream);

memoryStream.Close();

return clone;
}
}

///
/// this class is reference from the class that needs to be cloned
///

[Serializable]
public class MyReferenceClass : MyBaseObject
{
///
/// Some Id
///

private int _id;


}

///
/// Class that needs to be cloned
///

[Serializable]
public class MyCloneableClass : MyBaseObject
{
///
/// Class to be cloned having a reference Type
///

public MyReferenceClass _myReferenceClass;

///
/// Class to be cloned having a value type
///

private string _name;

///
/// Constructor
///

///
public MyCloneableClass(int id)
{
_myReferenceClass = new MyReferenceClass();
_myReferenceClass.ID = id;
}

}
This article brings together the different approaches, to deep copy a .NET object, for reference. We also get to look at what happens in the default copy wherein Member wise copy is done.

Thursday, February 12, 2009

WebPart: Register tag is added automatically to .ASPX file when you add PPS web part

This observation can be used in other context.
Suppose you've have a .ASPX page in SharePoint 2007 that has Performance Point Dashboard web part. To uses the SharePoint site on a client machine that does not have PPS, remove
%@ Register TagPrefix="WpNS1" Namespace ="Microsoft.PerformancePoint.Scorecards.WebParts" ....

If Client machine has PPS installed, and when you add the PPS web part to your page this entry is automatically added.

Tuesday, February 10, 2009

IIS 7: Resolving Anonymous Logon to SQL from ASP.NET Application deployed on IIS7

If you face an issue wherein your ASP.NET application deployed in IIS7 connects to SQL (SQL Server 2008 Analysis Server) as anonymous user then do the following:-
1. Create a new application pool "MyPool" with Managed Pipeline mode set to classic, Load userprofile set to False and Identity set to custom and using a domain user (and password) that has access to the database.
2. Under 'authorization' of the website itself (this website in now under "MyPool" application pool), have Anonymous set to enabled and ASP.NET impersonate set to disabled.
3. In Web.config, leave impersonation="false".
http://mvolo.com/blogs/serverside/archive/2007/12/08/IIS-7.0-Breaking-Changes-ASP.NET-2.0-applications-Integrated-mode.aspx

Thursday, February 5, 2009

SharePoint: Sharepoint connects as Anonymous User to SQL Analysis Server on a Remote Machine and fails to retrieve data from CUBE

If you are trying to connect to a Remote Analysis Server 2008 (64 Bit) it connects as Anonymous User for one of the reasons.

1) Make sure the Authorization is Windows
2) Try this
In web.config Set
identity impersonate=" false"
When it is “true”
For User's IE , User a/c has to connect/open SQL server It requires kerbero’s website (setting that you do while setup of WebSite:Portnumber)
When it is “false”
For User's IE, website Identity Account has to connect/open SQL server
It does not require kerbero’s website

Monday, February 2, 2009

.NET: Reading Property Values using Reflection

class CA
{
private string _myProperty;
public string MyProperty
{
get
{
MyProperty = "Hello"; return MyProperty;
}
}
}


class ReadPropertyGeneric where T : class
{
Type myType;
T _objClass;
public void ReadAndSetProperty(T objClass)
{
myType = (typeof(T));
_objClass = objClass;
// Get the public properties.
PropertyInfo[] myPropertyInfo = myType.GetProperties(BindingFlags.Public BindingFlags.Instance);
Console.WriteLine("The mumber of public properties is {0}.", myPropertyInfo.Length);
// Display the public properties.
DisplayPropertyInfo(myPropertyInfo);
}
public void DisplayPropertyInfo(PropertyInfo[] myPropertyInfo)
{
// Display information for all properties.
for (int i = 0; i < myPropertyInfo.Length; i++)
{
PropertyInfo myPropInfo = (PropertyInfo)myPropertyInfo[i];
Console.WriteLine("The property name is {0}.", myPropInfo.Name);
Console.WriteLine("The property type is {0}.", myPropInfo.PropertyType);
Console.WriteLine("The property value is {0}.", myPropInfo.GetValue(_objClass, null));
object setValueString = myPropInfo.GetValue(_objClass, null) + "NewValue";
myPropInfo.SetValue(_objClass, setValueString, new object[0]);
}
}

Tuesday, January 20, 2009

SharePoint: Redirect Web Part to another Page

If you want to redirect from the Document Library's AllItems.aspx page to another page on "OnLoad Event", Create a Re direct Web Part which would re-direct to the desired page and add it to AllItems.aspx page

MSDN: U.S. Local Highlights