Configuring Database Mail

March 13, 2008

Configuring Database Mail

Advertisements

Export Reports into Diffrent File Formate using Crystal Reports Service

March 12, 2008

In this article I am going to explain , how to convert crystal reports to different file format using crystal reports service , The export file format that supported by our Crystal Reports as follows :

1) PDF

2) MS Word

3) MS Excel

4) HTML

5) RTF

please do the following steps :

Creating a class file ExportReports.cs

Export

Please place the following code in that file

public ExportReports()
{
//
// TODO: Add constructor logic here
//
}

public enum ReportFormats : int
{
None = 0,
PDF = 1,
MS_Word = 2,
MS_Excel = 3,
HTML = 4,
RTF = 5

}

public byte[] ExportReportsTo(DataList<T> dt, string fileName,

ReportFormats format)
{

CrystalDecisions.CrystalReports.Engine.ReportDocument doc = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
//DataSet ds = (DataSet)context.Cache.Get(“ExportData”);
doc.Load(fileName);
doc.SetDataSource(dt);
CrystalDecisions.Shared.ExportOptions exportOpts = doc.ExportOptions;
if (format == ReportFormats.PDF)
{
exportOpts.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat;
}
else if (format == ReportFormats.MS_Word)
{
exportOpts.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.WordForWindows;

}
else if (format == ReportFormats.MS_Excel)
{
exportOpts.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.Excel;

}
else if (format == ReportFormats.HTML)
{
exportOpts.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.HTML40;

}
else if (format == ReportFormats.RTF)
{
exportOpts.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.RichText;

}
exportOpts.ExportDestinationType = CrystalDecisions.Shared.

ExportDestinationType.NoDestination;
exportOpts.DestinationOptions = new CrystalDecisions.Shared.

DiskFileDestinationOptions();

// Response.Write(doc.ExportToStream(exportOpts.ExportFormatType));
byte[] _stream = new byte[1024];
MemoryStream _ms = new MemoryStream();

_ms = (MemoryStream)doc.ExportToStream(exportOpts.ExportFormatType);
return _ms.ToArray();
}

}

the parameter dt support following format :

1)Data set

2) Data Table

3)Data Reader

4) System.Collection.IEnumerable (in this example we used)

you can change the parameter according to your wish

After creating class file we can you this service in your web page (aspx)

before you should create rpt file (eg: NewSubscriptionReports.rpt)

Create Report

Use IDE to design crystal Report Templates :

IDE

Save this rpt file to a web folder then use the following code in the page

protected void btnExport_Click(object sender, EventArgs e)
{

yournamespace.ExportReports.ReportFormats format;
format = ExportReports.ReportFormats.None;
string strExt = “”;
if (ddlFormat.SelectedValue.Equals(“1”))
{
format = ExportReports.ReportFormats.PDF;
strExt = “Pdf”;
}
else if (ddlFormat.SelectedValue.Equals(“2”))
{
format = ExportReports.ReportFormats.MS_Word;
strExt = “Doc”;
}
else if (ddlFormat.SelectedValue.Equals(“3”))
{
strExt = “xls”;
format = ExportReports.ReportFormats.MS_Excel;

}
else if (ddlFormat.SelectedValue.Equals(“4”))
{
format = ExportReports.ReportFormats.HTML;
strExt = “HTML”;
}
else if (ddlFormat.SelectedValue.Equals(“5”))
{
strExt = “RTF”;
format = ExportReports.ReportFormats.RTF;
}

ExportReports exportReports = new ExportReports();
// System.IO.MemoryStream _ms = new System.IO.MemoryStream();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ContentType = “application/” + strExt;
HttpContext.Current.Response.AddHeader(“Content-Disposition”, “inline; filename=NewSubscriptionReports.” + strExt);

HttpContext.Current.Response.BinaryWrite(exportReports.ExportReportsTo

(SchoolService.GetSchoolsSubscriptionReports

(SchoolService.ReportType.NewSubscriptionReport, startDate, endDate, allReopts, 0, 0), Server.MapPath(“~/Maintenance/SubscriptionReports

/ReportTemplates/NewSubscriptionReports.rpt”), format));
HttpContext.Current.Response.End();

}

please specify the .rpt file path in calling function:

Server.MapPath(“~/Maintenance/SubscriptionReports/ReportTemplates/NewSubscriptionReports.rpt”),

Photo Resize Handler in ASP.net

March 12, 2008

Introduction to ASP.NET HTTP Modules and Handlers

All requests to IIS are handled through Internet Server Application Programming Interface (ISAPI) extensions. ASP.NET has its own filter to ensure pages are processed appropriately. By default, the ASP.NET ISAPI filter (aspnet_isapi.dll) only handles ASPX, ASMX, and all other non-display file formats used by .NET and Visual Studio. However, this filter can be registered with other extensions in order to handle requests to those file types, too, but that will be covered later.

Every request flows through a number of HTTP modules, which cover various areas of the application (i.e. authentication and session intofmation). After passing through each module, the request is assigned to a single HTTP handler, which determines how the system will respond to the request. Upon completion of the request handler, the response flows back through the HTTP modules to the user.

The HTTP Pipeline

