Dear Friends,
I am a beginner into .net world.
I have requirement to print a Bill for a sale in my current project. I have heard that we can use crystal report instead of html reports. But i never used.
I am writing the sale details to a sale table and i want to take it as a formatted output using crystal reports.
Somebody pls send me some sample code and pls help me in connecting the data base to crystal report and take the output.
I am using SQL server 2000 as back end and c# for code behind.
Any Piece of info is appreciatable,,
Thanks in Advance
Manuonce i get to work this morning i'll find the code where i display a CR through asp.net... i'll be back in about an hour or so
ok this is in vb but you should be able to make out what i'm doing... you will notice though that i don't actually display it in crystal's web browsing object. it never look right as far as the formating goes. so in order to get around that i could export it to a pdf that would be saved to a directory and upon the user's session ending the file(s) would be deleted then. heres the code. also create you crystal report and save the rpt file some place with in the website, much like i did. also keep in mind this code is crappy and i admit it... it's when i first started my job so i apologize crappyness of it ;P
Imports System
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports System.IO
Partial Class ReportPrinter : Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim rpt As New CrystalDecisions.CrystalReports.Engine.ReportClass
Dim UniqueFileName As String = MakeUniqueFileName()
Dim strExportFile As String = String.Format("{0}\{1}.pdf", Server.MapPath("~\Reports\Exports"), UniqueFileName) '.MapPath(".") + "\Reports\Exports\" + Session.SessionID.ToString + ".pdf"
Dim DiskOpts As CrystalDecisions.Shared.DiskFileDestinationOptions = _
New CrystalDecisions.Shared.DiskFileDestinationOptions
If Page.Request.QueryString("Quotation") <> "" AndAlso Page.Request.QueryString("Quotation") = "true" Then
Page.Title = "Quotation"
rpt.FileName = Server.MapPath("~\Reports\Quotation.rpt") 'Server.MapPath(".") + "\Reports\Quotation.rpt"
Dim QuoteID As Decimal = Decimal.Parse(Page.Request.QueryString("QuoteID"))
Dim Revision As String = Page.Request.QueryString("Revision")
For Each parm As CrystalDecisions.Shared.ParameterField In _
rpt.ParameterFields
With parm
Dim ParmName As String = .ParameterFieldName
Select Case ParmName.ToLower
Case "@.pQuoteID".ToLower
.CurrentValues.AddValue(QuoteID)
Case "@.pRevision".ToLower
.CurrentValues.AddValue(Revision)
Case "TermsConditions".ToLower
'Load the terms and conditions from the file.
.CurrentValues.AddValue(LoadTermsAndConditions())
Case "CompanyName".ToLower
'Load the company name
.CurrentValues.AddValue(LoadCompanyInfo(True))
Case "CompanyContactInfo".ToLower
'Load the company address info and phone and fax
.CurrentValues.AddValue(LoadCompanyInfo(False))
End Select
End With
Next
ElseIf Page.Request.QueryString("CustPros") <> "" AndAlso Page.Request.QueryString("CustPros") = "true" Then
Page.Title = "Customer/Prospect Report"
rpt.FileName = Server.MapPath("~\Reports\CustomerProspects.rpt") 'Server.MapPath(".") + "\Reports\CustomerProspects.rpt"
End If
'PaperSize and PaperOrientation must be set here to avoid a possible problem
'with the report being given the default printers paper size and paper
'orientation.
rpt.PrintOptions.PaperSize = PaperSize.PaperLetter
rpt.PrintOptions.PaperOrientation = PaperOrientation.DefaultPaperOrientation
With rpt.ExportOptions
.ExportDestinationType = ExportDestinationType.DiskFile
.ExportFormatType = ExportFormatType.PortableDocFormat
DiskOpts.DiskFileName = strExportFile
.DestinationOptions = DiskOpts
End With
rpt.Export()
rpt.Close()
rpt.Dispose()
DiskOpts = Nothing
Response.Redirect("Reports/Exports/" + UniqueFileName + ".pdf", False)
End Sub
Private Function LoadTermsAndConditions() As String
Dim fTermConditions As New IO.StreamReader(Server.MapPath("~\Reports\TermsConditions.txt")) '"C:\Inetpub\wwwroot\QuoteManager\Reports\TermsConditions.txt")
Dim retval As New StringBuilder
Do While fTermConditions.Peek > -1
retval.AppendLine(fTermConditions.ReadLine)
Loop
fTermConditions.Close()
fTermConditions.Dispose()
fTermConditions = Nothing
Return retval.ToString
End Function
Private Function LoadCompanyInfo(ByVal GetCompanyName As Boolean) As String
Dim retval As New StringBuilder
Dim xDoc As New Xml.XmlDocument
Dim xpath As String = "//CompanyInfo/Info"
xDoc.Load(Server.MapPath("~\Reports\CompanyInfo.xml")) '"C:\Inetpub\wwwroot\QuoteManager\Reports\CompanyInfo.xml")
For Each xn As Xml.XmlNode In xDoc.SelectNodes(xpath)(0).ChildNodes
If TypeOf xn Is Xml.XmlElement Then
Dim xe As Xml.XmlElement = DirectCast(xn, Xml.XmlElement)
If String.Equals(xe.Name.ToLower.Trim, "companyname") AndAlso GetCompanyName Then
retval.Append(xe.Attributes(0).Value)
Exit For
Else
Select Case xe.Name.ToLower.Trim
Case "address1"
If xe.Attributes(0).Value.Trim.Length <> 0 Then
retval.AppendLine(xe.Attributes(0).Value.Trim)
End If
Case "address2"
If xe.Attributes(0).Value.Trim.Length <> 0 Then
retval.AppendLine(xe.Attributes(0).Value.Trim)
End If
Case "city"
If xe.Attributes(0).Value.Trim.Length <> 0 Then
retval.Append(xe.Attributes(0).Value.Trim + ", ")
End If
Case "state"
If xe.Attributes(0).Value.Trim.Length <> 0 Then
retval.Append(xe.Attributes(0).Value.Trim + " ")
End If
Case "zip"
If xe.Attributes(0).Value.Trim.Length <> 0 Then
retval.AppendLine(xe.Attributes(0).Value.Trim)
End If
Case "phone"
If xe.Attributes(0).Value.Trim.Length <> 0 Then
retval.AppendLine("Phone:" + ControlChars.Tab + xe.Attributes(0).Value.Trim)
End If
Case "fax"
If xe.Attributes(0).Value.Trim.Length <> 0 Then
retval.AppendLine("Fax:" + ControlChars.Tab + xe.Attributes(0).Value.Trim)
End If
Case "webaddress"
If xe.Attributes(0).Value.Trim.Length <> 0 Then
retval.AppendLine(xe.Attributes(0).Value.Trim)
End If
Case "emailaddress"
If xe.Attributes(0).Value.Trim.Length <> 0 Then
retval.AppendLine(xe.Attributes(0).Value.Trim)
End If
End Select
End If
End If
Next
xDoc = Nothing
Return retval.ToString
End Function
Private Function MakeUniqueFileName() As String
Dim UID As String = Page.User.Identity.Name.Substring(Page.User.Identity.Name.LastIndexOf("\") + 1)
Dim ID As String = Date.Now.ToString.Replace("\", "").Replace("/", "").Replace(":", "").Replace(" ", "")
Return String.Concat(UID, ID)
End Function
End Class
0 comments:
Post a Comment