Archive

Archive for August, 2011

Exporting XML in a C# ASP .NET Web Application

August 16, 2011 2 comments

XML (extensible markup language) is a popular format of data for importing and exporting between different applications designed using different programming languages. Since XML uses a standardized format of data, applications can easily parse the XML data to pull out specific fields, blocks, and even write their own XML files. XML is especially useful as a protocol for communicating over the Internet with applications

using (System.IO.MemoryStream stream = new System.IO.MemoryStream())

{

// Create an XML document. Write our specific values into the document.

System.Xml.XmlTextWriter xmlWriter = new System.Xml.XmlTextWriter(stream, System.Text.Encoding.ASCII);

// Write the XML document header.

xmlWriter.WriteStartDocument();

// Write our first XML header.

xmlWriter.WriteStartElement(“WebApplications”);

// Write an element representing a single web application object.

xmlWriter.WriteStartElement(“WebApplication”);

// Write child element data for our web application object.

xmlWriter.WriteElementString(“Date”, DateTime.Now.ToString());

xmlWriter.WriteElementString(“Programmer”, “Primary Objects”);

xmlWriter.WriteElementString(“Name”, “Hello World”);

xmlWriter.WriteElementString(“Language”, “C# ASP .NET”);

xmlWriter.WriteElementString(“Status”, “Complete”);

// End the element WebApplication

xmlWriter.WriteEndElement();

// End the document WebApplications

xmlWriter.WriteEndElement();

// Finalize the XML document by writing any required closing tag.

xmlWriter.WriteEndDocument();

// To be safe, flush the document to the memory stream.

xmlWriter.Flush();

// Convert the memory stream to an array of bytes.

byte[] byteArray = stream.ToArray();

// Send the XML file to the web browser for download.

Response.Clear();

Response.AppendHeader(“Content-Disposition”, “filename=MyExportedFile.xml”);

Response.AppendHeader(“Content-Length”, byteArray.Length.ToString());

Response.ContentType = “application/octet-stream”;

Response.BinaryWrite(byteArray);

xmlWriter.Close();

}

Advertisements
Categories: ASP.Net, C#

Creating Crystal reports in ASP.NET

August 12, 2011 3 comments

Create a new website and right click on

solution explorer >

add new Item

Select Crystal Report

In the dialog box choose blank report

 

 

Now click on Crystal Report Menu in Visual Studio 2010 and select Database Expert

 

 

Select OLE DB under “Create New Connection” and select Data Provider.

 

 

Click on Next, In Next Screen provide Server name and credential. And select Database.

 

 

Now from Database field, drag and drop fields as required in the report.

 

Add New ASPX Page and drag Crystal Report viewer  on the form.

Click on smart tag of Report Viewer and choose report source and click on finish.

Here is the HTML markup.

<CR:CrystalReportViewer ID=”CrystalReportViewer1″ runat=”server”

AutoDataBind=”True” GroupTreeImagesFolderUrl=”” Height=”1202px”

ReportSourceID=”CrystalReportSource1″ ToolbarImagesFolderUrl=””

ToolPanelWidth=”200px” Width=”1104px” />

<CR:CrystalReportSource ID=”CrystalReportSource1″ runat=”server”>

<Report FileName=”CrystalReport.rpt”>

</Report>

</CR:CrystalReportSource>

 

Add following code in Page_load,

 

protected void Page_Load(object sender, EventArgs e)

{

ReportDocument crystalReport = new ReportDocument();

crystalReport.Load(Server.MapPath(“CrystalReport.rpt”));

CrystalReportViewer1.ReportSource = crystalReport;

}