To understand the role of HTTP modules within ASP.NET, you need to understand how the HTTP pipeline works. Once an HTTP request comes in over port 80 (the regular HTTP port or port 443 for HTTPS and the secure sockets layer), the request passes through a number of stages making up the HTTP pipeline before it’s actually fielded by your application.

Microsoft® Internet Information Services (IIS) is the first participant in the chain. IIS is still used to route the request to the ASP.NET runtime. IIS maps the ASP.NET file extensions to ASPNET_ISAPI.DLL, an ISAPI extension provided with ASP.NET. The job of ASPNET_ISAPI.DLL is to forward the request to the ASP.NET worker process, ASPNET_WP.EXE. From that point on, the request is wrapped up into an instance of HttpContext and piped through a number of ASP.NET classes. The HttpContext class includes members representing such things as the Response and the Request and also includes security information along with anything else you might want to know about the request.

The next step for the request is to pass through an instance of HttpApplication.After the HttpApplication object massages the request, it pushes the request through one or more HttpModule objects. There are a number of system-level HTTP modules.The final piece of the chain is the HttpHandler

HTTP Module

In order to insure that your HttpHandler is hooked up to the HTTP pipeline requires an entry in the application’s web config. or the machine.config file. The same holds true for HTTP modules. When an HTTP Module is added to the pipeline, the ASP.NET runtime will call the methods of the IHttpModule interface at the appropriate times. When the module is first created by the runtime, the Init method is called. As illustrated above, the parameter passed into the Init method allows your code to tie into the HttpApplication object.

The following table lists the key events exposed by the HttpApplication object. Note that all these events are implemented as multicast delegates so that numerous modules can register for each one.

Event
When It’s Called

BeginRequest
Before request processing starts

AuthenticateRequest
To authenticate client

AuthorizeRequest
To perform access check

ResolveRequestCache
To get response from cache

AcquireRequestState
To load session state

PreRequestHandlerExecute
Before request sent to handler

PostRequestHandlerExecute
After request sent to handler

ReleaseRequestState
To store session state

UpdateRequestCache
To update response cache

EndRequest
After processing ends

PreSendRequestHeaders
Before buffered response headers sent

PreSendRequestContent
Before buffered response body sent


In addition to the above methods, we can also respond to the methods in the global.asax file for the HttpApplication object. These events include the session start and end events, as well as the start and end events for the application.

HTTP Handlers

HTTP handlers are the .NET components that implement the System.Web.IHttpHandler interface. Any class that implements the IHttpHandler interface can act as a target for the incoming HTTP requests. HTTP handlers are somewhat similar to ISAPI extensions. One difference between HTTP handlers and ISAPI extensions is that HTTP handlers can be called directly by using their file name in the URL, similar to ISAPI extensions.

HTTP modules are classes that have access to the incoming request. These modules can inspect the incoming request and make decisions that affect the internal flow of the request. After passing through the specified HTTP modules, the request reaches an HTTP handler, whose job it is to generate the output that will be sent back to the requesting browser. The following diagram illustrates the pipeline an ASP.NET request flows through.

Photo Resize Handler

In this article we’ll create a HTTP Handler in ASP.NET to resize an image as per parameters that user passes.

Creating the Handler

please go and find topic from that site http://www.dotnetic.com/dnet/?q=node/5

please use following code to PhotoResizeHandler.ashx.cs ( Append)

define a method for resize image in same file :

private static byte[] ResizeImageFile(byte[] imageFile, int height, int width)
{
using (Image oldImage = System.Drawing.Image.FromStream(new MemoryStream(imageFile)))
{
int newwidth = 0;
int newheight = 0;
float oldwidth = oldImage.Width;
float oldheight = oldImage.Height;
float imgratio = oldwidth / oldheight;
if (imgratio > 1)
{
newwidth = width;
newheight = Convert.ToInt32 ( height / imgratio);
}
else
{
newheight = height;
newwidth = Convert.ToInt32(width * imgratio);
}
Size newSize = new Size(newwidth, newheight);
using (Bitmap newImage = new Bitmap(newSize.Width, newSize.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb))
{
using (Graphics canvas = Graphics.FromImage(newImage))
{
//canvas.SmoothingMode = SmoothingMode.AntiAlias;
//canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
//canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
canvas.DrawImage(oldImage, new Rectangle(new Point(0, 0), newSize));
MemoryStream m = new MemoryStream();
newImage.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg);
return m.GetBuffer();
}
}
}
}

Invoking Handler from aspx page

You would invoke the handler from aspx page just like you call any other aspx page.
Following Image control invokes the handler:

<asp:Image ID=”Image1″ ImageUrl=”~/PhotoResizeHandler.ashx?imageFile=images/Leaves.jpg&height=200&width=200″
runat=”server” />

or like this :

ImageButton lnkPhotoThumb = ( (ImageButton)( e.Item.FindControl( “PhotoThumb” ) ) );

lnkPhotoThumb.ImageUrl = Page.ResolveUrl( “~/Handlers/PhotoResizeHandler.ashx?imageFile=” + Page.ResolveUrl( “~/UploadedFiles/Resources/”)Leaves.jpg + “&height=120&width=120” );

ref: http://www.dotnetic.com/dnet/?q=node/5

Also , a big thanks to Shelly (my colleague) who provide me the logic of maintaining the aspect ratio of images 🙂