How to sum TimeSpan in LINQ with C#

If you want to sum TimeSpan properties in LINQ with C#, use:

            List<TimeSpan> list = new List<TimeSpan>             {
                new TimeSpan(1),
                new TimeSpan(2),
                new TimeSpan(3)

            // TimeSpan.Zero is the initial offset, in this case 0 ticks
            // subtotal is used to sum to items in the list
            // t is the current item in the list
            TimeSpan total = list.Aggregate(TimeSpan.Zero, (subtotal, t) => subtotal.Add(t));


            // Result: 6



Thanks to:

Short cut for pasting unformatted text, with the default paste options in Microsoft Office 2010

In most cases when I copy text from a source document or website and want to paste this text in Microsoft Office Outlook or Microsoft Office Word, I want the text to be formatted like the destination document (pasting as unformatted text). Previously I used two methods to accomplish this:

  • Paste > Paste Special > Unformatted Text > OK ( 4 clicks)
  • Notepad ctrl+a then ctrl+v the ctrl+a and then ctrl+c (1 click but many key presses)

Paste special dialog


In Microsoft Office 2010 (and perhaps in older Microsoft Office versions) you can now set default paste options to unformatted text. The pasted text will be formatted like the destination document

In Microsoft Office Word 2010

  • File > Options > Advanced > [Cut, copy and paste] section > Keep Text Only


In Microsoft Office Outlook 2010

  • Open a new mail message
  • File > Options > Mail > Editor Options > Advanced > [Cut, copy and paste] section > Keep Text Only


When you paste text in you’re mail or word document, it will be formatted like the mail or document.


Microsoft Office 2010 OneNote

  • In Microsoft Office 2010 OneNote, you can use the [Contenxt Menu Key] + T, instead of [CTRL + V] to paste unformatted text.
  • Or use alt + h + v + t

Set initial sorting on a Telerik RadGrid column

If you want to set an initial sorting on a column on a Telerik RadGrid, use the “NeedDataSource” event and add a GridSortExpression to the MasterTableView.SortExpressions

        protected void RadGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
            // Set datasource here…
            if (!IsPostBack)
                // Clear the current sortexpressions

                // Create "Date" sorting
                GridSortExpression expression = new GridSortExpression();
                expression.FieldName = "Date";
                expression.SortOrder = GridSortOrder.Descending;

                // Set initial sortexpression to the [Date] column


In the aspx page:

                   <ItemStyle Width="70px" />

Or you could use the “SortExpressions” tag in the MasterTableView tag:

<telerik:RadGrid ID="RadGrid1" runat="server"> <MasterTableView> <SortExpressions> <telerik:GridSortExpression FieldName="Name" SortOrder="Ascending" /> </SortExpressions> <Columns> <telerik:GridBoundColumn AutoPostBackOnFilter="true" CurrentFilterFunction="Contains" DataField="Name" FilterControlWidth="200px" meta:resourcekey="CustomerNameColumn" UniqueName="Name" SortExpression="Name" ShowFilterIcon="true"> <ItemStyle Width="200px" /> </telerik:GridBoundColumn>


How to solve: IE 8 new tab hang on Connecting… on Windows 7 x64

The solution posted on: solved my problem

For Win 7 64 bits :
1 – Create a new notepad document and paste this text :
@echo off
echo IEREREG Version 1.07 for IE8 27.03.2009
echo by Kai Schaetzl
echo installs and registers (if suitable) all DLLs known to be used by IE8.
echo should only take a few seconds, but please be patient
REM ******************************
echo registering IE files
REM IE files (= part of setup)
regsvr32 /s /i browseui.dll
REM regsvr32 /s /i browseui.dll,NI (unnecessary)
regsvr32 /s corpol.dll
regsvr32 /s dxtmsft.dll
regsvr32 /s dxtrans.dll
REM simple HTML Mail API
regsvr32 /s “%ProgramFiles(x86)%\internet explorer\hmmapi.dll”
REM group policy snap-in
regsvr32 /s ieaksie.dll
REM smart screen
regsvr32 /s ieapfltr.dll
REM ieak branding
regsvr32 /s iedkcs32.dll
REM dev tools
regsvr32 /s “%ProgramFiles(x86)%\internet explorer\iedvtool.dll”
regsvr32 /s iepeers.dll
REM Symptom: IE8 closes immediately on launch, missing from IE7
regsvr32 /s “%ProgramFiles(x86)%\internet explorer\ieproxy.dll”
REM no install point anymore
REM regsvr32 /s /i iesetup.dll
REM no reg point anymore
REM regsvr32 /s imgutil.dll
regsvr32 /s /i /n inetcpl.cpl
REM no install point anymore
REM regsvr32 /s /i inseng.dll
regsvr32 /s jscript.dll
REM license manager
regsvr32 /s licmgr10.dll
REM regsvr32 /s msapsspc.dll
REM regsvr32 /s mshta.exe
REM VS debugger
regsvr32 /s msdbg2.dll
REM no install point anymore
REM regsvr32 /s /i mshtml.dll
regsvr32 /s mshtmled.dll
regsvr32 /s msident.dll
REM no reg point anymore
REM regsvr32 /s msrating.dll
REM multimedia timer
regsvr32 /s mstime.dll
REM no install point anymore
REM regsvr32 /s /i occache.dll
REM process debug manager
regsvr32 /s “%ProgramFiles(x86)%\internet explorer\pdm.dll”
REM no reg point anymore
REM regsvr32 /s pngfilt.dll
REM regsvr32 /s /i setupwbv.dll (not there anymore!)
regsvr32 /s tdc.ocx
regsvr32 /s /i urlmon.dll
REM regsvr32 /s /i urlmon.dll,NI,HKLM
regsvr32 /s vbscript.dll
REM VML renderer
regsvr32 /s “%CommonProgramFiles%\microsoft shared\vgx\vgx.dll”
REM no install point anymore
REM regsvr32 /s /i webcheck.dll
regsvr32 /s /i /n wininet.dll
REM ******************************
echo registering system files
REM additional system dlls known to be used by IE
REM added 11.05.2006 Symptom: Add-Ons-Manager menu entry is present but nothing happens
regsvr32 /s extmgr.dll
REM added 12.05.2006 Symptom: Javascript links don’t work (Robin Walker) .NET hub file
regsvr32 /s mscoree.dll
REM added 23.03.2009 Symptom: Find on this page is blank
regsvr32 /s oleacc.dll
REM added 24.03.2009 Symptom: Printing problems, open in new window
regsvr32 /s ole32.dll
REM mscorier.dll
REM mscories.dll
REM Symptom: open in new tab/window not working
regsvr32 /s actxprxy.dll
regsvr32 /s asctrls.ocx
regsvr32 /s cdfview.dll
regsvr32 /s comcat.dll
regsvr32 /s /i /n comctl32.dll
regsvr32 /s cryptdlg.dll
regsvr32 /s /i /n digest.dll
regsvr32 /s dispex.dll
regsvr32 /s hlink.dll
regsvr32 /s mlang.dll
regsvr32 /s mobsync.dll
regsvr32 /s /i msieftp.dll
REM regsvr32 /s msnsspc.dll #no entry point
regsvr32 /s msr2c.dll
regsvr32 /s msxml.dll
regsvr32 /s oleaut32.dll
REM regsvr32 /s plugin.ocx #no entry point
regsvr32 /s proctexe.ocx
REM plus DllRegisterServerEx ExA ExW … ?
regsvr32 /s /i scrobj.dll
REM shdocvw.dll hasn’t been updated for IE7 and IE8, it still registers itself for the Windows Internet Controls
regsvr32 /s /i shdocvw.dll
regsvr32 /s sendmail.dll
REM ******************************
REM PKI/crypto functionality
REM initpki can take very long to run and is rarely a problem
REM if there are problems with crypto, SSL, certificates
REM remove the three following REMs from the lines
REM echo We are almost done except one crypto file
REM echo but this will take very long, be patient!
REM regsvr32 /s /i:A initpki.dll
REM ******************************
REM tabbed browser, do at the end, why originally with /n ?
regsvr32 /s /i ieframe.dll
REM ******************************
echo correcting bugs in the registry
REM do some corrective work
REM Symptom: new tabs page cannot display content because it cannot access the controls (added 27. 3.2009)
REM This is a result of a bug in shdocvw.dll (see above), probably only on Windows XP
reg add “HKCR\TypeLib\{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}\1.1\0\win32” /ve /t REG_SZ /d %systemroot%\system32\ieframe.dll /f
REM ******************************
echo all tasks have been finished
2 – Close all your IE windows and processes.
3 – Save your document on your Desktop by example, with the .bat extension. Right-click on it, and select “Run as administrator”.
4 – Test if this tip resolved your issue by openning IE. If you use a Windows 32 bits version, please replace %ProgramFiles(x86)% by %ProgramFiles% in your .bat file.

How to change the value of a default on a column with TSQL

If you want to change the value of default on a column, you must use the ALTER TABLE syntax (DROP DEFAULT and CREATE DEFAULT should be avoided, will be removed in future releases):

-- Create a default on column 'cust_name' in table 'MyCustomer' with value 'new customer'
IF OBJECT_ID('dbo.MyCustomers') IS NOT NULL
  DROP TABLE dbo.MyCustomers
CREATE TABLE dbo.MyCustomers(
  cust_id int NOT NULL,
  cust_name varchar(30) NOT NULL DEFAULT ('new customer'))

-- Remove the default from column 'MyColumn1' in table 'MyTable'
declare @defaultName varchar(100), @cmd varchar(1000)
set @defaultName =
 select name
 from sys.objects so JOIN sys.sysconstraints sc on so.object_id = sc.constid
 where object_name(so.parent_object_id) = 'MyTable'
 and so.type = 'D'
 and sc.colid = (select column_id from sys.columns where name = 'MyColumn1' and Object_ID = Object_ID(N'MyTable'))
set @cmd = 'alter table MyTable drop constraint ' + @defaultName

-- Add a default on column 'cust_name' in table 'MyCustomer' with value 'new text customer'
alter table MyCustomers add constraint DF_MyCustomers_cust_name default 'new text customer' for cust_name;

Configuring Internet Explorer to Remember Your SharePoint Login Credentials


Go to Control Panel
Go to Internet Options
Click the “Security” tab
Click “Local Intranet”
Click “Sites”
Click “Advanced”
Add your SharePoint site into the “Local Intranet” zone.
Click OK to confirm on all Internet Options screens.
Open up Internet Explorer and go to your SharePoint site.  When prompted for username/password make sure you enter your credentials and check “Remember My Password”.
Your username/password should now be passed to the SharePoint site automatically.

Get first day of current week or last week with TSQL

To get the first day of the current week use:

select dateadd(dd, (datepart(dw, getdate()) * -1) + 2, getdate())

To get the first day of previous week and last day of previous week use:

select dateadd(day, -8, dateadd(dd, (datepart(dw, getdate()) * -1) + 2, getdate()))
select dateadd(day, -1, dateadd(dd, (datepart(dw, getdate()) * -1) + 2, getdate()))

You can also, use a variable for the current date and time, like:

declare @currentDateTime as datetime
set @currentDateTime = '2011-01-15 07:47:31.887' 

select dateadd(day, -8, dateadd(dd, (datepart(dw, @currentDateTime) * -1) + 2, @currentDateTime))
-- Result: 2011-01-02 07:47:31.887

select dateadd(day, -1, dateadd(dd, (datepart(dw, @currentDateTime) * -1) + 2, @currentDateTime))
-- Result: 2011-01-09 07:47:31.887

Leadtools engine initialization error, or there is no appropriate license

If you get the error:

Leadtools.Forms.Ocr.OcrException : Engine initialization error, or there is no appropriate license.
at Leadtools.Forms.Ocr.Plus.OcrEngine.StartupEngine(String startupParameters)
at Leadtools.Forms.Ocr.Plus.OcrEngine.Startup(RasterCodecs rasterCodecs, String workDirectory, String startupParameters)

make sure the ocr engine is started with the correct path to the installation of the Leadtools.

 // Start OCR-engine var ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Plus, false);
            if (!ocrEngine.IsStarted)
                ocrEngine.Startup(null, null, @“C:\InstallationFolderLeadTools”);


The LeadTools OCR Engine should be installed in  C:\InstallationFolderLeadTools.


Adding LeadTools installation folder to the PATH variable

The error can some times be caused by the folder C:\InstallationFolderLeadTools not being in the PATH variable.

Add the [C:\InstallationFolderLeadTools] to the PATH variable.

Control Panel > System > Advanced System Settings






This error can also be caused by an IIS or IIS express application pool not running in 32-bit application mode.



Change IIS Application Pool setting [Enable 32-bit Applications] to [True]



IIS Express

Add enable32BitAppOnWin64="true" in [C:\Users\User1\Documents\IISExpress\config\applicationhost.config].


            <add name="Clr4IntegratedAppPool" managedRuntimeVersion="v4.0" managedPipelineMode="Integrated" CLRConfigFile="%IIS_USER_HOME%\config\aspnet.config" autoStart="true" enable32BitAppOnWin64="true">

                <processModel loadUserProfile="true" />


            <add name="Clr4ClassicAppPool" managedRuntimeVersion="v4.0" managedPipelineMode="Classic" CLRConfigFile="%IIS_USER_HOME%\config\aspnet.config" autoStart="true" enable32BitAppOnWin64="true"/>

            <add name="Clr2IntegratedAppPool" managedRuntimeVersion="v2.0" managedPipelineMode="Integrated" CLRConfigFile="%IIS_USER_HOME%\config\aspnet.config" autoStart="true" enable32BitAppOnWin64="true"/>

            <add name="Clr2ClassicAppPool" managedRuntimeVersion="v2.0" managedPipelineMode="Classic" CLRConfigFile="%IIS_USER_HOME%\config\aspnet.config" autoStart="true" enable32BitAppOnWin64="true"/>

            <add name="UnmanagedClassicAppPool" managedRuntimeVersion="" managedPipelineMode="Classic" autoStart="true" enable32BitAppOnWin64="true"/>

            <applicationPoolDefaults managedRuntimeLoader="v4.0">

                <processModel />



You should use System.Security.SecurityElement.Escape in C# to escape special characters in XML and not System.Web.HttpUtility.HtmlEncode

You should use the function System.Security.SecurityElement.Escape and not the function System.Web.HttpUtility.HtmlEncode to escape special characters in XML if you don’t want the characters like éûÉ to be converted to &#233;&#251;&#201; which is permit able but not necessary en can result in long xml fields.

var escapedXml = System.Security.SecurityElement.Escape(@"&<>'""’éûÉغ");
// Result=&amp;&lt;&gt;&apos;&quot;’éûÉغ

escapedXml = System.Web.HttpUtility.HtmlEncode(@"&<>'""’éûÉغ");
// Result=&amp;&lt;&gt;'&quot;’&#233;&#251;&#201;غ

To revert the escape process, use:

SecurityElement securityElement = System.Security.SecurityElement.FromString("<test>H&amp;M</test>");
string unescapedText = securityElement.Text;
Console.WriteLine(unescapedText); // Result: H&M


// Data is the un-escaped text that should be inserted in a XML tag.
string data = "H&amp;M";

// A xml tag with the name "test" is used, just for creating the SecurityElement.
var securityElement = new SecurityElement("test", data);

// Generate the un-escaped text.
string unescapedText = securityElement.Text;

// Result: H&M

Or use the InnerText property of a XmlNode:

// Data is the un-escaped text that should be inserted in a XML tag.
string data = "H&amp;M";
XmlDocument document = new XmlDocument();
document.LoadXml(string.Format("<test>{0}</test>", data));
XmlNode node = document.DocumentElement.FirstChild;

// Generate the un-escaped text.
string unescapedText = node.InnerText;

// Result: H&M