Pages

Sunday, October 17, 2010

Programmatically create a PDF in my .NET application

using CrystalDecisions.CrystalReports.Engine;

ReportDocument rptCust;
string sDate_time;
string sDestination_path;

CrystalDecisions.Shared.ExportOptions myExportOptions;
CrystalDecisions.Shared.DiskFileDestinationOptions File_destination;
CrystalDecisions.Shared.PdfRtfWordFormatOptions Format_options;

myExportOptions = new CrystalDecisions.Shared.ExportOptions();
File_destination = new CrystalDecisions.Shared.DiskFileDestinationOptions();
Format_options = new CrystalDecisions.Shared.PdfRtfWordFormatOptions();

sDate_time = DateTime.Now.ToString("ddMMyyyyHHmmssff");
sDestination_path = sDestination_file + sPolicy_number + sPolicy_number1 + "-" + sDate_time + ".pdf";

File_destination.DiskFileName = sDestination_path;
myExportOptions = rptCust.ExportOptions;

myExportOptions.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile;
myExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat;
myExportOptions.DestinationOptions = File_destination;
myExportOptions.FormatOptions = Format_options;

rptCust.Export();

Convert vb to c sharp project to C# 4.0

1. start a new C# project,
2. add the 4 class files that you have,
3. run them each through the VB->c# translator you linked to originally,
4. dump the VB logging stuff and add in log4net
5. turn the Windows Scripting stuff from VB into C# (I think your problem with this is that the translator above is flipping out on the types of WindowsScripting Host stuff)
6. Compile and test.

With luck, this will take you a couple of hours. With bad luck, it depends on what the project actually does and that will determine how long.

I wish you good luck.

If you decide to go this route, be liberal about commenting out huge parts of code and compiling and working on eliminating compiling errors first. I'll try to keep an eye out to help you with any other specific questions that I see come across the front page.

Saturday, October 16, 2010

Add delegate to interface c#

Those are declaring delegate types. They don't belong in an interface. The events using those delegate types are fine to be in the interface though:

public delegate void UpdateStatusEventHandler(string status);
public delegate void StartedEventHandler();

public interface IMyInterface
{
event UpdateStatusEventHandler StatusUpdated;
event StartedEventHandler Started;
}

The implementation won't (and shouldn't) redeclare the delegate type, any more than it would redeclare any other type used in an interface.

Saturday, October 2, 2010

c# DateTime, Trim without converting to string

Example1 
var
dt = DateTime.Now; // 10/1/2010 10:44:24 AM
var dateOnly = dt.Date; // 10/1/2010 12:00:00 AM

Example2
DateTime now = DateTime.Now;
DateTime today = now.Date;

Example3
DateTime now = DateTime.Now;
DateTime today = new DateTime(now.Year, now.Month, now.Day);

Reading Email using Pop3 in C#

Following code taken from POP3 Tutorial page and links would help you:

// 
// create client, connect and log in
Pop3 client = new Pop3();
client
.Connect("pop3.example.org");
client
.Login("username", "password");

// get message list
Pop3MessageCollection list = client.GetMessageList();

if (list.Count == 0)
{
Console.WriteLine("There are no messages in the mailbox.");
}
else
{
// download the first message
MailMessage message = client.GetMailMessage(list[0].SequenceNumber);
...
}

client
.Disconnect();

Create Excel (.XLS and .XLSX) file from C#

//Create the data set and table
DataSet ds = new DataSet("New_DataSet");
DataTable dt = new DataTable("New_DataTable");

//Set the locale for each
ds
.Locale = System.Threading.Thread.CurrentThread.CurrentCulture;
dt
.Locale = System.Threading.Thread.CurrentThread.CurrentCulture;

//Open a DB connection (in this example with OleDB)
OleDbConnection con = new OleDbConnection(dbConnectionString);
con
.Open();

//Create a query and fill the data table with the data from the DB
string sql = "SELECT Whatever FROM MyDBTable;";
OleDbCommand cmd = new OleDbCommand(sql, con);
OleDbDataAdapter adptr = new OleDbDataAdapter();

adptr
.SelectCommand = cmd;
adptr
.Fill(dt);
con
.Close();

//Add the table to the data set
ds
.Tables.Add(dt);

//Here's the easy part. Create the Excel worksheet from the data set
ExcelLibrary.DataSetHelper.CreateWorkbook("MyExcelFile.xls", ds);

Building JSON object to send to an AJAX WebService

[WebMethod]
public Response ValidateAddress(Request request)
{
return new test_AddressValidation().GenerateResponse(
test_AddressValidation
.ResponseType.Ambiguous);
}

...

public class Request
{
public Address Address;
}

public class Address
{
public string Address1;
public string Address2;
public string City;
public string State;
public string Zip;
public AddressClassification AddressClassification;
}

public class AddressClassification
{
public int Code;
public string Description;
}

Simpler way to Serializing objects in C# 4.0

[Serializable]
public class ContentModel
{
public int ContentId { get; set; }
public string HeaderRendered { get; set; }

public ContentModel()
{
ContentId = 0;
HeaderRendered = string.Empty;
}

public ContentModel(SerializationInfo info, StreamingContext ctxt)
{
ContentId = (int)info.GetValue("ContentId", typeof(int));
HeaderRendered = (string)info.GetValue("HeaderRendered", typeof(string));
}

public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info
.AddValue("ContentId ", ContentId);
info
.AddValue("HeaderRendered", HeaderRendered);
}
}

C# Polymorphism

Compile Time Polymorphism

Method overloading is a great example. You can have two methods with the same name but with different signatures. The compiler will choose the correct version to use at compile time.

Run-Time Polymorphism

Overriding a virtual method from a parent class in a child class is a good example. Another is a class implementing methods from an Interface. This allows you to use the more generic type in code while using the implementation specified by the child. Given the following class definitions:

public class Parent
{
public virtual void SayHello() { Console.WriteLine("Hello World!"); }
}

public class Child : Parent
{
public override void SayHello() { Console.WriteLine("Goodbye World!"); }
}

The following code will output "Goodbye World!":

Parent instance = new Child();
instance
.SayHello();

Early Binding

Specifying the type at compile time:

SqlConnection conn = new SqlConnection();

Late Binding

The type is determined at runtime:

object conn = Activator.CreateInstance("System.Data.SqlClient.SqlConnection");