SQL SERVER – Query to Retrieve the Nth highest record from database table

You can replace the “Table” table with your table name and “N” with the level of salary to be determined.

Here is query to find Nth Highest Record from Database Table.

Method 1: Using sub query
SELECT *
FROM Table t1
WHERE (N-1) = (SELECT COUNT(DISTINCT(t2.salary))
FROM Table t2
WHERE t2.salary > t1.salary)

Method 2: Using Top
SELECT TOP 1 Salary
FROM (
SELECT DISTINCT TOP N Salary
FROM Table1
ORDER BY Salary DESC
) AS Emp
ORDER BY Salary

Share this:
Share

Asp.net: How to Read Application Settings from the Web.config File at Runtime

This example reads an application setting identified by the key customsetting1 from a Web.config file. The appSettings element is a NameValueCollection collection of strings. Working with collection elements can be slightly more complicated than working with other configuration elements.

To obtain configuration settings for the root-level Web configuration, null is passed to the OpenWebConfiguration method.

To update a configuration setting, use the Save or SaveAs method of the configuration object. For more information, see Using the Configuration Classes. For additional code examples, see the AppSettingsSection class and related classes.

This example uses the non-static method of obtaining configuration data, which allows you to pull configuration data from any application. If you are going to obtain configuration information from the application in which your code resides, use the static method, which processes faster. For more information, see the Working with Local and Remote Configuration Settings section in ASP.NET Configuration API Overview.

Example:

C#:
System.Configuration.Configuration rootWebConfig1 = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(null);
if (rootWebConfig1.AppSettings.Settings.Count > 0)
{
System.Configuration.KeyValueConfigurationElement customSetting = rootWebConfig1.AppSettings.Settings[“customsetting1”];

if (customSetting != null)
Console.WriteLine(“customsetting1 application string = \”{0}\””, customSetting.Value);
else
Console.WriteLine(“No customsetting1 application string”);
}

VB.Net:
Dim rootWebConfig1 As System.Configuration.Configuration
rootWebConfig1 = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Nothing)
If (rootWebConfig1.AppSettings.Settings.Count > 0) Then
Dim customSetting As System.Configuration.KeyValueConfigurationElement
customSetting = rootWebConfig1.AppSettings.Settings(“customsetting1”)
If Not (customSetting.Value = Nothing) Then
Console.WriteLine(“customsetting1 application string = {0}”, customSetting.Value)
Else
Console.WriteLine(“No customsetting1 application string”)
End If
End If

Robust Programming
Values read from the appSettings element of the Web.config file are always of type String. If the specified key does not exist in the Web.config file, no error occurs. Instead, an empty string is returned.

Security
The configuration file should be protected on the server by using Windows security settings to limit who can read the file. Avoid storing sensitive information such as user credentials in the appSettings element of the Web.config file. Also consider encrypting configuration settings.

Share this:
Share

Asp.net Difference between appSettings and Connection Strings in web.config

In previous versions of ASP.NET, connection strings were stored in the appSettings. In ASP.NET 2.0, features, such as Session, Membership, Personalization, and Role Manager, rely on connection strings that are stored in the connectionStrings element. You can also use the connectionStrings element to store connection strings for your own applications.

The difference is that the connectionString section gives you strongly typed access to your connection strings through the ConfigurationManager class. It’s meant for connection strings specifically. The connectionStrings element specifies a collection of database connection strings, as name/value pairs, for ASP.NET applications and features.

A connectionString object is an XML node that has specific attributes to set; and semantically it refers to a database connection string.

Example:
<connectionStrings>
<clear/>
<add name=”LocalSqlServer” connectionString=”Data Source=(local);Initial Catalog=(DBName);Integrated Security=True” providerName=”System.Data.SqlClient” />
</connectionStrings>

