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 Remove duplicates from a IEnumerable using C# or VB.NET

To Remove duplicates from a IEnumerable using C# or VB.NET you can use the snippet below.

Sample .NET 3.5 and newer

Sample C#

using System;
using System.Collections.Generic;
using System.Linq;

namespace de.Fesslersoft.DistinctIenumerbable
{
    class Program
    {
        static void Main(string[] args)
        {
            var myList = new List<string>
            {
                "1A", "1A", "1B", "1C", "1C", "1D", "1D"
            };

            myList = myList.Distinct().ToList();

            foreach (var item in myList)
            {
                Console.WriteLine(item);
            }
            Console.Read();
        }
    }
}

Sample VB.NET (autogenerated)

Imports System.Collections.Generic
Imports System.Linq

Namespace de.Fesslersoft.DistinctIenumerbable
	Class Program
		Private Shared Sub Main(args As String())
			Dim myList = New List(Of String)() From { _
				"1A", _
				"1A", _
				"1B", _
				"1C", _
				"1C", _
				"1D", _
				"1D" _
			}

			myList = myList.Distinct().ToList()

			For Each item As var In myList
				Console.WriteLine(item)
			Next
			Console.Read()
		End Sub
	End Class
End Namespace

Sample .NET 2.0

Sample C#

using System;
using System.Collections.Generic;

namespace de.Fesslersoft.DistinctIenumerbable
{
    class Program
    {
        static void Main(string[] args)
        {
            List<String> myList = new List<string>();
            myList.Add("1A");
            myList.Add("1A");
            myList.Add("1B");
            myList.Add("1C");
            myList.Add("1C");
            myList.Add("1D");
            myList.Add("1D");

            var newList = new List<String>();
            foreach (var item in DistinctIenumerable(myList))
            {
                newList.Add(item);
            }

            foreach (var item in newList)
            {
                Console.WriteLine(item);
            }
            Console.Read();
        }

        internal static IEnumerable<T> DistinctIenumerable<T>(IEnumerable<T> input)
        {
            var passedValues = new Dictionary<T, bool>();
            foreach (T item in input)
            {
                if (!passedValues.ContainsKey(item))
                {
                    passedValues.Add(item, false);
                    yield return item;
                }
            }
        }
    }
}

Sample VB.NET (autogenerated)

Imports System.Collections.Generic

Namespace de.Fesslersoft.DistinctIenumerbable
	Class Program
		Private Shared Sub Main(args As String())
			Dim myList As List(Of [String]) = New List(Of String)()
			myList.Add("1A")
			myList.Add("1A")
			myList.Add("1B")
			myList.Add("1C")
			myList.Add("1C")
			myList.Add("1D")
			myList.Add("1D")

			Dim newList = New List(Of [String])()
			For Each item As var In DistinctIenumerable(myList)
				newList.Add(item)
			Next

			For Each item As var In newList
				Console.WriteLine(item)
			Next
			Console.Read()
		End Sub

		Friend Shared Function DistinctIenumerable(Of T)(input As IEnumerable(Of T)) As IEnumerable(Of T)
			Dim passedValues = New Dictionary(Of T, Boolean)()
			For Each item As T In input
				If Not passedValues.ContainsKey(item) Then
					passedValues.Add(item, False)
					yield Return item
				End If
			Next
		End Function
	End Class
End Namespace

Please rate this snippet

How to select a item on rightclick using devexpress ListboxControl

To select a item on rightclick using devexpress ListboxControl you need to use the MouseDown Event.
See the samples below.

Sample C#

private void listBoxControl1_MouseDown(object sender, MouseEventArgs e)
{
	listBoxControl1.SelectedIndex = listBoxControl1.IndexFromPoint(new Point(e.X, e.Y));
}

Sample VB.NET

Private Sub listBoxControl1_MouseDown(sender As Object, e As MouseEventArgs)
	listBoxControl1.SelectedIndex = listBoxControl1.IndexFromPoint(New Point(e.X, e.Y))
End Sub

Please rate this snippet

How to get all Description Attributes of an Enum using C# or VB.NET

To get all Description Attributes of an Enum using C# or VB.NET you can use the snippet below.

Sample C# – Class

