T-SQL tip: log error details in catch

If you want to log detailed information on an error in a catch block, you can use the following code:

 

BEGIN CATCH
        declare @Message varchar(2048) = 
        '<< Your custom error text here. >>' + 
        'Number: ' + cast(isnull(error_number(), '') as varchar(50)) + ' ' +
        'Severity: ' + cast(isnull(error_severity(), '') as varchar(50)) + ' ' +
        'State: ' + cast(isnull(error_state(), '') as varchar(100)) + ' ' +
        'Procedure: ' + cast(isnull(error_procedure(), '') as varchar(1024)) + ' ' +
        'Line: ' + cast(isnull(error_line(), '') as varchar(10)) + ' ' +
        'Message: ' + cast(isnull(error_message(), '') as varchar(1024)) + ' '

        ROLLBACK TRANSACTION    

        RAISERROR (@Message, 16, 1)
        

 

END CATCH

How to convert a string to a stream, without using encoding in C#

If you want to convert a string to a stream, without using encoding. You can use the following string extension:

 

usage

namespace PTB.Cs.Test.Research
{
    using PTB.Cs.Extensions;
    using System;
    using System.IO;
    using Xunit;

    public class RliTester
    {
        
        [Fact]
        public void Test()
        {
            string subject = "Some text.";
            long expected = 10;
            long actual = 0;

            using (Stream stream = subject.ToStream())
            {
                actual = stream.Length;
            }    
            Assert.Equal(expected, actual);
        }
    }
}

Code

namespace Extensions
{
    using System.IO;

    public static class StringExtensions
    {
        /// <summary>
        /// Convert a string to a stream, without using encoding.
        /// Don't forget to dispose the "result" stream in the calling method.
        /// </summary>
        /// <param name="subject">The string to convert.</param>
        /// <returns>A stream with position set to 0.</returns>
        public static Stream ToStream(this string subject)
        {
            var result = new MemoryStream();

            using(var stream = new MemoryStream())
            using (var writer = new StreamWriter(stream))
            {
                writer.Write(subject);
                writer.Flush();
                stream.Position = 0;
                stream.CopyTo(result);
                result.Position = 0;
            }
            
            return result;
        }
    }
}

Keyboard shortcuts for entering comments and check in to TFS in Visual Studio 2013

When searching for keyboard shortcuts in Microsoft Visual Studio 2013 I came across the keyboard shortcut for bringing up the pending changes window:

 

image

 

 

Press CTRL + 0, P

Now the first time it will bring up the pending changes window, but the comments textbox will not be focused.

Put your cursor in the textbox, write some comments.

 

image

 

 

Press ALT + I

This will check in your changes.

 

Now when you make a change to a file and press CTRL + 0, P the comment textbox will be focused and you can directly start typing your comments.

How to change Application scope Settings (that are read-only) at runtime in C#

If you set your settings to the "Application" scope the settings will be read-only.

If you want to change these settings at runtime, you can use the following code from my unit test project:

 

First create a Settings class in your unit test project:

Right click on the unit test project and choose "Properties".

Click on "Settings":

Click on "This project does not contain a default settings file. Click here to create one."

 

image

 

At two test settings: "MyStringSetting" and "MyInSetting"":

 

image

 

image

 

The App.config will be changed like:

image

 

Now to show the code that changes the settings at runtime:

 

namespace UnitTestProject2
{
    using System;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using UnitTestProject2.Properties;

    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            var settings = new Settings();
            
            Assert.AreEqual(settings.MyStringSetting, "This is some random text.");
            settings.ChangeSetting("MyStringSetting", "This is some other text.");
            Assert.AreEqual(settings.MyStringSetting, "This is some other text.");

            Assert.AreEqual(settings.MyIntSetting, 999);
            settings.ChangeSetting("MyIntSetting", 100);
            Assert.AreEqual(settings.MyIntSetting, 100);
        }
    }
    
}

namespace UnitTestProject2.Properties
{
    internal sealed partial class Settings
    {
        public void ChangeSetting<T>(string key, T value)
        {
            this[key] = value;
        }
    }
}

Variable sized column with ellipsis in a table

If you want a column of a table in HTML to expand and you want the text to show "ellipsis" for overflowing text, you can use the solution found at:

 

