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:

Please rate this snippet

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:

Please rate this snippet

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:

Please rate this snippet

How to check if a number is a power of 2 in C# and VB.NET

To check if a number is a power of 2 in C# and VB.NET you can use the following extension method.

Sample C#

public static bool IsPowerOfTwo(this ulong input)
{
    return (input != 0) && ((input & (input - 1)) == 0);
}

Sample VB.NET

<System.Runtime.CompilerServices.Extension> _
Public Shared Function IsPowerOfTwo(input As ULong) As Boolean
	Return (input <> 0) AndAlso ((input And (input - 1)) = 0)
End Function

As always, the extension method was added to out Fesslersoft.Extensions library @Github

Please rate this snippet

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

Please rate this snippet

How to check if number is a prime in Java

To check if number is a prime in Java you can use the following snippet.

Sample Java

public static boolean IsPrime(long input) {
	boolean primeCheck = true;
	for (long count = 3; count <= Math.sqrt(input); count += 2)
		if (input % count == 0) {
			primeCheck = false;
			break;
		}
	if (( input%2 !=0 && primeCheck && input > 2) || input == 2) {
		return true;
	} else {
		return false;
	}
}

Please rate this snippet