This small article will describe , how to open SPSite and SPWeb from asp.net application and how to dispose it properly.
Since we are required to open a sharepoint object, so we need to refer Microsoft.Sharepoint.dll.
The location of the dll is:
Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI
Step 1. : Refer the above dll and add the namespace:
using Microsoft.Sharepoint; //C# code written
SPSite is an object to create an instance of a Sharepoint Site
SPWeb is an object to create an instance of a Sharepoint Web.
using (SPSite ospSite=new SPSite ("http://servername:portname"))
using (SPWeb ospWeb=ospSite.openWeb())
//At this point you can perform any operation on ospWeb and ospSite.
Here, I have used using to open an instance of SPSite/SPWeb object because this automatically dispose the object as the scope ends.
You don't required to close and dispose it again.
For more detials, check the following link: