How to catch specific MS-SQL SQLExceptions in C# and VB.NET

To catch specific SQLExceptions using Microsoft.NET and Microsoft Sequel Server you can use the snippet below. You also need to look for the specific error numbers/codes you want to catch. To do this you can either execute the following query in the SQL Management Studio or you can use this
List of SQLException Numbers/Codes (Direct-DL around 15mb).

Samples

T-SQL Sample

C# Sample

VB.NET Sample

Compatibility: working .NET 2.0 working .NET 3.0 not tested .NET 3.5 not working .NET 4.0 not working .NET 4.5not working .NET 4.6

If you have any questions or suggestions feel free to rate this snippet, post a comment or Contact Us via Email.

Related links:

IsSmallerThan generic extension method for C# and VB.NET

This is the IsSmallerThan generic extension method, this extension method which is part of the Fesslersoft.Extensions. It should only work with numeric values.

Samples

Sample C#

Sample VB.NET


If you have any questions or suggestions feel free to rate this snippet, post a comment or Contact Us via Email.

Related links:

IsLargerThan generic extension method for C# and VB.NET

This is the IsLargerThan generic extension method, this extension method which is part of the Fesslersoft.Extensions. It should only work with numeric values.

Samples

Sample C#

Sample VB.NET

If you have any questions or suggestions feel free to rate this snippet, post a comment or Contact Us via Email.

Related links:

How to round to X decimal places in C# and VB.NET

To round to X decimal places in C# and VB.NET you can use one of the following methods.
Take a look at the output image to see the results.

Sample ConsoleApp in C#

static void Main(string[] args)
{
	decimal input = Decimal.Parse("3,546932");
	decimal inputZero = Decimal.Zero;
	decimal inputShort = Decimal.Parse("3,545");
	decimal inputShorter = 1;

	Console.WriteLine("######################################################################");
	Console.WriteLine("");
	Console.WriteLine("Testinput1:\t{0}", input);
	Console.WriteLine("Testinput2:\t{0}", inputZero);
	Console.WriteLine("Testinput3:\t{0}", inputShort);
	Console.WriteLine("Testinput4:\t{0}", inputShorter);
	Console.WriteLine("");

	Console.WriteLine("########## using decimal.Round AwayFromZero 2 Decimals ##########");
	Console.WriteLine("");
	Console.WriteLine("Output 1:\t{0}",decimal.Round(input, 2, MidpointRounding.AwayFromZero));
	Console.WriteLine("Output 2:\t{0}", decimal.Round(inputShort, 2, MidpointRounding.AwayFromZero));
	Console.WriteLine("Output 3:\t{0}", decimal.Round(inputShorter, 2, MidpointRounding.AwayFromZero));
	Console.WriteLine("Output 4:\t{0}", decimal.Round(inputZero, 2, MidpointRounding.AwayFromZero));
	Console.WriteLine("");

	Console.WriteLine("########## using decimal.Round ToEven 2 Decimals ##########");
	Console.WriteLine("");
	Console.WriteLine("Output 1:\t{0}", decimal.Round(input, 2, MidpointRounding.ToEven));
	Console.WriteLine("Output 2:\t{0}", decimal.Round(inputShort, 2, MidpointRounding.ToEven));
	Console.WriteLine("Output 3:\t{0}", decimal.Round(inputShorter, 2, MidpointRounding.ToEven));
	Console.WriteLine("Output 4:\t{0}", decimal.Round(inputZero, 2, MidpointRounding.ToEven));
	Console.WriteLine("");

	Console.WriteLine("########## using .ToString(\"0.00\") 2 Decimals ##########");
	Console.WriteLine("");
	Console.WriteLine("Output 1:\t{0}", input.ToString("0.00"));
	Console.WriteLine("Output 2:\t{0}", inputShort.ToString("0.00"));
	Console.WriteLine("Output 3:\t{0}", inputShorter.ToString("0.00"));
	Console.WriteLine("Output 4:\t{0}", inputZero.ToString("0.00"));
	Console.WriteLine("");

	Console.WriteLine("########## using .ToString(\"0.00\") 5 Decimals ##########");
	Console.WriteLine("");
	Console.WriteLine("Output 1:\t{0}", input.ToString("0.00000"));
	Console.WriteLine("Output 2:\t{0}", inputShort.ToString("0.00000"));
	Console.WriteLine("Output 3:\t{0}", inputShorter.ToString("0.00000"));
	Console.WriteLine("Output 4:\t{0}", inputZero.ToString("0.00000"));
	Console.WriteLine("");

	Console.Read();
}

Sample ConsoleApp in VB.NET

Private Shared Sub Main(args As String())
	Dim input As Decimal = [Decimal].Parse("3,546932")
	Dim inputZero As Decimal = [Decimal].Zero
	Dim inputShort As Decimal = [Decimal].Parse("3,545")
	Dim inputShorter As Decimal = 1

	Console.WriteLine("######################################################################")
	Console.WriteLine("")
	Console.WriteLine("Testinput1:" & vbTab & "{0}", input)
	Console.WriteLine("Testinput2:" & vbTab & "{0}", inputZero)
	Console.WriteLine("Testinput3:" & vbTab & "{0}", inputShort)
	Console.WriteLine("Testinput4:" & vbTab & "{0}", inputShorter)
	Console.WriteLine("")

	Console.WriteLine("########## using decimal.Round AwayFromZero 2 Decimals ##########")
	Console.WriteLine("")
	Console.WriteLine("Output 1:" & vbTab & "{0}", Decimal.Round(input, 2, MidpointRounding.AwayFromZero))
	Console.WriteLine("Output 2:" & vbTab & "{0}", Decimal.Round(inputShort, 2, MidpointRounding.AwayFromZero))
	Console.WriteLine("Output 3:" & vbTab & "{0}", Decimal.Round(inputShorter, 2, MidpointRounding.AwayFromZero))
	Console.WriteLine("Output 4:" & vbTab & "{0}", Decimal.Round(inputZero, 2, MidpointRounding.AwayFromZero))
	Console.WriteLine("")

	Console.WriteLine("########## using decimal.Round ToEven 2 Decimals ##########")
	Console.WriteLine("")
	Console.WriteLine("Output 1:" & vbTab & "{0}", Decimal.Round(input, 2, MidpointRounding.ToEven))
	Console.WriteLine("Output 2:" & vbTab & "{0}", Decimal.Round(inputShort, 2, MidpointRounding.ToEven))
	Console.WriteLine("Output 3:" & vbTab & "{0}", Decimal.Round(inputShorter, 2, MidpointRounding.ToEven))
	Console.WriteLine("Output 4:" & vbTab & "{0}", Decimal.Round(inputZero, 2, MidpointRounding.ToEven))
	Console.WriteLine("")

	Console.WriteLine("########## using .ToString(""0.00"") 2 Decimals ##########")
	Console.WriteLine("")
	Console.WriteLine("Output 1:" & vbTab & "{0}", input.ToString("0.00"))
	Console.WriteLine("Output 2:" & vbTab & "{0}", inputShort.ToString("0.00"))
	Console.WriteLine("Output 3:" & vbTab & "{0}", inputShorter.ToString("0.00"))
	Console.WriteLine("Output 4:" & vbTab & "{0}", inputZero.ToString("0.00"))
	Console.WriteLine("")

	Console.WriteLine("########## using .ToString(""0.00"") 5 Decimals ##########")
	Console.WriteLine("")
	Console.WriteLine("Output 1:" & vbTab & "{0}", input.ToString("0.00000"))
	Console.WriteLine("Output 2:" & vbTab & "{0}", inputShort.ToString("0.00000"))
	Console.WriteLine("Output 3:" & vbTab & "{0}", inputShorter.ToString("0.00000"))
	Console.WriteLine("Output 4:" & vbTab & "{0}", inputZero.ToString("0.00000"))
	Console.WriteLine("")

	Console.Read()
End Sub

OUTPUT

Decimal Place Formatting

Decimal Place Formatting

PercentOf method for C# and VB.NET

This snippet will give you the PercentOf method for C# and VB.NET.

Sample C#

private decimal CalculatePercentOf(object value1, object value2)
{
	decimal val1;
	decimal val2;
	decimal.TryParse(value1.ToString(), out val1);
	decimal.TryParse(value2.ToString(), out val2);
	decimal result = 0;
	if (val1 > 0 && val2 > 0)
	{
		result = (decimal) val1/val2*100;
	}
	return result;
}

