Get nth highest and lowest salary of an employe

0 comments

Query to get nth(3rd) Highest Salary

 Select TOP 1 Salary as '3rd Highest Salary'
from (SELECT DISTINCT TOP 3 Salary from Employee ORDER BY Salary DESC)
a ORDER BY Salary ASC



Query to get nth(3rd) Lowest Salary


 Select TOP 1 Salary as '3rd Lowest Salary'
from (SELECT DISTINCT TOP 3 Salary from Employee ORDER BY Salary ASC)
a ORDER BY Salary DESC 

Tips to Increase performance of SQL Server

0 comments
Don’t use the * in your queries. A SELECT * creates a overload on table, Input/Output and network bandwidth.

All columns involved in indexes should appear on WHERE and JOIN clauses on the same sequence they appear on index.

Avoid VIEWs. Use them only when there are benefits of doing so.

Verify if a critical query gains performance by turning it in a stored procedure.

Select a random row with Microsoft SQL Server

0 comments
SELECT * column FROM table
ORDER BY NEWID()

Social Sharing Links

0 comments

<div id="share-buttons">

<!-- Facebook -->
<a href="http://www.facebook.com/sharer.php?u=http://www.simplesharebuttons.com" target="_blank"><img src="http://www.simplesharebuttons.com/images/somacro/facebook.png" alt="Facebook" /></a>

<!-- Twitter -->
<a href="http://twitter.com/share?url=http://www.simplesharebuttons.com&text=Simple Share Buttons" target="_blank"><img src="http://www.simplesharebuttons.com/images/somacro/twitter.png" alt="Twitter" /></a>

<!-- Google+ -->
<a href="https://plus.google.com/share?url=http://www.simplesharebuttons.com" target="_blank"><img src="http://www.simplesharebuttons.com/images/somacro/google.png" alt="Google" /></a>

<!-- Digg -->
<a href="http://www.digg.com/submit?url=http://www.simplesharebuttons.com" target="_blank"><img src="http://www.simplesharebuttons.com/images/somacro/diggit.png" alt="Digg" /></a>

<!-- Reddit -->
<a href="http://reddit.com/submit?url=http://www.simplesharebuttons.com&title=Simple Share Buttons" target="_blank"><img src="http://www.simplesharebuttons.com/images/somacro/reddit.png" alt="Reddit" /></a>

<!-- LinkedIn -->
<a href="http://www.linkedin.com/shareArticle?mini=true&url=http://www.simplesharebuttons.com" target="_blank"><img src="http://www.simplesharebuttons.com/images/somacro/linkedin.png" alt="LinkedIn" /></a>

<!-- Pinterest -->
<a href="javascript:void((function()%7Bvar%20e=document.createElement('script');e.setAttribute('type','text/javascript');e.setAttribute('charset','UTF-8');e.setAttribute('src','http://assets.pinterest.com/js/pinmarklet.js?r='+Math.random()*99999999);document.body.appendChild(e)%7D)());"><img src="http://www.simplesharebuttons.com/images/somacro/pinterest.png" alt="Pinterest" /></a>

<!-- StumbleUpon-->
<a href="http://www.stumbleupon.com/submit?url=http://www.simplesharebuttons.com&title=Simple Share Buttons" target="_blank"><img src="http://www.simplesharebuttons.com/images/somacro/stumbleupon.png" alt="StumbleUpon" /></a>

<!-- Email -->
<a href="mailto:?Subject=Simple Share Buttons&Body=I%20saw%20this%20and%20thought%20of%20you!%20 http://www.simplesharebuttons.com"><img src="http://www.simplesharebuttons.com/images/somacro/email.png" alt="Email" /></a>

</div>

WCF Basics Information

0 comments
WCF Means : Windows Communication Foundation

Windows Communication Foundation (Code named Indigo) is a programming platform and run-time system for building, configuring and deploying network-distributed services. It is the latest service oriented technology; Interoperability is the fundamental characteristics of WCF. It is unified programming model provided in .Net Framework 3.0. WCF is a combined feature of Web Service, Remoting, MSMQ and COM+. WCF provides a common platform for all .NET communication.
Features of WCF

Create log files to handle the exceptions using log4net.dll to track entire application

0 comments
Exception Handling Using Log4net dll
  1. first you need to download the log4net dll add then click add reference to your project
  2. Add the below lines to your web.config file
    <configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>

Then add the below lines to below the configsection block in web.config file

<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="file" value="Logs\\logfile" />
<param name="appendToFile" value="true" />
<param name="rollingStyle" value="Date" />
<param name="StaticLogFileName" value="false"/>
<param name="datePattern" value="yyyyMMdd.\tx\t" />
<layout type="log4net.Layout.PatternLayout">
<param name="conversionPattern" value="%date{yyyy-MM-dd HH:mm:ss-fff} %-5level: [%logger] %message%newline" />
</layout>
</appender>
<root>
<level value="All" />
<appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>

Add few lines of code to Global.asax.cs file

public class Global : HttpApplication
{
private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

protected void Application_Start(object sender, EventArgs e)
{
log4net.Config.XmlConfigurator.Configure();
Log.Info("Application Started");
}

protected void Session_Start(object sender, EventArgs e)
{
}

protected void Application_BeginRequest(object sender, EventArgs e)
{
}

protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
}