public class DescriptionAttributes<T>
    {
        protected List<DescriptionAttribute> Attributes = new List<DescriptionAttribute>();
        public List<string> Descriptions { get; set; }

        public DescriptionAttributes()
        {
            RetrieveAttributes();
            Descriptions = Attributes.Select(x => x.Description).ToList();
        }

        private void RetrieveAttributes()
        {
            foreach (var attribute in typeof(T).GetMembers().SelectMany(member => member.GetCustomAttributes(typeof(DescriptionAttribute), true).Cast<DescriptionAttribute>()))
            {
                Attributes.Add(attribute);
            }
        }
    }

Usage C#

class Program
    {
        public enum TestEnum
        {
            [Description("An apple a day keeps the doctor away")]
            Apple=0,
            [Description("Yummie Strawberry")]
            Strawberry=1,
            [Description("Banana for scale")]
            Banana=2
        }

        static void Main(string[] args)
        {
            var descriptions = new DescriptionAttributes<TestEnum>().Descriptions.ToList();
            foreach (var description in descriptions)
            {
                Console.WriteLine(description);
            }
            Console.Read();
        }
    }

Sample VB.NET – Class

Public Class DescriptionAttributes(Of T)
	Protected Attributes As New List(Of DescriptionAttribute)()
	Public Property Descriptions() As List(Of String)
		Get
			Return m_Descriptions
		End Get
		Set
			m_Descriptions = Value
		End Set
	End Property
	Private m_Descriptions As List(Of String)

	Public Sub New()
		RetrieveAttributes()
		Descriptions = Attributes.[Select](Function(x) x.Description).ToList()
	End Sub

	Private Sub RetrieveAttributes()
		For Each attribute As var In GetType(T).GetMembers().SelectMany(Function(member) member.GetCustomAttributes(GetType(DescriptionAttribute), True).Cast(Of DescriptionAttribute)())
			Attributes.Add(attribute)
		Next
	End Sub
End Class

Usage VB.NET

Class Program
	Public Enum TestEnum
		<Description("An apple a day keeps the doctor away")> _
		Apple = 0
		<Description("Yummie Strawberry")> _
		Strawberry = 1
		<Description("Banana for scale")> _
		Banana = 2
	End Enum

	Private Shared Sub Main(args As String())
		Dim descriptions = New DescriptionAttributes(Of TestEnum)().Descriptions.ToList()
		For Each description As var In descriptions
			Console.WriteLine(description)
		Next
		Console.Read()
	End Sub
End Class

Result / Output

Enum Descriptions

Enum Descriptions

Please rate this snippet

How to manually populate a multicolumn devexpress TreeList using C# and VB.NET

To manually populate a multicolumn devexpress TreeList using C# and VB.NET you can use the snippet below.

Sample C#

using System;
using System.Collections.Generic;
using DevExpress.XtraEditors;

namespace de.Fesslersoft.MultiColumnTreeList
{
    public partial class Form1 : XtraForm
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            var products = new List<ProductInfo>
            {
                new ProductInfo() {name = "Item1", desc = "Description1", price = Decimal.Zero}, 
                new ProductInfo() {name = "Item2", desc = "Description2", price = 3.0m}, 
                new ProductInfo() {name = "Item3", desc = "Description3", price = 5.45m}, 
                new ProductInfo() {name = "Item4", desc = "Description4", price = 12.32m}
            };


            foreach (var product in products)
            {
                var node = treeList1.AppendNode(null, null);
                node.SetValue(0, product.name);
                node.SetValue(1, product.desc);
                node.SetValue(2, product.price);
            }
        }
    }
    public class ProductInfo
    {
        public string name { get; set; }
        public string desc { get; set; }
        public decimal price { get; set; }
    }
}

Sample VB.NET

Imports System.Collections.Generic
Imports DevExpress.XtraEditors

