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 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 get the selected row index in Devexpress Xtragrid Control using C# and VB.NET

To get the selected row index in Devexpress Xtragrid Control using C# and VB.NET you can use the snippet below.

Samples

Sample C#

Sample VB.NET

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 get current Domain of the PC in C# and VB.NET

To get current Domain of the PC in C# and VB.NET you can use the snippet below.

Sample C#

using System;
using System.DirectoryServices.ActiveDirectory;

namespace de.Fesslersoft.DomainTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var myDomain = Domain.GetComputerDomain();
            foreach (DomainController dc in myDomain.DomainControllers)
            {
                Console.WriteLine("{0} - {1}", dc.IPAddress, dc.Name);
            }
            Console.Read();
        }
    }
}

Sample VB.NET

Imports System.DirectoryServices.ActiveDirectory

Namespace de.Fesslersoft.DomainTest
	Class Program
		Private Shared Sub Main(args As String())
			Dim myDomain = Domain.GetComputerDomain()
			For Each dc As DomainController In myDomain.DomainControllers
				Console.WriteLine("{0} - {1}", dc.IPAddress, dc.Name)
			Next
			Console.Read()
		End Sub
	End Class
End Namespace

Please rate this snippet

How to get all types that implement a specific Interface in C# and VB.NET

To retrieve a IEnuemerable of all types in the current or another Assembly that implement a specific Interface / Abstract class and so on, you can use the snippets below.

Methods

C# Version

VB.NET Version

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

Do you have an alternate or better method for this task?
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 get the size of all tables in MSSQL

To get the size of all tables using MSSQL you can use the snippet below.

Sample MSSQL

SELECT 
    tab.NAME AS [Tablename],
    s.Name AS [Schema Name],
    part.rows AS [Count of Rows],
    SUM(a.total_pages) * 8 AS [Total Space in KB], 
    SUM(a.used_pages) * 8 AS [Used Space in KB], 
    (SUM(a.total_pages) - SUM(a.used_pages)) * 8  AS [Unused Space in KB]
FROM sys.tables tab
INNER JOIN sys.indexes idx ON tab.OBJECT_ID = idx.object_id
INNER JOIN sys.partitions part ON idx.object_id = part.OBJECT_ID AND idx.index_id = part.index_id
LEFT OUTER JOIN sys.schemas s ON tab.schema_id = s.schema_id
INNER JOIN sys.allocation_units a ON part.partition_id = a.container_id
WHERE tab.NAME NOT LIKE 'dt%' AND tab.is_ms_shipped = 0 AND idx.OBJECT_ID > 255 
GROUP BY tab.Name, s.Name, part.Rows
ORDER BY 4 DESC

Please rate this snippet

How to use a select in update statement using MSSQL

To use a select statement in a update statement using MSSQL you can use the snippet below.

Sample SQL

UPDATE TableOne
SET
    t1.ColumnOne = t2.ColumnOne,
    t1.ColumnTwo = t2.ColumnTwo
FROM
    TableOne AS t1
INNER JOIN
    TableTwo AS t2
ON
    TableOne.id = TableTwo.id

Please rate this snippet

How to get the Month name of a Datetime in C# and VB.NET

To get the Month name of a Datetime in C# and VB.NET you can use the following snippet.

Sample C#

Console.WriteLine(new DateTime(2010, 1, 1).ToString("MMM", CultureInfo.InvariantCulture)); //Jan
Console.WriteLine(new DateTime(2010, 1, 1).ToString("MMMM", CultureInfo.InvariantCulture)); //January

Sample VB.NET

Console.WriteLine(New DateTime(2010, 1, 1).ToString("MMM", CultureInfo.InvariantCulture))
'Jan
Console.WriteLine(New DateTime(2010, 1, 1).ToString("MMMM", CultureInfo.InvariantCulture))
'January

Please rate this snippet

How to query a DataTable using LINQ in C# and VB.NET

To query a DataTable using LINQ in C# and VB.NET you can use the snippet below.
In order to use the snippet, you need to reference System.Data.DataSetExtensions.

Sample C#

var resultData = (from myDataRow in myDataTable.AsEnumerable() where myDataRow.Field<Int32>("Fieldname") == 1 select myRow);

Sample VB.NET

Dim resultData = (From myDataRow In myDataTable.AsEnumerable() Where myDataRow.Field(Of Int32)("Fieldname") = 1myRow)

Please rate this snippet