Just another WordPress.com weblog

Archive for December, 2010

Query to take Nth higest values from table

SELECT TOP 1 VacationHoursFROM (SELECT DISTINCT TOP N VacationHoursFROM humanresources.Employee ORDER BY VacationHours DESC) aORDER BY VacationHours

Note : N – nth position

Advertisements

Handle custom errors in stored procedure in Sql Server

The RAISERROR statement is used to produce an ad hoc error message or to retrieve a custom message that is stored in the sys.messages table. You can use this statement with the error handling code presented in the previous section to implement custom error messages in your applications. The syntax of the statement is shown here.

RAISERROR ({msg_id |msg_str }{,severity ,state }
[ ,argument [ ,,...n ] ] ))
[ WITH option [ ,,...n ] ]

A description of the components of the statement follows.
msg_id :-The ID for an error message, which is stored in the error column in sysmessages.
msg_str :-A custom message that is not contained in sysmessages.
severity :- The severity level associated with the error. The valid values are 0–25. Severity levels 0–18 can be used by any user, but 19–25 are only available to members of the fixed-server role sysadmin. When levels 19–25 are used, the WITH LOG option is required.
state A value that indicates the invocation state of the error. The valid values are 0–127. This value is not used by SQL Server.

Eg:

USE tempdb
go
ALTER PROCEDURE ps_NonFatal_INSERT
@Column2 int =NULL
AS
DECLARE @ErrorMsgID int
INSERT NonFatal VALUES (@Column2)
SET @ErrorMsgID =@@ERROR
IF @ErrorMsgID <>0
BEGIN
RAISERROR ('An error occured updating the NonFatal table',10,1)
END

Note:

SELECT * FROM sys.messages return result in 11 different languages (total rows would be app. rows 98116)

TRY CATCH Block in Sql Server

The TRY…CATCH statement in Transact-SQL allows you to detect and handle error conditions gracefully within your database applications. This statement is the cornerstone of SQL Server error handling and is an extremely important part of developing robust database applications.
Syntax:
BEGIN TRY
{ sql_statement }
END TRY
BEGIN CATCH
{ sql_statement }
END CATCH

Caching Mechanism in ASP.NET

Caching is a feature of ASP.NET that improves the performance of web applications by minimizing the usage of server resources to a great extent.Caching is a feature that stores data in local memory, allowing incoming requests to be served from memory directly.

Types of Caching

Caching in ASP.NET can be of the following types

  • Page Output Caching – Caches the output from an entire page and returns it for future requests instead of re-executing the requested page Eg: <%@OutputCache Duration=”30″ VaryByParam=”none” %>
  • Page Fragment Caching – Caches just a part of a page which can then be reused even while other parts of the page are being dynamically generated     Eg : <%@ OutputCache Duration=”15″ VaryByControl=”EmpID;DeptID” VaryByParam=”*”%>
  • Data Caching – Programmatically caches arbitrary objects for later reuse without re-incurring the overhead of creating them. Eg….
public DataSet GetUserInfo()
{   
  string cacheKey = "UserInfo";   
  DataSet ds = Cache[cacheKey] as DataSet;   
  if (ds == null)   
 {     
   ds = GetUserInfoFromDatabase();     
   Cache.Insert(cacheKey, ds, null, NoAbsoluteExpiration,       
   TimeSpan.FromHours(15),CacheItemPriority.High, null);   
}      
return ds;
}   

DataSet GetUserInfoFromDatabase() {
// Usual code to populate a data set from thedatabase. This data set
// object is then returned.
}

Benefits of Caching

The following are the benefits of using Caching

  • Faster page rendering
  • Minimization of database hits
  • Minimization of the consumption of server resources

User Control Vs Custom Control

Both User Control and Custom Control can be created to avoid the work repeatation.

User Control:

  • User Controls (.ascx) derived from System.Web.UI.UserControl
  • Designed for single-application Deployed as .ASCX and source code of the application
  • If the same control needs to be used in more than one application, it introduces redundancy and maintenance problems
  • we have to use with in the application and it rests in the application directory.

Eg: A much better choice when you need static content within a fixed layout, for example, when you make headers and footers

Custom Control

  • Custom controls derived from System.Web.UI.WebControls.WebControl(if you want the control to render HTML that generates a visual interface on the client computer) or System.Web.UI.Control(if you want the control to render nonvisual elements. For example, <meta> and <head> are examples of nonvisual rendering)
  • Designed so that it can be used by more than one application
  • Deployed either in the application’s Bin directory or in the global assembly cache
  • Distributed easily and without problems associated with redundancy and maintenance
  • we can keep it in the Toolbox and use it when ever we want

Eg : More suited for when an application requires dynamic content to be displayed; can be reused across an application, for example, for a data bound table control with dynamic rows

Protected: Wrap a User Control inside a Web Part using WSPBuilder

This content is password protected. To view it please enter your password below:

Pages Types in SharePoint

First, let’s understand the main different between Application Pages and Content Pages:

  • Content Pages – Contains content controls that can be modified by end users.
    ONLY Content Pages can contain Web Part Zones, therefore can contain Web Parts.
  • Application Pages – Are regular ASPX pages that exposed in every site collection or sub site under the _layouts folder.

When we understand these main differences it is easy to know when to use each page, For example:

Scenario A – Content Pages
You need to create an RSS to show relevant content to users in a certain group and you need to support customization by sites administrators and personalization for end users.

Scenario B – Application Pages
Your company wants to provide a central page that displays the latest articles from Eggheadcafe.com. This page will be available for company employees and could not be configured by end users or sites administrators.

http://www.eggheadcafe.com/tutorials/aspnet/2145201e-00aa-4877-b5f7-cf150d7f0c5f/sharepoint-application-pa.aspx

Tag Cloud