http://stackoverflow.com/questions/17345158/variable-sized-column-with-ellipsis-in-a-table

 

Just give each cell for the specific column the following class:

.expand-column
        {
            max-width: 1px;
            overflow: hidden;
            -ms-text-overflow: ellipsis;
            -o-text-overflow: ellipsis;
            text-overflow: ellipsis;
            white-space: nowrap;
        }

How to set / overwrite the Context.User.IsInRole for debugging/testing purposes in C#

In my case I wanted to debug a ASP .NET MVC application, that used Windows authentication, but my account was not added to the "Administrators" role in development. If you still want to debug the site as an Administrator you can set the role of the authenticated user in the Global.asax.

protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
    if (Context.User != null)
    {
        var userPrincipal = new GenericPrincipal(Context.User.Identity, new string[] { "Adminstrators" });
        Context.User = userPrincipal;
    } 
}

Solving: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

I was getting the error: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding, when the CloseActivity action was called on a BPT Action.

 

Cause

This was caused by a query in the OnClose action of the BPT action. This query was taking more then 30 seconds resulting in a SQL Server query timeout.

 

Solution

After tweaking the query the error was resolved.

 

4f28f0f1-8d43-4e0b-bc22-3fd624b7f387

Time of Log:

2013-11-11 09:49:09

eSpace:

Test

Tenant:

Users

User:

(7)

Session Id:

ldvw1leqzfsw55ze4zxopmz5

Server:

SERVER001

Module:

Message:

Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

Stack:

at OutSystems.HubEdition.RuntimePlatform.Processes.ProcessBase.ThrowSpecificException(String failureMessage)
at OutSystems.HubEdition.RuntimePlatform.GenericExtendedActions.#Reb(HeContext heContext, Int32 activityId, Boolean advanceProcess, Boolean skip, Boolean skipAll, Action`4 activityChecks, List`1 inputs)
at OutSystems.HubEdition.RuntimePlatform.GenericExtendedActions.ActivityClose(HeContext heContext, Int32 activityId, String requiredssKey, String actionName, List`1 inputs, Int32& nextHumanActivityId)
at OutSystems.HubEdition.RuntimePlatform.GenericExtendedActions.ActivityClose(HeContext heContext, Int32 activityId, Int32& nextHumanActivityId)
at ssHRCallRegistration.Actions.ActionCloseActivityCustomAction(HeContext heContext, Int32 inParamActivityId)
at ssHRCallRegistration.Flows.FlowMainFlow.WBlkEnlistmentCreateDocuments.CommandDone(HeContext heContext)
-Env-
eSpaceVer: 725 (Id=2492, PubId=2461, CompiledWith=8.0.0.8)
RequestUrl: http://12.0.0.1/Test/Activity_View.aspx (Method: POST)
AppDomain: /LM/W3SVC/1/ROOT/Test-55-130286322452188885
FilePath: C:\…\PS\running\Test.0862112041\Activity_View.aspx
Locale: nl-NL
DateFormat: yyyy-MM-dd
PID: 1692 (‘w3wp’, Started=’11/8/2013 7:33:37 AM’, Priv=517Mb, Virt=5661Mb)
TID: 66
Thread Name:
.NET: 2.0.50727.6407

Fontawesome is awesome!

 

Fontawesome can be found at: http://fontawesome.io

Icons based on a font, gives you infinite scale, icon color adjustment with CSS etc.:

 

image

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Font awesome</title>
    <link href="css/font-awesome.css" rel="stylesheet" />
</head>
<body>
    <h3>Small icons</h3>
    <i class="fa fa-check" style="color:black"></i>
    <i class="fa fa-check" style="color:orange"></i>
    <i class="fa fa-check" style="color:green"></i>
    <h3>Large icons</h3>
    <i class="fa fa-5x fa-check" style="color:black"></i>
    <i class="fa fa-5x fa-check" style="color:orange"></i>
    <i class="fa fa-5x fa-check" style="color:green"></i>
    <p></p>
    <h3>Rotated icon</h3>
    <i class="fa fa-5x fa-info-circle fa-rotate-180" style="color:blue"></i>
</body>
</html>