How to split a delimited string to multiple rows using MSSQL

To split a delimited string to multiple rows using MSSQL you can use the snippet below.

Sample MSSQL

CREATE FUNCTION dbo.StringSplit
(
    @Input       NVARCHAR(MAX),
    @Delimiter  NVARCHAR(255)
)
RETURNS TABLE
AS
    RETURN (
		SELECT Number = ROW_NUMBER() OVER (ORDER BY Number),
        Item FROM (
					SELECT Number, Item = LTRIM(RTRIM(SUBSTRING(@Input, Number, CHARINDEX(@Delimiter, @Input + @Delimiter, Number) - Number)))
					FROM (SELECT ROW_NUMBER() OVER (ORDER BY ao.[object_id])
					FROM sys.all_objects AS ao CROSS APPLY sys.all_objects) AS n(Number)
					WHERE Number <= CONVERT(INT, LEN(@Input))
					AND SUBSTRING(@Delimiter + @Input, Number, 1) = @Delimiter
				  ) AS item);
GO




How to Display an Image in Devexpress XtraGrid Control using C# and VB.NET

To Display an Image in Devexpress XtraGrid Control using C# and VB.NET see the example below.

Sample C#

#region

using System.Data;
using System.Drawing;
using System.Windows.Forms;
using WindowsFormsApplication77.Properties;
using DevExpress.XtraEditors.Repository;

#endregion

namespace de.fesslersoft.XtraGridImageTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            gridControl1.DataSource = CreateTable(3);
            gridView1.Columns["Image"].ColumnEdit = new RepositoryItemPictureEdit();
        }

        private DataTable CreateTable(int rowCount)
        {
            var image = Resources.Image1;
            var tbl = new DataTable();
            tbl.Columns.Add("ID", typeof (int));
            tbl.Columns.Add("URL", typeof (string));
            tbl.Columns.Add("Image", typeof (Image));
            for (var i = 0; i < rowCount; i++)
            {
                tbl.Rows.Add(new object[] {i, "http://codesnippets.fesslersoft.de", image});
            }
            return tbl;
        }
    }
}

Sample VB.NET

#Region ""

Imports System.Data
Imports System.Drawing
Imports System.Windows.Forms
Imports WindowsFormsApplication77.Properties
Imports DevExpress.XtraEditors.Repository

#End Region

Namespace de.fesslersoft.XtraGridImageTest
	Public Partial Class Form1
		Inherits Form
		Public Sub New()
			InitializeComponent()
			gridControl1.DataSource = CreateTable(3)
			gridView1.Columns("Image").ColumnEdit = New RepositoryItemPictureEdit()
		End Sub

		Private Function CreateTable(rowCount As Integer) As DataTable
			Dim image = Resources.Image1
			Dim tbl = New DataTable()
			tbl.Columns.Add("ID", GetType(Integer))
			tbl.Columns.Add("URL", GetType(String))
			tbl.Columns.Add("Image", GetType(Image))
			For i As var = 0 To rowCount - 1
				tbl.Rows.Add(New Object() {i, "http://codesnippets.fesslersoft.de", image})
			Next
			Return tbl
		End Function
	End Class
End Namespace

Result

XtraGrid ColumnImages

XtraGrid ColumnImages



How to delete .svn files and folders using Windows Batch Script

to delete .svn files and folders using Windows Batch Script you can use the following snippet.

Sample Windows Batch Script

FOR /R "C:\Users\CodeSnippets.Fesslersoft.de\Documents\Visual Studio 2013\Projects\ConsoleApplication" %%X IN (.svn) DO (RD /S /Q "%%X" 2>nul)

Replace C:\Users\CodeSnippets.Fesslersoft.de\Documents\Visual Studio 2013\Projects\ConsoleApplication with your Solution’s Path.




What is the difference between typeof(), GetType() and IS in C# and VB.NET

What is the difference between typeof(), GetType() and is?

  • typeof() takes a type name specified at compile Time.
  • GetType() gets the Type of an Object at runtime
  • is returns true if an instance is in the inheritance tree. Since the is Keyword is casting the object, you should use it with care.
  • here is an example.

    class Employee { } 
    class Developer : Employee { }
    
    void PrintTypes(Employee e) { 
        print(e.GetType() == typeof(Employee)) // false 
        print(e is Employee)                   // true 
        print(e.GetType() == typeof(Developer))    // true
    }
    




    How to add or remove a class on hover using jQuery

    To add or remove a class on hover using jQuery you can use the snippet below.

    Adding a Class

    See this JSFiddle for adding a CSS-Class.

    $('#test1').hover(function(){  $(this).addClass('hoverClass');});
    

    Removing a Class

    See this JSFiddle for removing a CSS-Class.

    $('#test1').hover(function(){  $(this).removeClass('hoverClass');});
    




    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
    




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

    To get all types that implement a specific Interface in C# and VB.NET you can use the snippet below.

    Sample C#

    var types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(x => x.GetTypes()).Where(y => typeof(IDisposable).IsAssignableFrom(y) && !y.IsInterface);
    

    Sample VB.NET

    Dim types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(Function(x) x.GetTypes()).Where(Function(y) GetType(IDisposable).IsAssignableFrom(y) AndAlso Not y.IsInterface)
    




    Generic Type Conversion snippet for C# and VB.NET

    Here you can find a generic Type conversion snippet for C# and VB.NET.

    Sample C#

    public static T GenericConvert<T, X>(X value) where X : IConvertible
    {
    	return (T)Convert.ChangeType(value, typeof(T));
    }
    

    Sample VB.NET

    Public Shared Function GenericConvert(Of T, X As IConvertible)(value As X) As T
    	Return DirectCast(Convert.ChangeType(value, GetType(T)), T)
    End Function
    




    How to open a Website in the default Browser in C# and VB.NET

    To open a Website in the default Browser in C# and VB.NET you can use the snippet below.

    Sample C#

    System.Diagnostics.Process.Start("http://codesnippets.fesslersoft.de/");
    

    Sample VB

    System.Diagnostics.Process.Start("http://codesnippets.fesslersoft.de/")