You’ll notice it has a few different attributes:

  • name
  • connectionString : This has a specific string inside of it, it needs an Initial Catalog, a security mechanism (in this case Integrated Security
  • providerName

AppSettings is meant to store general settings in web.config is used to store server names, file paths, and other miscellaneous settings needed by an application.. You can use it to store connection strings also, but I recommend not doing that since there is a specific element for it in connectionStrings.

appSettings is just a user-defined Key-value pair that allows you to… well… set application settings.

It can be anything:

Example:
<appSettings>
<add key=”Email” value=”abc@abc.com”/>
<add key=”MasterKey” value=”True”/>
<add key=”GoogleAPI” value=”1234567890-AA”/>
</appSettings>

In many cases, it would just be odd to put the connectionString in a key-value pair like appSettings (semantically and programmatically). As well as it would make it more difficult to encrypt the connectionString when you need to.

Share this:
Share

C# – Example when and why to use delegates

Why to use Delegate:
All of us have been exposed to event driven programming of some sort or the other. A delegate can be seen as a placeholder for a/some method(s) and used to pass methods as arguments to other methods. Event handlers are nothing more than methods that are invoked through delegates. You create a custom method, and a class such as a windows control can call your method when a certain event occurs.

Delegates in c# are type safe objects which are used to hold reference of one or more methods. Delegate represents references to methods with a particular parameter list and return type. When you instantiate a delegate, you can associate its instance with any method with a compatible signature and return type.

An interesting and useful property of a delegate is that it does not know or care about the class of the object that it references. Any object will do; all that matters is that the method’s argument types and return type match the delegate’s. This makes delegates perfectly suited for “anonymous” invocation.

What is the use of Delegates?
Suppose if you have multiple methods with same signature (return type & number of parameters) and want to call all the methods with single object then we can go for delegates.

Delegates are two types
–  Single Cast Delegates
–  Multi Cast Delegates

Single Cast Delegates:
Single cast delegate means which hold address of single method like as explained in above example.

Example:
public delegate int DelegatSample(int a,int b);

public class Sampleclass
{
public int Add(int x, int y)
{
return x + y;
}
public int Sub(int x, int y)
{
return x – y;
}
}
class Program
{
static void Main(string[] args)
{
Sampleclass sc=new Sampleclass();

DelegatSample delgate1 = sc.Add;
int i = delgate1(10, 20);
Console.WriteLine(i);
DelegatSample delgate2 = sc.Sub;
int j = delgate2(20, 10);
Console.WriteLine(j);
}
}

Output:
Add Result : 30
Sub Result : 10

Multicast Delegates:
Multi cast delegate is used to hold address of multiple methods in single delegate. To hold multiple addresses with delegate we will use overloaded += operator and if you want remove addresses from delegate we need to use overloaded operator -=

Multicast delegates will work only for the methods which have return type only void. If we want to create a multicast delegate with return type we will get the return type of last method in the invocation list

Example:
public delegate void MultiDelegate(int a,int b);

public class Sampleclass
{
public static void Add(int x, int y)
{
Console.WriteLine(“Addition Value: “+(x + y));
}
public static void Sub(int x, int y)
{
Console.WriteLine(“Subtraction Value: ” + (x – y));
}
public static void Mul(int x, int y)
{
Console.WriteLine(“Multiply Value: ” + (x * y));
}
}
class Program
{
static void Main(string[] args)
{
Sampleclass sc=new Sampleclass();
MultiDelegate del = Sampleclass.Add;
del += Sampleclass.Sub;
del += Sampleclass.Mul;
del(10, 5);
Console.ReadLine();
}
}

Output
Addition Value : 15
Subtraction Value : 5
Multiply Value : 50

Delegates have the following properties:

  1. Delegates are like C++ function pointers but are type safe.
  2. Delegates allow methods to be passed as parameters.
  3. Delegates can be used to define callback methods.
  4. Delegates can be chained together; for example, multiple methods can be called on a single event.
  5. C# version 2.0 introduced the concept of Anonymous Methods, which allow code blocks to be passed as parameters in place of a separately defined method. C# Version 3.0 introduced lambda expressions as a more concise way of writing inline code blocks. Both anonymous methods and lambda expressions (in certain contexts) are compiled to delegate types. Together, these features are now known as anonymous functions.

 

Share this:
Share

Asp.Net – what is query string in asp.net with example

Querystring is a kind of variable which can be used to pass a value from one page to another page. hence there are several way to pass value from one page to another page. but we are describing on this article.

Querystring variable pass with variable with “?”

For example: http://www.abc.com/test.html?var1=this is query string variable value.

In the above example “var1” is variable name and “this is query string variable value.” the variable value. we can use this querystring in asp.net as follows.

string var1=Request.QueryString[“var1”];

We can use mutliple querystring variable as well. first variable would use with “?” and other variable will use with “&”

For example: http://www.abc.com/test.html?var1=this is first variable&var2=this is second variable&var3=this is third variable

In the above example “var1”, “var2” and “var3” are querystring variable and we can fetch the values of query string value in asp.net as follows.

string var1=Request.QueryString[“var1”];
string var2=Request.QueryString[“var2”];
string var3=Request.QueryString[“var3”];

Share this:
Share