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;
}