Namespace de.Fesslersoft.MultiColumnTreeList
	Public Partial Class Form1
		Inherits XtraForm
		Public Sub New()
			InitializeComponent()
		End Sub

		Private Sub Form1_Load(sender As Object, e As EventArgs)
			Dim products = New List(Of ProductInfo)() From { _
				New ProductInfo() With { _
					Key .name = "Item1", _
					Key .desc = "Description1", _
					Key .price = [Decimal].Zero _
				}, _
				New ProductInfo() With { _
					Key .name = "Item2", _
					Key .desc = "Description2", _
					Key .price = 3.0D _
				}, _
				New ProductInfo() With { _
					Key .name = "Item3", _
					Key .desc = "Description3", _
					Key .price = 5.45D _
				}, _
				New ProductInfo() With { _
					Key .name = "Item4", _
					Key .desc = "Description4", _
					Key .price = 12.32D _
				} _
			}


			For Each product As var In products
				Dim node = treeList1.AppendNode(Nothing, Nothing)
				node.SetValue(0, product.name)
				node.SetValue(1, product.desc)
				node.SetValue(2, product.price)
			Next
		End Sub
	End Class
	Public Class ProductInfo
		Public Property name() As String
			Get
				Return m_name
			End Get
			Set
				m_name = Value
			End Set
		End Property
		Private m_name As String
		Public Property desc() As String
			Get
				Return m_desc
			End Get
			Set
				m_desc = Value
			End Set
		End Property
		Private m_desc As String
		Public Property price() As Decimal
			Get
				Return m_price
			End Get
			Set
				m_price = Value
			End Set
		End Property
		Private m_price As Decimal
	End Class
End Namespace

Please rate this snippet

How to search stored procedures using MS-SQL

To search stored procedures using MS-SQL you can use the snippet below.

Sample MSSQL

SELECT Name FROM sys.procedures WHERE name LIKE '%StoredProcName%'

Please rate this snippet

How to use the HTML5 datalist tag

HTML5 Datalist Element

Description

HTML5 introduced the new <datalist> tag, which specifies a list of predefined <input> element options. It is used to provide the autocomplete feature on <input> elements. Users will see a drop-down list of pre-defined options as they input data. This element has no other attributes than the global attributes, common to all elements. The id of the datalist element must match with the list attribute of the input box.Keep in mind that Safari doesn’t support datalist yet. If you are using this element with an unsupported (older) browser version, the most browsers will simply ignores the datalist tag and render rest of the webpage. Internet Explorer 9 and below is a little bit special, it will simply print the option values to the page (a possible workaround can be found at http://viralpatel.net/).

Usage

Here you can see a Basic example on how to use the datalist tag in HTML5.

Live Sample

Keep in mind that this Live-Example only works on supported Browsers.



JSFiddle’s

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

Related links:

Compatibility:
working Chrome
working Internet Explorer
not tested Firefox
not working Opera
not working Safari

Please rate this snippet

How to deep copy a IList in C# and VB.NET

To deep copy a IList in C# and VB.NET you can use the snippet.

Sample C#

public static IList<T> Clone<T>(IList<T> listToClone) where T : ICloneable
{
	return listToClone.Select(item => (T)item.Clone()).ToList();
}

Sample VB.NET

Public Shared Function Clone(Of T As ICloneable)(listToClone As IList(Of T)) As IList(Of T)
	Return listToClone.[Select](Function(item) DirectCast(item.Clone(), T)).ToList()
End Function

Please rate this snippet

How to make a multicolumn listbox in C# and VB.NET

To make a multicolumn listbox in C# and VB.NET you can use the following snippet.
First you need to add the Columns to the Listview.

multicolumnslistviewcsharp_add

Adding columns to the Listview

Sample C#

listView1.Items.Add("Column1Text").SubItems.AddRange(new string[] { "col1;row1", "col2;row1", "col3;row1" });
listView1.Items.Add("Column2Text").SubItems.AddRange(new string[] { "col1;row2", "col2;row2", "col3;row2" });
listView1.Items.Add("Column3Text").SubItems.AddRange(new string[] { "col1;row3", "col2;row3", "col3;row3" });

Sample VB.NET

listView1.Items.Add("Column1Text").SubItems.AddRange(New String() {"col1;row1", "col2;row1", "col3;row1"})
listView1.Items.Add("Column2Text").SubItems.AddRange(New String() {"col1;row2", "col2;row2", "col3;row2"})
listView1.Items.Add("Column3Text").SubItems.AddRange(New String() {"col1;row3", "col2;row3", "col3;row3"})
multicolumnslistviewcsharp

Result: Listview with Multiple Columns

Please rate this snippet

How to check if an array contains a value in Javascript

To check if an array contains a value in Javascript you can use the snippet below.

Sample Javascript

Array.prototype.inArray = function (inputValue) {
	var counter;
	for (counter=0; counter < this.length; counter++) {
		if (this[counter] === inputValue) {
			return true;
		}
	}
	return false;
};

Please rate this snippet