Constructors and Destructors (C#)

This is for my quick references and I have taken most of the information from MSDN and other web sites. BTW I have written and tested the source!

Constructors
– Constructors are methods that are executed when an object of a class is created.
– They have the same name as the class.
– A class  may have multiple constructors that take different arguments.
– Constructors enable the programmer to set default values.
– Like class, structs also can have constructors.

There are 3 kind of constructors in .Net (as far as I know), they are

  • Instance Constructors -Used to create and initialize instances of the class
  • Private Constructors – A special type of instance constructor that is not accessible outside the class (cannot be instantiated)
  • Static Constructors – Automatically initialize the class before the first instance is created or any static members are referenced
Example:
public class A //Class Name A
{
public int i;
public A() // Constructor of A
{
i=1;
}
}
class Program
{
static void Main(string[] args)
{
A a = new A(); //Initiated with new Operator
Console.WriteLine("The initialized value is: {0}", a.i);

System.Console.ReadLine(); //This line is to stop the result window
}
}

Destructors
– are used to destruct instances of classes.
– cannot be defined in structs. They are only used with classes.
– A class can only have one destructor.
– cannot be inherited or overloaded.
– cannot be called. They are invoked automatically
– does not take modifiers or have parameters.

Example:
class A
{
public A()
{
Console.WriteLine("Constructor");
}
~A()
{
Console.WriteLine("Destructor");
}
}

 

class Program
{
static void Main(string[] args)
{
A a = new A(); //Initiated with new Operator

System.Console.ReadLine(); //This line is to stop the result window
}
}
Advertisement

Leave a comment

Content Editor Web Part (CEWP) & JavaScript duplicating entries in SharePoint 2010

Recently we have migrated our SharePoint 2007 application to SharePoint 2010 and we also had manually configured few web parts in the new environment. Most of the web parts were working perfectly like sharepoint 2007 but one content editor web part which shows the weather forecast had some wired repeating behavior! 😦

Here is the weather forecast web part in the 2007 which has JavaScript to update the weather from their web site.

The JavaScript code in the Source editor:

I have created the CEWP in 2010 and paste the source code in to the editor.

and Publish the page, It is working fine no? but not really..!

Whenever I edit the page, the web part is duplicating the weather widget like this!

After spending few minutes I have found the solution in this post, actually SharePoint 2010 does not support the JavaScript directly into the source editor. So we cannot copy and paste the JavaScript to the CEWP editor instead, we have to create the scripts as a txt file (paste into a Notepad and save), upload into the SharePoint and then pointing that file as the source of CEWP.

Step 1: Open a Notepad and paste the source code

Step 2: Upload the txt file into SharePoint

Step 3: Get the url path of the txt file and paste it on the content link property of the content editor pane on the right side and click ok.

That’s all. No duplication probs any more. Happy Migrating..!

Thanks. R./

References:

1. http://sharepointadam.com/2010/08/31/insert-javascript-into-a-content-editor-web-part-cewp/

, , , , , , ,

3 Comments

Unexpected Error has occurred. Error is ‘SetParent failed for Database ‘AR633564737049916886′.’

When I was installing the Application Definition Designer, I have got the following wired error message! 😦

The text format of this error message is:

Unexpected Error has occurred. Error is 'SetParent failed for Database 'AR633564737049916886'.' --> SetParent failed for Database '<em>AR633564737049916886'.' --> Failed to connect to server. \BDC. --> An error has occurred while establishing a connection to the server. When connecting to SQL Server..

This  is not even a brief message to understand no? After spending few minutes, I have found the possible causes for this error.

1. Not enough permissions to configure BDC (Business Data Catalog) instance

2. There is no BDC instance on our SQL Server, this means the installation expects the BDC instance to continue.

In my case, I have adequate permissions for BDC, so the problem is 2nd one. If we run the BDCStudioSetup.msi file directly, Instead of running the setup.exe on the “BDC Definition Editor” directory then we get this error! 😦

You can see the complete installation and configuration of Application Definition Designer here.

Happy configuring..! Thanks. R./

References:
1. http://www.techyv.com/questions/setparent-failed-database-error-while-installing-data-catalog

2. http://www.microsoft.com/en-us/download/details.aspx?id=79

, , , , , , ,

2 Comments

Installing and configuring the “Application Definition Designer” tool – BDC

Everyone knows about the usage of Application Definition Designerthis is a Microsoft tool that comes with the SharePoint Server SDK. You can see the overview and the system requirements here. While installing this tool I have faced few problems, so I just wanted to keep the steps in my bolg.

Step 1: Download the tool and double click the “OfficeServerSDK.exe”, You will be getting this screen.

Step 2: Click Next 😦

Step 3: The Office SharePoint Server 2007 SDK has been successfully installed. Great!

Step 4: Navigate to SDK installation path\Tools\BDC Definition Editor\. The default installation path for the MOSS SDK is <%Program Files%>\2007 Office System Developer Resources\. For me this is the path..

C:\Program Files \2007 Office System Developer Resources\Tools\BDC Definition Editor

Step 5: In this directory, double click the setup.exe

Please Note: If you double click  the BDCStudioSetup.msi file directly, Instead of clicking setup.exe then you will get this error This happened to me when I installed! 🙂

Step 6: Click Accept and continue, this will configure the SQL client.

Step 7: Click Next and close! 🙂

Step 8: That’s all, you can run the “ApplicationDefinitionDesigner.exe” in the “All Programs”

Step 9: We are ready to create the ADF (Application Definition File) to configure the BDC (Business Data Catalog) now.

Thanks. R./

References:

1. http://www.microsoft.com/en-us/download/details.aspx?id=79

2. http://nareshbojja.wordpress.com/2008/09/08/setparent-failed-for-database/

, , , , , , , , ,

3 Comments

There are no Business Data Types loaded in the Catalog – Business Data Type Picker

I was working on BDC (Business Data Catalog), to display the user profile data from SQL Server in SharePoint 2007. I was using “Application Definition Designer”  (A Microsoft tool that comes with the SharePoint Server SDK, you can see the installation and configuration here) to generate the ADF (Application Definition File). I tried to access the Business Data Types on the SharePoint page and then I have got the following wired error message. 🙂

BDC is not a really new stuff, so I tried to find the solution on the web but I didn’t get any direct solution immediately. Most of them are suggested to check the permission but I have adequate permission for BDC. Finally I have got the solution form this thread. Actually this is a bug in the Application Definition Designer and I don’t really know why it’s still there!

There might be two reasons to getting this error.

1. Not having enough permission to access the BDC

2. The “Finder” method is not generating properly in the “MethodInstanceType” property.

In my case I had adequate permisioion for the BDC, so the  “Finder” method was my problem.

You can see the “MethodInstanceType” property has “IdEnumerator” method instead of the “Finder”. So just click the drop down list and select the Finder method.

Then just export the ADF again, the Business Data Type Picker is showing the entries now huh? 🙂

References:

1. http://social.msdn.microsoft.com/forums/en-US/sharepointbdc/thread/c069a168-5dd9-4f5e-90bf-872e0ce00d28 

2. http://lightningtools.com/blog/archive/2010/08/19/there-are-no-business-data-types-loaded-in-the-catalog.aspx

, , , , , , , , , , ,

2 Comments

Deploy and Re-deploy a web part solution package (.wsp) using STSADM command.

Deploying a web part solution to the SharePoint using STSADM is an easy task, everyone knows about it. But while developing a web part, sometimes we need to re-deploy the solution again and again. In this case we cannot simply re-deploy the solutions and we have to follow some additional steps to redeploy again.

There are several ways to create the solution file, but I have used the WSPBuilder to create this web part solution. You can see this in my previous post.

Deploy :

There are 2 steps to deploy a solution using stsadm.

  1. Add solution – Add a solution file to the solution store. See more details here.
    • Syntax: stsadm -o addsolution   -filename <solution file name>
  2. Deploy solution – Deploys files related to a solution from the configuration database to individual front-end Web servers in the farm. See more details here.
    • Syntax: stsadm -o deploysolution   -name <solution name>   -immediate  -allowgacdeployment   -force –allcontenturls
  3. We need one more command to Executes all administrative timer jobs immediately instead of waiting for the timer job to run. See more details here.
    • Syntax:stsadm -o execadmsvcjobs

Ok, we will see the steps here:

Step 1: Open the cmd prompt and change the directory (cd) to stsadm.exe location, I am using SharePoint 2007 so this is the path:  “cd c:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\bin”

Step 2: Adding the solution, my solution file is in the C:\Temp\

stsadm -o addsolution -filename C:\Temp\WSPBuilderStepBiStep.wsp

Step 3: Deploying the solution

stsadm -o deploysolution -name WSPBuilderStepBiStep.wsp -immediate -allowGacDeployment -force -allcontenturls

Step 4: Executing the timer jobs

stsadm -o execadmsvcjobs

Please note: The name and filename in the above commands, if it is filename we have to use full file path.

Re-Deploy:

When we deploy the solution package without retract and delete then we will get the following error message:

There are many ways to do this (Using UI, WSPBuilder and so…) but here are the steps which I normally used (Specially in the production servers):

  1. Retract solution: Retracts the specified solution’s deployment which removes files from the front-end Web server. This is actually reversing the deployment. See more details here.
    • Syntax: stsadm -o retractsolution   -name <solution name>  [-immediate]  [-allcontenturls]
  2. Delete solution:Removes a solution file (package) from the solution store. See more details here.
    • Syntax: stsadm -o deletesolution    -name <name>    [-override]

Here are the steps:

Step 1: Retracting the solution

stsadm -o retractsolution -name WSPBuilderStepBiStep.wsp -immediate –allcontenturls

Step 2: Executing the timer jobs

stsadm -o execadmsvcjobs

Step 3: Deleting the solution

stsadm -o deletesolution -name WSPBuilderStepBiStep.wsp –override

Step 4: Re-deploy the solution again, for this follow the steps 1 to 3 in the Deploy!

That’s all, Happy deploying..! 🙂

, , , , ,

2 Comments

Developing a simple web part (.wsp) using WSPBuilder

Before we started the development, we have to install the WSPBuilder (SharePoint WSP tool), an extensions for Visual Studio. Just download the latest version here and install it. It is pretty easy steps.

Step 1: Create a new project on visual studio and then select the project type WSPBuilder  under C# programming language (I am using Visual Studio 2008).

Step 2: This is will create the following structure in the solution explorer.

Step 3: Right click on the project (WSPBuilderStepBiStep) and then add a new Item.

Step 4: This will give the following dialog box and then select WSPbuilder and Web Part Feature template. Enter the name and click Add button (I have given WebPartStepBiStep here)

Step 5: We will get the following dialog box and asking for the scope for this Web Part. I just modified the description and select the scope and then click OK.

Step 6: The solution explorer will look like this. Under the 12 hive folder you can see the Template and Features sub folders.

Step 7: Double click  on the WebPartStepBiStep.cs file and add your custom code on the right place. I just modifed the property like this:

public string MyProperty
        {
            get
            {
                if (_myProperty == null)
                {
                    _myProperty = "Hello Step Bi Step!";
                }
                return _myProperty;
            }
            set { _myProperty = value; }
        }

If you wanna to add a label rather than using this LiteralControl just replace the following code

this.Controls.Add(new LiteralControl(this.MyProperty));

with this

System.Web.UI.WebControls.Label label=new System.Web.UI.WebControls.Label();
label.Text = "Hello StepBiStep!";
this.Controls.Add(label);

Step 8: After replacing the code, the CreateChildControls() method will look like this:

        protected override void CreateChildControls()
        {
            if (!_error)
            {
                try
                {
                    base.CreateChildControls();
                    // Your code here...
                    System.Web.UI.WebControls.Label label=new System.Web.UI.WebControls.Label();
                    label.Text = "Hello StepBiStep!";
                    this.Controls.Add(label);
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
        }

Step 9: To build the project, just right click on the project name (WSPBuilderStepBiStep –> Build) and click Build.

Step 10: To build the WSP, Right click on the project name(WSPBuilderStepBiStep) –> WSPBuilder –> Build WSP

Step 11: This will create the .dll and .wsp files.


Step 12: To deploy the web part (.wsp file), right click on the project name(WSPBuilderStepBiStep) –> WSPBuilder –> Deploy


Step 13: You will get the following output, saying the .wsp file deployed successfully (done!).


Step 14: Go to the Web Part gallery and  populate the deployed Web Part on the New web part page.

Step 15: To use this Web Part in to your page, just follow these steps from my previous post. That’s all! Happy WSPBuilding..! 🙂

Please note: You have to run the Visual Studio as Administrator, otherwise you will get this error when you deploy the web part.

References:

1. http://rasor.wordpress.com/2008/10/12/wss-dev-quickstart/

, , , , , , , , , ,

2 Comments

How to add a custom web part to SharePoint 2007

Everyone knows about this, but I wanted to re-use this for my next series of custom web part posts. 🙂
I have already developed and deployed  the web part (WebpartStepBiStep.wsp) using STSADM command, you can see my previous post here. Now we will see the steps:

Step 1: On the SharePoint site, click Site Actions –> Site Settings –>Modify all site settings

Step 2: Site Settings page, click Web Parts under Galleries

Step 3: On the Web Part Gallery page, click New

Step 4:On the New Web Parts page, select the check box next to the Web Part that you want to add, and then click Populate Gallery

Step 5: Web part will be showing on the web part gallery.

Step 6: Click Site Actions, and then click Edit Page

Step 7: Click Add a Web Part

Step 8: In the Add Web Parts dialog box,  select the check box next to the Web Part that you want to add, and then click Add button

Step 9: The web part will be shown on your page.

Step 10: If you want modify the appearance and layout then Click Publish.

Step 11: That’s all, we are done.

Reference:
http://support.microsoft.com/kb/939306

, ,

2 Comments

Could not load file or assembly ‘CrystalDecisions.CrystalReports.Engine, Version=10.5.3700.0, Culture=neutral, PublicKeyToken=692fbea5521e1304’ or one of its dependencies. The system cannot find the file specified.

I have developed a windows application which generates report using basic Crystal Reports in Visual Studio 2008. It was working fine in my development environment but on the client machine it was throwing this error message when I try to access the report.

When I click the details of the error message, I have got the following:

System.IO.FileNotFoundException: Could not load file or assembly 'CrystalDecisions.CrystalReports.Engine, Version=10.5.3700.0, Culture=neutral, PublicKeyToken=692fbea5521e1304' or one of its dependencies. The system cannot find the file specified.
File name: 'CrystalDecisions.CrystalReports.Engine, Version=10.5.3700.0, Culture=neutral, PublicKeyToken=692fbea5521e1304'
at DevInventory.InventoryReport.buttonPrint_Click(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message&amp; m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message&amp; m)
at System.Windows.Forms.ButtonBase.WndProc(Message&amp; m)
at System.Windows.Forms.Button.WndProc(Message&amp; m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog]

After spending few hours I realized the Crystal Reports run time need to be installed to access the report on the client machine. You can find the instruction in the support website.

According to the environment (Visual Studio, 64 or 32 bits) , you can download the .msi packages from the SAP website OR you can find the package in your local drive (ie: C:\Program Files (x86)\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages\CrystalReports10_5).

Then install this into the client machine, that ‘s all!

Happy Reporting..! 🙂

References:

1. http://www.codeproject.com/Questions/127276/Crystal-report-doesnt-work-in-client-s-pc

2. http://wiki.sdn.sap.com/wiki/pages/viewpage.action?pageId=56787567

, ,

10 Comments