Wednesday, March 2, 2011

Crystal Reports with Visual Studio

Recently I was trying to work with Crystal Reports for learning purpose and I have ran through couple of issues. Solutions to these issues were trivial but took good amount of time in research.

It goes without saying that you should have a valid installation of Visual Studio with crystal report basic and valid license on the server for running crystal reports.

Issue No. 1 - Can't see images for Crystal report viewer, or not able to use the export, print functionalities when deployed to another web server.

Solution : This happens because crystal repots viewer assumes a very specific directory structure for the images and scripts. If you are running IIS on your development machine, you can find the structure here "C:\inetpub\wwwroot\aspnet_client\System_Web\2_0_50727\CrystalReportWebFormViewer4"


Now all you have to do is that copy the content of this whole structure, right from aspnet_client to your webserver and create a virtual directory to point to this path. Even if you dont want to create a virtual directory or dont have access to do so, you can simply copy the full structure to the root of your webapp and that should do the magic.


If you are not using IIS and using a inbuilt webserver with Visual studio (cassini i.e.), still you will have this structure , only thing that in this case it would be available at C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\ASP.NETClientFiles

Inserting code in blogger was never so easy

I have always struggle for inserting code in my blog.
//Now it seems to be easy
Many thanks to him http://pleasemakeanote.blogspot.com/2008/06/posting-source-code-in-blogger.html for sharing the knowledge

Tuesday, February 22, 2011

Crystal Report Error - Field name is not known

If you are getting the error while running the crystal report like this. "This field name is not known". The below step should fix your problem.


Ensure that database view is updated corrected and correct field name is being reffered from Crystal Report.


Then open the crystal report in Crystal Report view and From the Database Menu, select option " Verify Database " .
It would ask you credentials and then would ask whether you want to update. Click Okay and Save the report file.


Now try to run the report again. You should see the report without any problem. This was the solution I saw recently somewhere, and thought of posting it for everyone else. Hope this helps someone.

Friday, September 10, 2010

CS0426: The type name XXX does not exist in the type XXX

Yesterday, I was getting below error continously when I was working on a sample of MasterPage.

CS0426: The type name 'MyMaster' does not exist in the type 'System.Web.UI.WebControls.TreeView'

While I tried a lot many things (deleting page, adding new etc.) and also googled couple of things, but could not get any clue. Later while reading a post I realized the problem.

Problem here was that I had named my project (and thereby assembly and the namespace) as TreeView. So at runtime, the CLR was trying to find my master page inside Sytem's standard treeview class.

So solution was simple i.e. just change the project and namespace name to a non reserved work like TreeviewPOC in my case.

So next time you get CS0426 error, just check if your namespace or project name is colliding with any of the standard (or even third party controls etc).

Happy Coding

Regards, Subhash Dike

Friday, August 20, 2010

How to unshelve when items are locked in other workspace

Ever faced a problem, where you have been given a shelveset but the owner has locked files in his shelveset.

Here is how you could get those files in your workspace without any issue.

