Search This Blog

Monday, 30 April 2012

How to show error page in ASP.Net


Handle custom errors in ASP.Net

Once the ASP.Net application is ready to launh to customers, there is no need to show the error details in to the web page when any errors occurred. In this scenario we can show our customised page with details like “Server is busy now please after some time” to the customer. At the same time we can store error details to database or txt file for future reference for developers. For customise errors in the ASP.Net we need to do some settings in the web.config file.

Settings on web.config to show error page

<customErrors mode="On" defaultRedirect="error.aspx">
<error statusCode="404" redirect="404Error.aspx"/>
</customErrors>

The above code should be placed in the web.config file for handling custom errors (mode=”On”). Also we can handling particular erros with error code with appropriate error page. In above mentioned code, if a user request a page that is not in the server, 404 error is occurred then page is automatically direct to 404Error.aspx. If any other errors occurred, it will show web page error.aspx. In the same way we can handle any type of error with the status code, with the appropriate web page. 

How to use cache in ASP.Net ?


Why need to cache data in ASP.Net?

Most of the developers don’t know where can use cache mechanisam in ASP.Net.   For good performance of ASP.Net dynamic application we can use cache mechanism. This is nothing but we store some dynamic data into the cache for reusablility of web pages when user request the same again. That means suppose we have a link in our websites, that displays all the items in the database. When user clicks first time, it will fetch all data from the database and displayed to the web page. If a user trying to get same data for the second time, normally we are not able track whether the user tried same data before, so we are doing same procedure again (Fetching data from database and render to the html). As a optimisation part of the we bsite, we need to fetch same data only in single time for a user (Because new items inserted in to the database is rare case).

Simple codes to cache data in ASP.Net

To achieve tracking of user’s request, we can use cache mechanism. In the above example when a user trying to get all items from the database, we need to fetch this data and will store to cache object. When the same user trying to get same data for the second time, we need to check is there any data in the cache object, if it is there just displayed to the web page else taken from the db and store to the cache for future.

How to store a string variable to cache?

Cache["name"]="Smitha";

if (Cache["name"] != null)
    Label1.Text= Cache["name"].ToString();

To insert objects into the cache, the Add method or different versions of the Insert method of the Cache class can be used. These methods allow us to use the more powerful features provided by the Cache class. One of the overloads of the Insert method is used as follows: (How to store data object into cache in ASP.Net ?)
Cache.Insert("Name", strName,
    new CacheDependency(Server.MapPath("name.txt"),
    DateTime.Now.AddMinutes(2), TimeSpan.Zero);

The first two parameters are the key and the object to be inserted. The third parameter is of type CacheDependency and helps us set a dependency of this value to the file named name.txt. So whenever this file changes, the value in the cache is removed. We can specify null to indicate no dependency. The fourth parameter specifies the time at which the value should be removed from cache. [See example 5 for an illustration.] The last parameter is the sliding expiration parameter which shows the time interval after which the item is to be removed from the cache after its last accessed time.
The cache automatically removes the least used items from memory, when system memory becomes low. This process is called scavenging. We can specify priority values for items we add to the cache so that some items are given more priority than others:
Cache.Insert("Name", strName,
    new CacheDependency(Server.MapPath("name.txt"),
    DateTime.Now.AddMinutes(2), TimeSpan.Zero,
    CacheItemPriority.High, null);

How to create a always visible div in css and HTML


How to create a div that is always visible even while scrolling

In some scenario we need to show some information like ads,twitter like account’s logo,new updates etc..  always to user even he is scrolling website We can implement this using simple HTML and CSS within few steps. We can create more attractive div with given appropriate color and style to css.

Step by step process to create always visible div

Here we are explaining how easily we can create an always visible div using simple CSS in html by steps by step. We can create always visible div within 2 steps.

Step 1

Create a css class and name it "alwaysVisibleDiv" (you can give any name that you like). And add position as "fixed";


<style type="text/css">
<!--
.alwaysVisibleDiv
{
    position: fixed;
    bottom: 10px;
    left: 10px;
}
//-->
</style>


Step2

Add a new div to the page and specify the class="alwaysVisibleDiv"

<div class="alwaysVisibleDiv">
    Hello I am here always
</div>

Always visible div is ready. You can see above div position is always there (here we are fixed bottom left) even user scrolling website. We can fix this div at any position of the web page by giving bottom,right,top,left position to the class ‘alwaysVisibleDiv’. By using this html css code we can keep element in view while scrolling website without any jquery,ajax techniques. 

How to confirm before close browser using javscript?


Can we stop browser close using Javascript?

Some time it’s better to give a warning to user when he trying to close the browser. Some browsers has the facility by default but they can be disabling from the browser itself. So it’s better to give a warning message to user from the application itself. In this case we can use JavaScript for given the confirm message to user before close the browser window.

How to stop browser closing using javscript ?

We have to call a javascript funtion to give a confirmation message to user on browser closing event. We can achieve it by call a event ‘onbeforeunload’ from the body tag. This event will fired when a user close the browser. On this event we are calling a function quit(), On this function we are given a confirm message to user and return the result. If user clicks OK browser will close, if user press cancel it will return to the browser

Simple Javascript codes to prevent browser close

<html>
<head>
<script type="text/javascript">
function quit(){
event.returnValue="Are you sure you have finished?"
}
</script>
</head>
<body onbeforeunload="quit()">
<div id="timeDiv"></div>
</body>
</html>

How to display current time using javscript?


How ro create a Javascript clock?
Most of the websites are shown current time on top or bottom. But if we want to display updated time that means updated by each seconds, we can implemented JavaScript timer for the same. In some of the case we need to display count time or stopwatch have to display on our website, that time also we can implement same technique.

Javascript code for dynamic timer.
Below are the javascript codes for timer. There is a default function setTimeout available in javscript for repeated execution for a particular period.

<html>
<head>
<script type="text/javascript">
function startTime()
{
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
// add a zero in front of numbers<10
m=checkTime(m);
s=checkTime(s);
document.getElementById('timeDiv').innerHTML=h+":"+m+":"+s;
t=setTimeout('startTime()',00);
}

function checkTime(i)
{
if (i<10)
  {
  i="0" + i;
  }
return i;
}
</script>
</head>

<body onload="startTime()" bgcolor="white">
<div id="timeDiv"></div>
</body>
</html>