Thursday, August 9, 2012

How to Print a Crystal Report Programmatically in ASP.NET?

You can print a Crystal Report using print option of Crystal Report Viewer. However, there are occasions when you want your application to print a report direct to printer without viewing the report in Crystal Report Viewer.
The ReportDocument class provides PrintToPrinter method that may be used to print a CR direct to the printer. If no printer is selected, the default printer will be used to send the printing pages to.
 
The PrintToPrinter method takes four parameters.

nCopies : Indicates the number of copies to print.
collated : Indicates whether to collate the pages.
startPageN : Indicates the first page to print.
endPageN : Indicates the last page to print.

The following steps will guide you to achieve the same:

  1. Add crystal report (.cr) file to your ASP.NET application.
  2. Add a report instance on the page level. 
    Dim report As MyReport = New MyReport

  1. Populate reports data on Page_Init  
      
    ' Get data in a DataSet or DataTable
        Dim ds As DataSet = GetData()
        ' Fill report with the data
     report.SetDataSource(ds)

  1. Print Report report.PrintToPrinter(1False00)

If you wish to print certain page range, change last two parameters From to To page number.

If you want to set page margins, you need to create a PageMargin object and set PrintOptions of the ReportDocument.

The following code sets page margins and printer name:

Dim margins As PageMargins =  Report.PrintOptions.PageMargins
   margins.bottomMargin = 200
   margins.leftMargin = 200
   margins.rightMargin = 50
   margins.topMargin = 100
   Report.PrintOptions.ApplyPageMargins(margins)

   ' Select the printer name
   Report.PrintOptions.PrinterName = printerName

Tuesday, August 7, 2012

SQL SERVER – Shrinking Truncate Log File – Log Full


Sometime, it looks impossible to shrink the Truncated Log file. Following code always shrinks the Truncated Log File to minimum size possible.
USE DatabaseName
GO
DBCC SHRINKFILE(<TransactionLogName>, 1)BACKUP LOG <DatabaseNameWITH TRUNCATE_ONLYDBCC SHRINKFILE(<TransactionLogName>, 1)GO
[Update: Please note, there are much more to this subject, read my more recent blogs. This breaks the chain of the logs and in future you will not be able to restore point in time. If you have followed this advise, you are recommended to take full back up right after above query.]

Wednesday, August 1, 2012

Remove (merge) SQL Servers' database secondary data file

 I have a database backup for which SQL Server Management Studio says that it has three files in it: an .mdffile, an .ndf file and one .ldf file. This secondary data file (the .ndf one) was created for no obvious reason, so I want to remove it altogether (without losing data, of course), preferably during while the database is being restored from the backup.



First back up the database.
Execute this:
USE database_name;
Then execute this:
DBCC SHRINKFILE('ndf_file_name', EMPTYFILE);
ALTER DATABASE database_name REMOVE FILE logical_ndf_file_name;

the default does not exist on the server. (sqlmanagerui) SQl server 2005

Error

the default does not exist on the server. (sqlmanagerui)

Solution
Try to use 

ALTER TABLE tablename DROP constraint constraintname