Just execute the below command (put the original values in place of placeholders denoted by <>

tf view /server:<Your Server Name> shelveset:"<ShelvesetName>";<Owner in Domain\UserID format> <Path of item you want to unshelve> > <Path Where you want to copy those files>

For more information you can visit MSDN Documentation

Wednesday, February 17, 2010

How to avoid recurring TFS login from Visual Studio

Every time you open Visual studio (with tfs connected mode), it asks you to enter credentials. Even if you are trying to do some command line stuff for tfs, it will prompt you for credentials.

Let's try to make that easy.

Steps (Windows Vista)

  1. Control Panel, User Accounts, User Accounts (again);
  2. Click "Manage your network passwords" ("Tasks" list on the left);
  3. In the "Stored User Names and Password" dialog, click Add;
  4. Type in your Code-Plex (or any other TFS server for that matter)
    1. Add only the host name in the "Log on to" field ();
    2. Type username in the "Domain\FirstName.LastName" format;
    3. Select "A Windows logon credential" radio button.

 

Steps (Windows 7)

  1. Control Panel, User Accounts
  2. Click "Manage your credentials" (on the left);
  3. In the "Stored Credentials for Automatic logon" page, click "Add a windows credential";
  4. On the resultant page Type in your following details -Plex (or any other TFS server for that matter)
    1. Add only the host name in the "Network details" field (tfs server full name);
    2. Type username in the "Domain\FirstName.LastName" format;
    3. Type Password

Steps (Windows XP)

  1. Control Panel, User Accounts, Advanced tab
  2. Click Manage Passwords
  3. Click Add
  4. Enter the server name (tfs server name), user name (Domain\FirstName.LastName and password).

Friday, September 5, 2008

Bridge Design Pattern using C# .Net

Bridge Design Pattern:

[This article has been created from a presentation that was created by my wife-Anita. Special thanks to Anita for facilitating the presentation.]

Definition: Decouple an abstraction from its implementation so that both can vary independently

You will find many articles describing the theory about using Bridge Design Pattern using c# .net. Many articles including wiki has already listed down possible usage of this design pattern. I will simply start with a problem statement and we will see how this problem can be achieved.

Here is the class diagram of my vacation planner (The problem statement) and the code.

Here is the code

using System;

using System.Collections.Generic;

using System.Text;

namespace Agency

{

class Program

{

static void Main(string[] args)

{

TravelManager tm = new TravelManager();

utilize(tm);

FoodManager fm = new FoodManager();

utilize(fm);

//fm.PayBill();

GuideManager gm= new GuideManager();

utilize(gm);

Console.ReadKey();

}

public static void utilize(AgencyManager am)

{

am.Engage();

am.Release();

}

}

}

using System;

using System.Collections.Generic;

using System.Text;

namespace Agency

{

abstract class AgencyManager

{

public abstract void Engage();

public abstract void Release();

//public abstract void PayBill();

//public void PayBill()

//{

// Console.WriteLine("Amount debited to a/c");

//}

}

class TravelManager : AgencyManager

{

public override void Engage()

{

TravelController.BookVehicle();

}

public override void Release()

{

TravelController.ReleaseVehivle();

}

//public override void PayBill()

//{

// Console.WriteLine("Amount debited to a/c");

//}

}

class FoodManager : AgencyManager

{

public override void Engage()

{

FoodController.OrderFood();

}

public override void Release()

{

FoodController.FinishFood();

}

}

class GuideManager : AgencyManager

{

public override void Engage()

{

GuideController.HireGuide();

}

public override void Release()

{

GuideController.DeHireGuide();

}

//public override void PayBill()

//{

// Console.WriteLine("Amount debited to a/c");

//}

}

}

using System;

using System.Collections.Generic;

using System.Text;

namespace Agency

{

public class TravelController

{

public static void BookVehicle()

{

Console.WriteLine("Vehicle booked");

}

public static void ReleaseVehivle()

{

Console.WriteLine("Vehicle Released");

}

}

public class FoodController

{

public static void OrderFood()

{

Console.WriteLine("Food ordered.");

}

public static void FinishFood()

{

Console.WriteLine("Food finished.");

}

}

public class GuideController

{

public static void HireGuide()

{

Console.WriteLine("Guide hired.");

}

public static void DeHireGuide()

{

Console.WriteLine("Guide dehired.");

}

}

}

As listed down in the above code and class diagram I have a simple abstract class and 3 implementations. Now the problem starts when I start exploring the application more. Let's say I have to add a new method PayBill which are required only for two implementations i.e for TravelManager and GuideManager. However I want to keep the third implementation FoodManager away from this method. Since all of them are implementing the same abstract class, I cannot add this method at the abstract level since in that case it will compulsory for FoodManager to implement that method which I don't want. The other way is to add the method at individual classes. However that will invite unnecessary duplication of code.

There can be someother solution to this problem but we will see how to handle such situation using the Bridge Design Pattern.

So here is the class diagram and the resulting code which shows us how we can resolve this issue.

And here is the code

using System;

using System.Collections.Generic;

using System.Text;

namespace BridgeDesignPattern

{

class Program

{

static void Main(string[] args)

{

PaidAgencies agency = new PaidAgencies();

agency.AgencyManager = new TravelManager();

agency.Engage();

agency.Release();

agency.PayBill();

agency.AgencyManager = new GuideManager();

agency.Engage();

agency.Release();

agency.PayBill();

AddOnAgecies freeagency = new AddOnAgecies();

freeagency.AgencyManager = new FoodManager();

freeagency.Engage();

freeagency.Release();

freeagency.Reimburse();

Console.ReadKey();

}

}

}

using System;

using System.Collections.Generic;

using System.Text;

namespace BridgeDesignPattern

{

/// <summary>

/// Abstraction

/// </summary>

public abstract class AgencyAbstraction

{

AgencyManager agencyManager;

public AgencyManager AgencyManager

{

get { return agencyManager; }

set { agencyManager = value; }

}

public abstract void Engage();

public abstract void Release();

}

/// <summary>

/// Refined abstraction

/// </summary>

public class PaidAgencies : AgencyAbstraction

{

public override void Engage()

{

AgencyManager.Engage();

}

public override void Release()

{

AgencyManager.Release();

}

public void PayBill()

{

Console.WriteLine("Amount Debited to Account");

}

}

/// <summary>

/// Refine abstraction 2

/// </summary>

public class AddOnAgecies : AgencyAbstraction

{

public override void Engage()

{

AgencyManager.Engage();

}

public override void Release()

{

AgencyManager.Release();

}

public void Reimburse()

{

Console.WriteLine("Amount credited to Account");

}

}

}

using System;

using System.Collections.Generic;

using System.Text;

namespace BridgeDesignPattern

{

/// <summary>

/// This is implementor

/// </summary>

public abstract class AgencyManager

{

public abstract void Engage();

public abstract void Release();

}

/// <summary>

/// This is concrete implementor

/// </summary>

public class TravelManager : AgencyManager

{

public override void Engage()

{

TravelController.BookVehicle();

}

public override void Release()

{

TravelController.ReleaseVehicle();

}

}

/// <summary>

/// This is concrete implementor

/// </summary>

class FoodManager : AgencyManager

{

public override void Engage()

{

FoodController.OrderFood();

}

public override void Release()

{

FoodController.FinishFood();

}

}

/// <summary>

/// This is concrete implementor

/// </summary>

class GuideManager : AgencyManager

{

public override void Engage()

{

GuideController.HireGuide();

}

public override void Release()

{

GuideController.DeHireGuide();

}

}

}

using System;

using System.Collections.Generic;

using System.Text;

namespace BridgeDesignPattern

{

/// <summary>

///

/// </summary>

public class TravelController

{

public static void BookVehicle()

{

Console.WriteLine("Vehicle booked");

}

public static void ReleaseVehicle()

{

Console.WriteLine("Vehicle Released");

}

}

public class FoodController

{

public static void OrderFood()

{

Console.WriteLine("Food ordered");

}

public static void FinishFood()

{

Console.WriteLine("Food finished");

}

}

public class GuideController

{

public static void HireGuide()

{

Console.WriteLine("Guide hired");

}

public static void DeHireGuide()

{

Console.WriteLine("Guide dehired");

}

}

}

As you can see now we can add methods at the Abstraction level whereas clients are using the refined abstraction. This way we are free to make changes at the abstract level as well as at the implementation level.

I Hope this explains the bridge design pattern clearly. Please feel free to comment on this article in case you have any queries/suggestions/comments. I have this source code along with a presentation in a zip and I can provide the same free of cost. Email me at SubhashD987[AT]mail.com

Enjoy programming…

Friday, August 8, 2008

Create new folder, Upload document to sharepoint site – Maintain metadata

There was one requirement which I was working on recently. It was very simple to create a user control wherein the user should be able to select the file and upload it to a WSS 3.0 Document Library. Also the second part was user should be able to create folders in the document library and upload document inside the folder.

 

With this very simple task was stated to me, I was very happy to answer my boss on the spot that the, I have the design ready. But boss (being a boss !) told me, Hold On! The requirement also states that the user should be able to see his/her name after uploading document and creating folder. For this I had a very straight forward solution which is inbuild for WSS 3.0. I used following code to upload the document.

    /// <summary>

/// Uploads Document to the Site

    /// foldername: Name of folder to which the document is to be uploaded. (Eg. "Shared Documents")

    /// documentFIleName: FileName of the file to be uploaded. Typically FileUpload control's output after formatting gets in here (Eg. "C:\MyFile.Doc")

/// </summary>

public void UploadDocumentToSite(string foldername, string documentFileName)

{

SPSite site = SPControl.GetContextSite(HttpContext.Current);

SPWeb myWeb = site.AllWebs["123"]; //123 is the site id.

 

try

{

site.AllowUnsafeUpdates = true;

myWeb.AllowUnsafeUpdates = true;

 

SPFolder destFolder = myWeb.GetFolder(foldername);

SPUser user = myWeb.Users["Domain\\UserName"];

 

destFolder.Files.Add(documentFileName, content, user, user, DateTime.Now, DateTime.Now);

         //The above overload used will help me to maintain the metadata such as created by and modified by fields.

 

}

catch (Exception ex)

{

//Log the error here

}

finally

{

//Dispose the web and site

}

}

 

So far so good. The above code perfectly uploads the document to the document library and also maintains the metadata such as Created by/Modified By fields.

The challenge starts later. Now the same requirement had a provision that user should be able to create folders in the document library. Creating folder is simple

SPFolder destFolder = myWeb.GetFolder(foldername);

destFolder.SubFolders.Add(folderName);

Now the problem starts. The above SubFolders.Add method does not have any overload where I can pass on SPUser. Apparently what would happen is that the document gets uploaded with some other userid (or system account which was impersonated) but not with the specific userid I wanted.

Then I again searched on the web and somewhere down the line I got a hint which worked for me like anything. The below is the way to go about it.

SPFolder newFolder = destFolder.SubFolders.Add(folderName);

newFolder.Item["Modified By"] = user.ID;

newFolder.Item["Created By"] = user.ID;

newFolder.Item.Update();

 

Wow… This was so simple but still so important to know.. I am very thankful to the guy who floated this on the web. (Unfortunately I did not bookmarked the url, but I commented down there)..

Go ahead and try this out if this works for you.. if it does, keep the work spreading…

Enjoy Life .. Enjoy Programming.. ..

Let me know if you need any assistance on such issues…