protected void Application_Error(object sender, EventArgs e)
{
var ctx = HttpContext.Current;
var exception = Server.GetLastError();

if (exception == null || exception.Message == "File does not exist.")
return;

var errorInfo = string.Format("Offending URL: {0}\r\n Session: {5}\r\n Source: {1}\r\n Message: {2}\r\n Detail Message: {3}\r\n Stack trace: {4}",
ctx.Request.Url,
exception.Source,
exception.Message,
exception.InnerException.Message,
exception.StackTrace,
ctx.Session.SessionID);

Log.Error(errorInfo);
Session["ErrorInfo"] = errorInfo;
}

protected void Session_End(object sender, EventArgs e)
{
}

protected void Application_End(object sender, EventArgs e)
{
Log.Info("Application Closed");
}
}

Now create one page default.aspx in that code behind file

Add two namespace at the top of the page

using log4net;
using System.Reflection;


public partial class _Default : System.Web.UI.Page
{
private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
try
{
Log.Error("No Error");
}
catch (Exception ex)
{
Log.Error("Error");
}
finally
{
}
}

}
}



Database Sql server to check or monitor or trace the free space/used/ file size in MB

1 comments
Database to check or monitor free space available

select convert(decimal(12,2),round((a.size-fileproperty(a.name,'SpaceUsed'))/128.000,2)) as FreeSpaceMB from dbo.sysfiles a where fileid = 1;

Database to check or monitor File size MB

select convert(decimal(12,2),round(a.size/128.000,2)) as FileSizeMB from dbo.sysfiles a where fileid = 1;

Database to check or monitor Space Used size in MB

select convert(decimal(12,2),round(fileproperty(a.name,'SpaceUsed')/128.000,2)) as SpaceUsedMB from dbo.sysfiles a where fileid = 1;

Iframe some browsers will not maintain session values

0 comments
When you using iframe some browser's will not maintain session values. In order to maintain session values properly add the below code in Global.asax file

protected void Application_BeginRequest(object sender, EventArgs e)
{
// for ie browser to maintain session values when using iframe
HttpContext.Current.Response.AddHeader("p3p", "CP=\"IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT\"");


}

Asp.net Membership tables auto growing when you use shopping cart sites. Use this query to delete records

0 comments
Asp.net Membership tables will grow automatically when you use shopping cart sites. After some period the database size will full and you will get the error as

"Could not allocate space for object 'dbo.aspnet_Profile'.'PK__aspnet_Profile__2057CCD0' in database '' because the 'PRIMARY' filegroup is full. Create disk space by deleting unneeded files, dropping objects in the filegroup, adding additional files to the filegroup, or setting autogrowth on for existing files in the filegroup."

So to delete use the below query in stored procedures or by using triggers to perform delete operation

DECLARE @Days int
SET @Days = 21
DECLARE @NoOfUsersToDelete int
SET @NoOfUsersToDelete = 1000


IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PagesToDelete]') AND type in (N'U'))
DROP TABLE [dbo].[PagesToDelete]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[aspnetUsersToDelete]') AND type in (N'U'))
DROP TABLE [dbo].[AspnetUsersToDelete]
create table PagesToDelete (PageID int NOT NULL PRIMARY KEY)
create table AspnetUsersToDelete (UserID uniqueidentifier NOT NULL PRIMARY KEY)

insert into AspnetUsersToDelete
select top(@NoOfUsersToDelete) UserID from aspnet_Users where
(isAnonymous = 1) and (LastActivityDate < (getDate()-@Days))
order by UserID
print 'Users to delete: ' + convert(varchar(255),@@ROWCOUNT)
GO
insert into PagesToDelete
select ID from Page where UserID in
(
select UserID from AspnetUsersToDelete
)
print 'Pages to delete: ' + convert(varchar(255),@@ROWCOUNT)
GO

delete from WidgetInstance where PageID IN
( SELECT PageID FROM PagesToDelete )

print 'Widget Instances deleted: ' + convert(varchar(255), @@ROWCOUNT)
GO

delete from Page where ID IN
( SELECT PageID FROM PagesToDelete )
GO
delete from UserSetting WHERE UserID IN
( SELECT UserID FROm AspnetUsersToDelete )
GO
delete from aspnet_Profile WHERE UserID IN
( SELECT UserID FROm AspnetUsersToDelete )
GO
delete from aspnet_UsersInRoles WHERE UserID IN
( SELECT UserID FROm AspnetUsersToDelete )
GO
delete from aspnet_PersonalizationPerUser WHERE UserID IN
( SELECT UserID FROm AspnetUsersToDelete )
GO
delete from aspnet_users where userID IN
( SELECT UserID FROm AspnetUsersToDelete )
PRINT 'Users deleted: ' + convert(varchar(255), @@ROWCOUNT)
GO
drop table PagesToDelete
drop table AspnetUsersToDelete

GO
=================================
Second method by using triggers to delete
=================================
CREATE TRIGGER DELETE_Aspprofileanduserstable
on aspnet_Profile
after update
as
begin
Delete from aspnet_profile
where userid in ( select userid from aspnet_users
where isanonymous = 1
and datediff(dd, lastactivitydate, getdate())> 14)


Delete from aspnet_users
where userid in ( select userid from aspnet_users
where isanonymous = 1
and datediff(dd, lastactivitydate, getdate())> 14)

end

go

Vikram Chandra Theme by BloggerThemes & NewWPThemes Sponsored by iBlogtoBlog