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

How to use Doubleclick in Devexpress Gridview in C# and VB.NET

Using doubleclick events in the Devexpress Gridview Control is a simple task.
There are 2 different ways of doing this. The variant you choose, depends on the Gridview’s gridView.OptionsBehavior.Editable property. If this property is set to false you have to use the DoubleClick event. If this property is set to true you have to use the ShownEditor and HiddenEditor event. I will also give an example on how to combine both methods, if you are using both (true and false) on the gridView.OptionsBehavior.Editable.

Samples

OptionsBehavior.Editable set to False

C# Sample

VB.NET Sample

OptionsBehavior.Editable set to True

When inplace editing is enabled, a click on a grid cell activates the inplace editor (rowedit).
All mouse events will be sent to the editor and cannot be handled by the GridView directly. The DoubleClick event handler is no longer being executed.

C# Sample

VB.NET Sample

OptionsBehavior.Editable set to True or False at Runtime

C# Sample

VB.NET Sample

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 load a custom mouse cursor in C# and VB.NET

To load a custom mouse cursor in C# and VB.NET you can use the follwing snippet.

Sample C#

using System.Runtime.InteropServices;

[DllImport("user32.dll", EntryPoint = "LoadCursorFromFileW", CharSet = CharSet.Unicode)]
public static extern IntPtr LoadCursorFromFile(String str);

public void main()
{
	IntPtr cursorHandle = LoadCursorFromFile(file);
	this.Cursor = new Cursor(cursorHandle);
}

Sample VB.NET

Imports System.Runtime.InteropServices

Public Declare Function LoadCursorFromFile Lib "user32.dll" Alias "LoadCursorFromFileW" (ByVal str As String) As IntPtr

Public Sub main()
	Dim cursorHandle As IntPtr = LoadCursorFromFile(file)
	Me.Cursor = New Cursor(cursorHandle)
End Sub

Please rate this snippet

How to hide a console window in C# and VB.NET

To hide a console window in C# and VB.NET you can use the following snippet.

Sample C#

[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

public static void SetConsoleWindowVisibility(bool show, IntPtr hWnd)
{
	try
	{
		ShowWindow(hWnd, !show ? 0 : 1);
	}
	catch (Exception ex)
	{
		//handle the exception your way
	}
}

Example usage

var hWnd = Window.FindWindow(null, Console.Title);
if (hWnd != IntPtr.Zero) 
{
	SetConsoleWindowVisibility(showWindows, hWnd);
}

Sample VB.NET

<DllImport("user32.dll")> _
Public Shared Function FindWindow(lpClassName As String, lpWindowName As String) As IntPtr
End Function

<DllImport("user32.dll")> _
Public Shared Function ShowWindow(hWnd As IntPtr, nCmdShow As Integer) As Boolean
End Function

Public Shared Sub SetConsoleWindowVisibility(show As Boolean, hWnd As IntPtr)
	Try
		ShowWindow(hWnd, If(Not show, 0, 1))
	Catch ex As Exception
        'handle the exception your way
	End Try
End Sub

Example usage

Dim hWnd = Window.FindWindow(Nothing, Console.Title)
If hWnd <> IntPtr.Zero Then
	SetConsoleWindowVisibility(showWindows, hWnd)
End If

Please rate this snippet