Friday, December 31, 2010

Crystal Reports .NET Error: Load Report Failed

Most of the ASP.NET developers might be familiar with this problem and faced it too. When we Google for it, most of the solutions indicate that the C:\Windows\Temp folder needs permission for NETWORK SERVICE account in Windows Server and ASP NET account in Windows Client.

On my web server i can clearly see temporary files of crystal reports being created when a report is called from ASP.NET page. It is recommended to set a special permission no C:\Windows\Temp folder as follows:

Add permission to List folder and Read
Add permission to Create files, Append files
Add permission to Delete files

This should fix the problem in 80% of the cases. Although there are other scenarios as well which cause this problem. But i have here indicated the one common problem, i hope it works for most of you and save you a day.

Another issue might be in the coding section, if report documents are not properly closed and disposed off, this will trigger the error as well. Whenever a report is called in a web application a copy in C:\Windows\Temp folder is created and than served to the client response.

You should inspect the C:\Windows\Temp folder to see if these temp files are not hanging around, if so that means the report documents are not properly close and disposed after processing. Crystal report document need to be closed by calling the Close() method and than the Dispose() method to clean.

There is a recommendation for this in SAP Crystal reports document, and the code should look similar to the following:
private void Page_Unload(object sender, EventArgs e)
{
if (boReportDocument != null)
{
boReportDocument.Close();
boReportDocument.Dispose();
GC.Collect();
}
}


Reference: Troubleshooting the “Load Report Failed” Error

SQL Database Snapshot

Create Database Snapshot on Current database:

USE TEST

GO

DECLARE @name VARCHAR(1000),
@filename VARCHAR(1000),
@dbname VARCHAR(1000),
@dbssname VARCHAR(1000),
@dbssfilename VARCHAR(1000)

DECLARE @hour VARCHAR(2),
@minute VARCHAR(2)

SET @hour = DATENAME(HOUR,GETDATE())
IF LEN(@hour) = 1 SET @hour = '0'+@hour
SET @minute = DATENAME(MINUTE,GETDATE())
IF LEN(@minute) = 1 SET @minute = '0'+@minute

SET @dbname = Db_Name();
SET @dbssname = @dbname+'_data_'+@hour+@minute
SET @dbssfilename = 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Data\'+@dbssname+'.ss'

SELECT @name = [name], @filename = [filename]
FROM sys.sysfiles
WHERE groupid = 1;


--CREATE SNAPSHOT
EXEC( 'CREATE DATABASE ' + @dbname + '_dbss' +@hour+@minute + ' ON
( NAME =' + @name + ', FILENAME = '''+@dbssfilename +''')
AS SNAPSHOT OF '+@dbname);


Restore Database to a Database Snapshot:
USE master;
GO
-- Reverting TEST to TEST_dbss1717
RESTORE DATABASE TEST from
DATABASE_SNAPSHOT = 'TEST_dbss1717';
GO

Drop Database Snapshot:
USE master;
GO
DROP DATABASE TEST_dbss1717;

Friday, November 26, 2010

ASP.NET Change Master page on Run time

As in my earlier post we looked at how we can change Theme of a page on click of a button, this similar principle is applied when we try to change master page on click of a button.

We again have the same question:

Can in Change master page on button click?
How to change master page on click of button?

The answer is again simple after reading the documentation of System.Web.UI.Page.MasterPageFile() property:

Property: Public Overridable Property MasterPageFile() As String
Member of: System.Web.UI.Page
Summary: Gets or sets the file name of the master page.
Exceptions:
System.InvalidOperationException: The System.Web.UI.Page.MasterPageFile
property is set after the System.Web.UI.Page.PreInit event is complete.
System.Web.HttpException: The file specified in does not exist or The page does not have a System.Web.UI.WebControls.Content control as the top level control.


The 'MasterPageFile' property can only be set in or before the 'Page_PreInit' event.

Same work around to change the MasterPageFile on run time on click of a button:

Partial Class Default
Inherits System.Web.UI.Page

Protected Sub btnChange_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles btnChange.Click
'Cannot change MasterPageFile on Click event
End Sub

Protected Sub Page_PreInit(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.PreInit
'If Page is postback
If Me.IsPostBack = True Then
If Not Request(btnChange.UniqueID) Is Nothing Then
Me.MasterPageFile = "~/NewMasterpage.master"
End If
End If

End Sub

End Class


We have changed the MasterPageFile in 'Page_PreInit' event but on click of a button.

UniqueID of a control gets the unique, hierarchically qualified identifier for the server control. The fully qualified identifier for the server control, this is the ID which is received as key when value is post back.

The output of button is like this:
<input type="submit" name="btnChange" value="Change Theme" id="btnChange" />

Here name is what we call btnChange.UniqueID and id is what we call btnChane.ClientID, in HTML input controls "id" is used for client side validation and other java script things, and "name" attribute is used to identify the field when it is post back to server.

Tuesday, November 23, 2010

ASP.NET Change Page Theme on Run time

A simple question about setting themes in ASP.NET would be of in which event I can set the or change the Theme of my page. Like if i want to give an option for user to select from list of available themes and the site on run time could be changed to a specific theme.

Can in Change theme on button click?
How to change theme on click of button?

The answer was simple after reading the documentation of System.Web.UI.Page.Theme() property:

Property: Public Overridable Property Theme() As String
Member of: System.Web.UI.Page
Summary: Gets or sets the name of the page theme.
Exceptions:
System.InvalidOperationException: An attempt was made to set System.Web.UI.Page.Theme after the System.Web.UI.Page.PreInit event has occurred.
System.ArgumentException: System.Web.UI.Page.Theme is set to an invalid theme name.


The 'Theme' property can only be set in or before the 'Page_PreInit' event.

I have made a work around to change the theme on run time on click of a button:

Partial Class Default
Inherits System.Web.UI.Page

Protected Sub btnChange_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles btnChange.Click
'Cannot change theme on Click event
End Sub

Protected Sub Page_PreInit(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.PreInit
'If Page is postback
If Me.IsPostBack = True Then
If Not Request(btnChange.UniqueID) Is Nothing Then
Me.Page.Theme = "NewTheme"
End If
End If

End Sub

End Class


We have still changed the theme in 'Page_PreInit' event but on click of a button.

UniqueID of a control gets the unique, hierarchically qualified identifier for the server control. The fully qualified identifier for the server control, this is ID which is received as key when value is post back.

The output of button is like this:
<input type="submit" name="btnChange" value="Change Theme" id="btnChange" />

Here name is what we call btnChange.UniqueID and id is what we call btnChane.ClientID, in HTML input controls "id" is used for client side validation and other java script things, and "name" attribute is used to identify the field when it is post back to server.