.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal
Post New Web Links

disposing SPSite and SPWeb objects?

Posted By:      Posted Date: September 03, 2010    Points: 0   Category :SharePoint
hi          can i code using(SPSite oSite=SPContext.Current.Site) {    using(SPWeb oWeb=oSite.OpenWeb()) {} } or should i code like this SPSite oSite=SPContext.Current.Site; using(SPWeb oWeb=oSite.Openweb()){} which is better? i am trying to code this inside ItemAdding Event handler. please suggest

View Complete Post

More Related Resource Links

Login to SPSite\SPWeb through console app using user/password

Hi,I'm developing some basic console application which interacts with SharePoint site through the object mode. I'm using:SPSite site = new SPSite(MySiteUrl); SPWeb web = site.OpenWeb(); This way, current windows user is acessing the site. Is it possible to supply the user with username/password to be able to acess with different credentials than current?Thanks in advance!

First Development Program with SPSite, SPWeb and SPList works only locally but not in farm

Dear All, Here is a piece of code which wrote in a VS 2010 with Standlone Sharepoint 2010 in Windows 7 64 bit and it works fine. Private Shared _SiteURL As String     Private Shared Sub Initialize(ByVal SiteURL As String)                 _SiteURL = SiteURL         Using sc As New SPSite(_SiteURL)             Dim spWebSite As SPWeb = sc.RootWeb             _AllSPList = New List(Of String)             _HiddenSPList = New List(Of String)             _VisibleSPList = New List(Of String)             For Each item As SPList In spWebSite.Lists                 _AllSPList.Add(item.Title)                 If item.Hidden = True Then                     _HiddenSPList.Add(item.Title)             &nb

179 heaps created, above warning threshold of 32. Check for excessive SPWeb or SPSite usage.

Hi All, I am gettings hundreds of following error messages in my sharepoint log files 179 heaps created, above warning threshold of 32. Check for excessive SPWeb or SPSite usage. Also I have noticed that after these error messages my sharepoint site is now dead slow. Any idea how to resolve this problem ?  

Error opening site using SPSite and SPWeb instances using VS 2010, Win Server 2008 R2


Hi I am trying to open my site using SPSite and SPWeb instances and getting an error message as below-

The Web application at http://url could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application.

  CODE is -



                    SPSite siteCOll = new SPSite(siteURL);

                    SPWeb oWeb = siteCOll.OpenWeb("url");



I can access site using browser perfectly.

I am running VS as administrator.

I have done permission checking aswell.


please help!





Error occured when using SPWeb or SPSite in webpart


I am creating a custom webpart, in which I have a label control. This label will display the current site URL and here is the code

protected override void CreateChildControls()
            SPWeb wb = SPContext.Current.Web;
            string baseURL = wb.Url.ToString();
            Label lblTitle = new Label();
            lblTitle.Text = baseURL.ToString();

But when I try to use SPWeb or SPSite within CreateChildControl method, I am getting an unexpected error occurred message. Also I have used "using Microsoft.SharePoint;" too.

If suppose I comment those two line my web part is displaying the properly. As below

protected override void CreateChildControls()
            //SPWeb wb = SPContext.Current.W

IDisposable SPSite and SPWeb fixes


I think those of you who know how to fix this will see the title and laugh....saying oh yea..I remember when I first discovered that.  So here we go.

MOSS2007.  Performance started going bad this week.  Very bad.  MS called in for support.  They ran a scan and pointed us to the stuff that we should address.  They pointed me at this MSDN Article basing on Stefan Goßner's writeup on fixing this stuff.  They asked me to run the SPDispose Utility against all of my custom code.  I did and the output showed no errors.

However, I scanned the log files of my DEV box, where I'm the only user.  I started some of my VS2008 Workflows.  I get the usual error

ERROR: request not found in the TrackedRequests. We might be creating and closing webs on different threads. ThreadId = 12, Free call stack =    at Microsoft.SharePoint.SPRequestManager.Release(SPRequest request)     at Microsoft.SharePoint.SPWeb.Invalidate()     at Microsoft.SharePoint.SPWeb.Close()     at Microsoft.SharePoint.SPSite.Close()     at Microsoft.SharePoint.SPSite.Dispose()     at RS_SDLC_Approv