public static decimal PercentOf(sbyte first, sbyte second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(sbyte first, short second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(sbyte first, int second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(sbyte first, long second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(sbyte first, double second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(sbyte first, decimal second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(sbyte first, float second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(short first, sbyte second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(short first, short second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(short first, int second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(short first, long second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(short first, double second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(short first, decimal second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(short first, float second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(int first, sbyte second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(int first, short second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(int first, int second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(int first, long second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(int first, double second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(int first, decimal second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(int first, float second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(long first, sbyte second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(long first, short second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(long first, int second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(long first, long second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(long first, double second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(long first, decimal second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(long first, float second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(double first, sbyte second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(double first, short second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(double first, int second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(double first, long second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(double first, double second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(double first, decimal second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(double first, float second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(decimal first, sbyte second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(decimal first, short second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(decimal first, int second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(decimal first, long second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(decimal first, double second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(decimal first, decimal second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(decimal first, float second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(float first, sbyte second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(float first, short second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(float first, int second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(float first, long second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(float first, double second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(float first, decimal second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(float first, float second)
{
	return CalculatePercentOf(first, second);
}

Sample VB.NET

Private Function CalculatePercentOf(value1 As Object, value2 As Object) As Decimal
	Dim val1 As Decimal
	Dim val2 As Decimal
	Decimal.TryParse(value1.ToString(), val1)
	Decimal.TryParse(value2.ToString(), val2)
	Dim result As Decimal = 0
	If val1 > 0 AndAlso val2 > 0 Then
		result = CDec(val1) / val2 * 100
	End If
	Return result
End Function

Public Shared Function PercentOf(first As SByte, second As SByte) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As SByte, second As Short) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As SByte, second As Integer) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As SByte, second As Long) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As SByte, second As Double) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As SByte, second As Decimal) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As SByte, second As Single) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Short, second As SByte) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Short, second As Short) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Short, second As Integer) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Short, second As Long) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Short, second As Double) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Short, second As Decimal) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Short, second As Single) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Integer, second As SByte) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Integer, second As Short) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Integer, second As Integer) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Integer, second As Long) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Integer, second As Double) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Integer, second As Decimal) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Integer, second As Single) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Long, second As SByte) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Long, second As Short) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Long, second As Integer) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Long, second As Long) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Long, second As Double) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Long, second As Decimal) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Long, second As Single) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Double, second As SByte) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Double, second As Short) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Double, second As Integer) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Double, second As Long) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Double, second As Double) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Double, second As Decimal) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Double, second As Single) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Decimal, second As SByte) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Decimal, second As Short) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Decimal, second As Integer) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Decimal, second As Long) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Decimal, second As Double) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Decimal, second As Decimal) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Decimal, second As Single) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Single, second As SByte) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Single, second As Short) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Single, second As Integer) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Single, second As Long) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Single, second As Double) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Single, second As Decimal) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Single, second As Single) As Decimal
	Return CalculatePercentOf(first, second)
End Function

IsLargerThan extension method for C# and VB.NET

If you need a IsLargerThan extension method you can use the follwing snippet.

Sample C#

public static bool IsLargerThan(this byte input, byte numberToCheck)
{
	return input > numberToCheck;
}

public static bool IsLargerThan(this sbyte input, sbyte numberToCheck)
{
	return input > numberToCheck;
}

public static bool IsLargerThan(this short input, short numberToCheck)
{
	return input > numberToCheck;
}
public static bool IsLargerThan(this ushort input, ushort numberToCheck)
{
	return input > numberToCheck;
}
public static bool IsLargerThan(this int input, int numberToCheck)
{
	return input > numberToCheck;
}

public static bool IsLargerThan(this uint input, uint numberToCheck)
{
	return input > numberToCheck;
}

public static bool IsLargerThan(this long input, long numberToCheck)
{
	return input > numberToCheck;
}
public static bool IsLargerThan(this ulong input, ulong numberToCheck)
{
	return input > numberToCheck;
}

public static bool IsLargerThan(this float input, float numberToCheck)
{
	return input > numberToCheck;
}

public static bool IsLargerThan(this double input, double numberToCheck)
{
	return input > numberToCheck;
}

public static bool IsLargerThan(this decimal input, decimal numberToCheck)
{
	return input > numberToCheck;
}

>h2>Sample VB.NET>/h2>

<System.Runtime.CompilerServices.Extension> _
Public Shared Function IsLargerThan(input As Byte, numberToCheck As Byte) As Boolean
	Return input > numberToCheck
End Function

<System.Runtime.CompilerServices.Extension> _
Public Shared Function IsLargerThan(input As SByte, numberToCheck As SByte) As Boolean
	Return input > numberToCheck
End Function

<System.Runtime.CompilerServices.Extension> _
Public Shared Function IsLargerThan(input As Short, numberToCheck As Short) As Boolean
	Return input > numberToCheck
End Function
<System.Runtime.CompilerServices.Extension> _
Public Shared Function IsLargerThan(input As UShort, numberToCheck As UShort) As Boolean
	Return input > numberToCheck
End Function
<System.Runtime.CompilerServices.Extension> _
Public Shared Function IsLargerThan(input As Integer, numberToCheck As Integer) As Boolean
	Return input > numberToCheck
End Function

<System.Runtime.CompilerServices.Extension> _
Public Shared Function IsLargerThan(input As UInteger, numberToCheck As UInteger) As Boolean
	Return input > numberToCheck
End Function

<System.Runtime.CompilerServices.Extension> _
Public Shared Function IsLargerThan(input As Long, numberToCheck As Long) As Boolean
	Return input > numberToCheck
End Function
<System.Runtime.CompilerServices.Extension> _
Public Shared Function IsLargerThan(input As ULong, numberToCheck As ULong) As Boolean
	Return input > numberToCheck
End Function

<System.Runtime.CompilerServices.Extension> _
Public Shared Function IsLargerThan(input As Single, numberToCheck As Single) As Boolean
	Return input > numberToCheck
End Function

<System.Runtime.CompilerServices.Extension> _
Public Shared Function IsLargerThan(input As Double, numberToCheck As Double) As Boolean
	Return input > numberToCheck
End Function

<System.Runtime.CompilerServices.Extension> _
Public Shared Function IsLargerThan(input As Decimal, numberToCheck As Decimal) As Boolean
	Return input > numberToCheck
End Function