Archive for the ‘Code and Development’ Category

Simple testing of transactions in sqlserver management studio

February 3rd, 2012

Testing that transactions do as they should is probably easiest done right in Sqlserver management studio.  The trick is to fire up two of them.

You can set breakpoints as with your usual Visual studio and use the same shortcuts for stepping through.

 

Transactions are complex, do not think otherwise.  Similarly to multi threaded programming it requires knowledge beforehand and just not the usual intellisense-and-see-which-methods-pops-up we have learned to use.

Playing around with transactions before implementing them is crucial for understanding and correct behaviour.

Just understanding that the transaction does what it should is probably not enough for any solution that has more than a trickle of traffic.  Resources I have used a lot are Inside Sqlserver and Sqlserver Books online (BOL). The latter is the same as the help.

System.Messaging.MessageQueueException was unhandled : A workgroup installation computer does not support the operation.

January 29th, 2012

If you play with MSMQ and get an exception like

System.Messaging.MessageQueueException was unhandled
Message=A workgroup installation computer does not support the operation.
Source=System.Messaging
ErrorCode=-2147467259
StackTrace:
at System.Messaging.MessageQueue.ResolveFormatNameFromQueuePath(String queuePath, Boolean throwException)
at System.Messaging.MessageQueue.get_FormatName()
at System.Messaging.MessageQueue.ReceiveCurrent(TimeSpan timeout, Int32 action, CursorHandle cursor, MessagePropertyFilter filter, MessageQueueTransaction internalTransaction, MessageQueueTransactionType transactionType)
at System.Messaging.MessageQueue.Receive()

 

It might be due to a reason like mine.

With

 

var queues = MessageQueue.GetPrivateQueuesByMachine(“.”);
var queue = queues.Where(q => q.QueueName == “private$\\mynewqueue”).Single();
var message = queue.Receive();

it does work
but with

 

var queue = new MessageQueue(“private$\\mynewqueue”);
var message = queue.Receive();

it doesn’t.

Change to

 

var queue = new MessageQueue(“.\\private$\\mynewqueue”);
var message = queue.Receive();

and you might be good to go again.

I haven’t bothered to figure out exactly why but my workaround might help you.

Tilt your extra monitor to portrait

January 27th, 2012

I, of course, have two monitors for my development computer.  (sometimes three but that is subject for another post)  The extra monitor I have is tilted to portrait so it is higher than wide.  I strongly recommend it.

We write code with the flow going from the top to the bottom.  Having a high monitor makes me see more code and keep less in the RAM part of the brain.

One could argue that methods should be short and there hence is no need for a screen that is so high that two methods are visible at once.  One could also argue that cars should be fragile so as to feel the need for avoiding collisions. Or calculators to calculate wrongly so we learn to do it manually instead.

Try tilting your extra monitor to portrait today!  You might not like it – hacking queries is better done wide, spreadsheets is often wide, presentations are wide – but then you have your other landscape monitor for that.

You might have to rearrange some tools in your favourite development environment but that is easy done.

I dare you to try it and leave a comment.

 

TortoiseSVN and code.google

January 27th, 2012

The quick and dirty documentation found for instance here hints at one should write the project name and user name together with the https source path.  So is not the case if one uses TortoiseSVN.

So the string

https://compulsorycat.googlecode.com/svn/trunk/ compulsorycat --username myusername@gmail.com

should really be

https://compulsorycat.googlecode.com/svn/trunk/

Not that hard to figure out really.  But it took me a while.  I even missed it at this helping side.

Just for information.  Nothing fancy.

Convert dotnet DateTime to Sqlserver SqlDateTime

December 9th, 2011

The SqlDateTime has a narrower span than the Dotnet ditto; dotnet starts from year 0 but sqlserver from 1753.  This means that uninitialised DateTime fields in dotnet will give runtime execution errors when trying to persist them in a sqlserver database.

I write together a small and simple method for making sure the DateTime interval is inside the valid SqlDateTime interval.

public static class DateTimeExtensions
    {
        /// <summary>This method returns the datetime fitting into min/max of Sqlserver
        /// as per http://codebetter.com/petervanooijen/2008/08/21/valid-date-time-values-in-sql-server-sqldatetime-vs-datetime/
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static DateTime ToValidSqlserverDatetime(this DateTime dt)
        {
            //  Must cast System.Data.SqlTypes.SqlDateTime.MinValue: http://codebetter.com/petervanooijen/2008/08/21/valid-date-time-values-in-sql-server-sqldatetime-vs-datetime/
            if (dt < (DateTime)System.Data.SqlTypes.SqlDateTime.MinValue)
            {
                return (DateTime)System.Data.SqlTypes.SqlDateTime.MinValue;
            }
            else if (dt > (DateTime)System.Data.SqlTypes.SqlDateTime.MaxValue)
            {
                return (DateTime)System.Data.SqlTypes.SqlDateTime.MaxValue;
            }
            else
            {
                return dt;
            }
        }
    }

The code is easier to read at pastebin.

Honour those who should.

 

 

 

 

 

 

Scope_Identity doesn’t return an Int in sqlserver but a Decimal

December 5th, 2011

This is an old item.  Scope_Identity in Sqlserver doesn’t return an Int but a Decimal.  So in Dapper the call is

                var id = conn.Query<Decimal>(...

Honour those who should and here.

VB.Net and lambda

November 30th, 2011

Using the terse lambda syntax used in C# also in VB.Net is also (almost) possible.

Here is a short example:

var nameList =
 myFacade.GetData().
 where( x => x.ID = 42 ).
 select( x => x.Name ).
 ToList();

which in VBNet becomes:

Dim nameList =
 myFacade.GetData().
 Where( Function(x) x.ID = 42 ).
 Select( Function(x) x.Name ).
 ToList()

Note that the periods have to be at the end of the line, otherwise the multi line capability of Vbnet doesn’t work.  This can be remedied by the old trailing underscore of VB.

If you need to set the type of x it is also doable:

Dim nameList =
 myFacade.GetData().
 Where( Function(x As MyClass) x.ID = 42 ).
 Select( Function(x As MyClass) x.Name ).
 ToList()

Unit tests, TDD, BDD

November 28th, 2011

Maybe we shouldn’t think of unit tests, TDD or BDD but more of go no go tests. Whatever you automatically test it has a Go/NoGo, Yes/No, Success/Fail result.

C#: string.ToUpper() and char.ToUpper()

November 16th, 2011

In C# there is a

"mystring".ToUpper()

but nothing in the char class.  Instead use

char.ToUpper('x')

… or write an extension method.

Singleton in dotnet

November 15th, 2011

Singleton is often considered the simplest pattern but I digress. Except for the simplest cases writing a singleton requires thorough knowledge about the language at hand; one really has to know the locks and ifs and constructors to make a zyzygy.

Below is linked a good article. I don’t consider it as wordy since it really explains the subject in depth in a simple way.

There are 6 examples where 4 can be used. At the bottom of the article is a short explanation if you don’t want to grok it.

- http://csharpindepth.com/Articles/General/Singleton.aspx