Disposing of objects



Can someone answer me how the GC will dispose objects created in the following 2 scenarios:


Private Function DoSomething() As Boolean
	Return New DatabaseManager().Insert()
End Function


Private Function DoSomething() As Boolean
	Dim mngr As New DatabaseManager()
	Return mngr.Insert()
End Function

As you can see, in the option 1, i dont create local variable to hold the reference of the object. In the second option, i hold the reference in local variable.

What option is better and why? (if any)





Need help with objects and arrays


I have an objuct that I declared like this:

    Class DailyStats
        Dim _day As String
        Dim _hour As String
        Dim _impressions As String
        Dim _clicks As String
        Property Day() As String
                Return _day
            End Get
            Set(ByVal value As String)
                _day = value
            End Set
        End Property
        Property Hour() As String
                Return _hour

How to check for null/empty strings on dynamic objects?


What is the proper way to check for null or empty strings on dynamic objects? This is the only solution I've found so far:

if (admin.PasswordHash.GetType() == typeof(DBNull) || admin.PasswordHash == string.Empty)

If the field is null, the object returned is DBNull.Value, but if the field is empty string, it's return as a System.String object. This makes it impossible to do regular "admin.PasswordHash == DBNull.Value" check, as that will fail whenever the field has been set to empty string. Is my example the right (and only) way to go?

how to serialize objects ??


Is there any way or process to serialize the the objects?? i am trying to store the instance of few control in viewstate but it shows error msg that it is not a serialized object. should i perform some steps to serialize the objects ???

Class which contains an array of objects of type "Class2"



1  Partial Class _Default
2  Inherits System.Web.UI.Page

3 Public Function Ohjoy(ByVal X As Integer) As Integer
4 Dim theClass As New Class1 '= New Class1()
5    theClass.ReSize(0)
6    theClass.myBaby(0).GetName(0) = "Hello" 'Property Chars is read only'
7    Return 1
8 End Function
I have a first class named "Class1". I have a second class named "Class2" Class2, is a member of class 1,
and is implemented as an array of objects of type Class 2.

If I try and call the Accessor GetName() my hard coded value "Hello" in my accessor is not returned? Only
an alpha character like "T"

Also if I try and assigne a value using the setter GetName() it does not work, and the compiler gives me
an error message "property Chars is ReadOnly? Does anyone know what is wrong in my code? Line 6 above


Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
End Sub
End Class

Public Class Class1
    Dim bs As Integer
    Public myBaby() As Class2
    Public myB As Class2
    Public Function ReSize(By

Cutting Edge: Expando Objects in C# 4


The .NET Framework 4 introduces some new features that enable you to go beyond static types. We explore dynamically expando objects and demonstrate how they can be used like dynamically updatable dictionary objects.

Dino Esposito

MSDN Magazine July 2010

Cutting Edge: Pros and Cons of Data Transfer Objects


After a brief refresher on procedural and object based patterns for organizing the business logic layer, the author focuses on data transfer objects and the impact they have on the development of the software project.

Dino Esposito

MSDN Magazine August 2009

Test-Driven Design: Using Mocks And Tests To Design Role-Based Objects


Use Test-Driven Development with mock objects to design object oriented code in terms of roles and responsibilities, not categorization of objects into class hierarchies.

Isaiah Perumalla

MSDN Magazine June 2009

Basic Instincts: Inspecting COM Objects With Reflection


Reflection is useful for debugging and logging and otherwise providing the type information you need. Here you'll see how to use reflection on COM types.

Lucian Wischik

MSDN Magazine January 2009

Toolbox: Serializing objects, Scott Allen's blog, Site Performance, and more


Serialize and Deserialize Fixed Length and Delimited Files, Scott Allen's blog, inspecting Web Pages, and more.

Scott Mitchell

MSDN Magazine August 2008

Toolbox: Cross Browser Testing, Mock Objects, and Raymond Chen


This month test your web site on many platforms and browsers without setting up a test environment, use mock objects for unit testing, and visit Raymond Chen.

Scott Mitchell

MSDN Magazine June 2008

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend