Wednesday, January 21, 2009

Handle back button of the browser

The "Back" browser button cannot be actually disabled by a web application as the browser security will not allow this.
If we want user to stay on the same page, even if user presses back button, then we can use javascript to achieve this.

<script type="text/javascript" language="javascript">
function Goback()

<body onload="Goback();">

This method will always redirect to the user on the current page itself.

For example, user is on page 1 and user goes to Page 2. Now from page 2 if user press back button of the browser then user will be redirect to the page 2 itself.

window.history contains the collection of the page visited by the user for particular browser session.
window.history.go(+1) and window.history.forward(); are same.

But if we want to display web page expired warning on the click of back button, then we must allow the browser to cache the page. This can be achieved by ASP.NET

/*To not to store the page in cache.*/

Response.ExpiresAbsolute = new DateTime(1900, 1, 1);

But one problem with this approach is that this will only work when there are some dynamic content in the page. i.e. change any combo or dropdown box value and then try to press back button.

If you can find out better solution then this please let me also know.


Saturday, January 03, 2009

Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack

Few days ago, i come across with this strange error. 
"Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack."
When I debugged my code i found that this error message is coming due to following statment.

I Used the following statement will fix the error.  

Response.Redirect internally calls to Response.End method due to this threadabort exception occurs. The Response.End method ends the page execution and shifts the execution to theApplication_EndRequest event in the application's event pipeline. The line of code that follows Response.End is not executed.

To work around this problem, use one of the following methods:

For Response.Redirect, use an overload, Response.Redirect(String url, bool endResponse) that  passes false for the  endResponse parameter to suppress the internal call to Response.End. For example:

Response.Redirect ("nextpage.aspx", false);

If you use this workaround, the code that follows Response.Redirect is executed.

For Server.Transfer, use the Server.Execute method instead.


Count number of tables in a SQL Server database

A simple query to find out number of tables in database is


SELECT COUNT(*) from information_schema.tables 
WHERE table_type = 'base table'



Whenever we write any procedure and execute it a message appears in message window that shows no of rows affected with the statement written in the procedure and we become very happy to see that our procedure is working. But do you know that this message creates an extra overhead on the network? Yes it does.

By removing this extra overhead from the network, we can actually improve the performance of our database and our application.

How should we do it?
When you create any procedure then first line of your procedure should be


This one line of code turns off the message that SQL server sends back to front end after every T-SQL statement is executed. This is applied for all SELECT, INSERT, UPDATE and DELETE statements. As when stored procedures are executed there is no need to pass this information back to front end.

When SET NOCOUNT is ON, the count is not returned. When SET NOCOUNT is OFF, the count is returned.If we still need to get the count of no of rows affected, we can still use @@ROWCOUNT option. Because The @@ROWCOUNT function is updated even when SET NOCOUNT is ON.

The setting specified by SET NOCOUNT is in effect at execute or run time and not at parse time.

Microsoft even realized the issue that this creates and has changed the stored procedure templates from SQL Server 2000 to SQL Server 2005.

Template used in SQL SERVER 2005

-- =============================================
-- Author:
-- Create date:
-- Description:
-- =============================================
-- Add the parameters for the stored procedure here
<@Param1, sysname, @p1> = ,
<@Param2, sysname, @p2> =
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.

-- Insert statements for procedure here
SELECT <@Param1, sysname, @p1>, <@Param2, sysname, @p2>


Setting SET NOCOUNT to ON can provide a significant performance boost, because network traffic is greatly reduced.


back to top