Tuesday, September 19, 2006

Could not load file or assembly 'CrystalDecisions.CrystalReports'

I managed to get hold of Visual Studio 2005, and like any new programmers, I was rubbing my hands in glee at getting the latest toolkit for development.

When I tried to open an existing file in Visual Studio 2005, it prompted me to upgrade to the .NET framework and create a new webconfig file. Then my nightmare with debugging begins, especially with Crystal Reports. In particular, this error got me stumped

Could not load file or assembly 'CrystalDecisions.CrystalReports.Engine visual studio 2005 .

The error occurs in the web.config file.

add assembly="CrystalDecisions.CrystalReports.Engine, Version=10.2.3000.0, Culture=neutral, PublicKeyToken="

I tried everything I knew, googling it, getting deployment packages from BusinessObjects etc. But none of them work as all I want is to compile my code without the darn error and make sure the migration to .NET 2.0 is successful.

So after sitting in the dark without a clue to what the error may be for an hour and fed up with reading tons of articles that don't work, I decided I have enough of googling and time to do something. As a first step, I open the bin folder, and check the Crystall report dll. Oops! There is the darn error! The version number for the old dll is 9.1.9800.9 and the assembly version is 10.2.3000.0! So I delete all the dll, and add the new CrystalReport dll.

To my surprise, Visual Studio version number is higher than what the assembly version is 10.2.5150.0. Thinking myself smart, I did what I think is right and changed the assembly in the web.config file to

add assembly="CrystalDecisions.CrystalReports.Engine, Version=10.2.5150.0, Culture=neutral, PublicKeyToken="

Then I found out that there is no such assembly in the .NET 2.0. Only the Version 10.2.3000.0 is there in the assembly. All attempts to add the new version into the assembly failed. Since I cannot edit the assembly, I tried changing it back the original version:

add assembly="CrystalDecisions.CrystalReports.Engine, Version=10.2.3000.0, Culture=neutral, PublicKeyToken="

And hey pesto, everything suddenly can compile!

Morale of story: Make sure your dlls in the bin directory in your new .NET 2.0 application is updated to the latest version and leave the assembly code in the web.config file alone. It saves you a lot of pain in migrating to .NET 2.0.

Sunday, September 03, 2006

How to send Email using C#

In order to send email in C# in ASP.NET 1.1., do the following:
using System.Web.Mail;

// create mail message object
MailMessage mail = new MailMessage();
mail.From = ""; // put the from address here
mail.To = ""; // put to address here
mail.Subject = ""; // put subject here
mail.Body = ""; // put body of email here
SmtpMail.SmtpServer = ""; // put smtp server you will use here
// and then send the mail

In ASP.NET 2.0, the MailMessage class and Smtp.Mail is obsoleted. Instead, you have to use the following code:

public static void CreateTestMessage4(string server)
MailAddress from = new MailAddress("ben@contoso.com");
MailAddress to = new MailAddress("Jane@contoso.com");
MailMessage message = new MailMessage(from, to);
message.Subject = "Using the SmtpClient class.";
message.Body = @"Using this feature, you can send an e-mail message from an application very easily.";
SmtpClient client = new SmtpClient(server);
Console.WriteLine("Sending an e-mail message to {0} by using SMTP host {1} port {2}.",
to.ToString(), client.Host, client.Port);