Saturday, February 2, 2013

Using Inline “With” Blocks to Augment a VB.NET Default Constructor

Say you have a simple data transfer class that looks like this:

Public Class Employee
    Public Property Name As String
    Public Property HireDate As Date
    Public Property EmployeeID As Integer
End Class

Normally to load up this class, you might call something like this:

Dim employee As New Employee
employee.Name = "Steve"
employee.HireDate = #1/1/2013#
employee.EmployeeID = 12345

In this case there is an implied Public Sub New() that gets called for you when the class is instantiated.  However, you could also call this:

Dim employee As New Employee With {.Name = "Steve", .HireDate = #1/1/2013#, _
                                                       .EmployeeID = 12345}

Or in VB 2010 or higher which supports implied line continuation, this:

Dim employee As New Employee With {
                .Name = "Steve",
                .HireDate = #1/1/2013#,
                .EmployeeID = 12345,
            }

Note: if you happen to be inside a “With” block already, the inner “With” will take precedence over the scope of the outer “With”.  This applies to both inline “With” blocks and normal “With” blocks, though nesting “With” blocks is not recommended for code clarity’s sake.

Saturday, November 24, 2012

IIS Express Quickie Command-line

This is a reminder to my future self for how to quickly start IIS Express from the command-line on 64-bit Windows 7 or 8 assuming you already have IIS Express installed using the defaults.

  1. Open admin command line.
  2. cd\Program Files (x86)\IIS Express
  3. iisexpress /path:c:\YourApplicationRootFolder /port:WhateverPortNumber

Note: You can also specify /clr:v2.0 if you hate fun.  The default is CLR 4 which works with .NET 4 and 4.5.

This was adapted from the full article on www.iis.net: http://www.iis.net/learn/extensions/using-iis-express/running-iis-express-from-the-command-line

Monday, November 19, 2012

Surface: Standard Onscreen Keyboard

If you feel that the default on-screen keyboard on the Surface RT isn't sufficient, you can enable what I would say is the "pro" keyboard via the settings.

  • Drag in from the right hand side to show the charms and click Settings.
  • On the bottom of the settings window, click "Change PC Settings" to show the Windows 8-style control panel.
  • Under General... Touch Keyboard... turn on the option called "Make the standard keyboard layout available".

Now when you're displaying the onscreen keyboard, there will be a fifth option available which is a more "standard" Windows keyboard including up arrow, down arrow, and the option for F1-F12 if you hit the Fn key first.  I still haven't found a way to show Insert/Delete/Home/Page Up/Page Down other than with the physical keyboard.

Monday, October 1, 2012

SQL in the City - New York 2012

I was able to attend the SQL in the City event in New York this past Friday.  I highly recommend that anyone who can spare the time and travel should attend the next SQL in the City day in your area.  I found it to be a very well put-together event with useful content and excellent networking opportunities.

At the event I was able to meet both Steve Jones (who compassionately shook my hand as I walked in dripping wet from the rain during my walk over to 3rd Avenue) and Grant Fritchey (Scary DBA).  It was great to have even a brief conversation with each of them and introduce myself.  I also got to meet Product Manager David Atkinson and Developer David Simner and talk briefly about the awesome SQL Source Control and SQL Compare products.  The presentations I attended, "Database Maintenance Essentials", "Red Gate Tools - the Complete Lifecycle", and "The Whys and Hows of Database Continuous Integration" were all very informative and useful.

The highlight of the day was finding out about Red-Gate's Virtual Restore product.  This tool essentially allows you to mount a .BAK file as a database.  It supports multiple databases mounted from the same .BAK file, and it is not destructive to the .BAK file.  From the description, it works very similarly to VMs that are mounted from a disk file and then store changes in a disk "diff" file.  This tool allows you to save both space and time and sounds very awesome for developers.

I must say that I think Red-Gate has made a critical error with the marketing of this tool.  Developers hear "restore" and think "DBA tool"; in fact I think that's what the Red-Gate marketing team thought too as this application is included in their DBA bundle only.  I would absolutely use this tool as a developer and I might even use it as often as I use things like SQL Compare and the rest of the more developer-oriented tools.  It's a game-changer, and I hope Red-Gate figures out that they're doing a very bad job of marketing this product and could probably be making a lot more money from it by just getting the word out that it's not only a DBA tool.  For most of the other Red-Gate developer tools, a savvy developer could imagine cooking-up some sort of "good enough" workaround to not have to buy it (though certainly not in a shorter time than the reasonable expense for the license), but I would never even attempt creating something like Virtual Restore.  I will absolutely be trying out this software later this week.  Red-Gate - you've got to get the word out, even think about changing the name or something!!!

Thanks very much to everyone at Red-Gate for putting this event together.  I hope to see you again at the next SQL in the City.

Tuesday, August 28, 2012

PowerShell: Redirecting Console Output and Error Output to a Variable and E-mailing it

I recently had the need to script the execution of a command-line utility and thought it would be a good learning experience to try doing it with PowerShell.  The utility would sometimes output to the STDOUT, but would also send its output to STDERR if there was a problem.  This seemed to involve many different problems all at once including:

  • Running an EXE with a space in the path and multiple command-line parameters
  • Capture both STDERR and STDOUT to a variable
  • Escaping special characters/using special characters
  • Doing string concatenation
  • Doing explicit type conversion
  • Sending an email
  • Joining a string array to a single string variable
  • Doing a "FOR" loop (optional but fun).

Anyway, here's the script I came up with that runs a command-line app and takes all the output (including the error stream) and joins it into a string and then sends it in an email to me.  It's a bit ugly because I am a PowerShell novice, but it does work.  The only trouble I've noticed is that sometimes the error lines will be a bit out of sequence from the corresponding stdout lines.  If someone knows how to fix that, please let me know in the comments!!!

 

#Setup the command string - notice the `" to escape the double-quotes inside the string (deals with spaces in path)

# and the old batch file trick of doing 2>&1 to ensure STDERR is piped to STDOUT.

$command = "& `"C:\Users\myprofile\Documents\visual studio 2010\Projects\TestOutput\TestOutput\bin\Debug\testoutput.exe`" someparameter -xyz someotherparameter -abc someotherthing -rfz -a somethinghere 2>&1"
#Execute the command and put the output in an array.

$console_output_array = invoke-expression $command

#loop through the array and print out the results to the command line (optional)

for ($i=0; $i -lt $console_output_array.length; $i++)
{
    [string]$i + "=<" + $console_output_array[$i] + ">"
}

#create a single string by joining together the array

$console_output_string = [string]::join("`r`n",$console_output_array)

#send an email with the results

$emailFrom = "fromemail@example.com"
$emailTo = "toemail@example.com" 
$subject = "Email subject goes here"
$smtp = new-object Net.Mail.SmtpClient("yoursmtpserver.example.com", 25)
$smtp.Send($emailFrom, $emailTo, $subject, $console_output_string)

Tuesday, August 14, 2012

Fix: Crystal Reports Shows Red X Across Entire Page When Report Run Inside Visual Studio

I was googling (with Bing AND Google) like crazy for a solution to this, but didn't find one from any of the open resource sites, so I figured I'd blog about my fix for this to help the next poor dev to come along.

I was getting user feedback that one of the Crystal Reports in an application that I support was intermittently crashing.  When I ran the report in Visual Studio, on certain pages of the report, I observed a red X across the whole screen with a white field.  Obviously something was very wrong here!!!

Red X on Crystal Report 

It boiled down to that there was a null value being used in one of my formula fields, and Crystal seems to raise an exception for nulls when they are used as part of certain types of formulas and not converted manually to an actual value first.  I didn't know which field was causing the issue so I just started a divide and conquer approach.  I suppressed all fields and confirmed that the report worked (select a bunch of fields, Right Click... Format Multiple Objects... Suppress).  Then I started un-suppressing a few objects at a time until the report started failing again.  Now I knew where the problem was (or at least one of the problems).

I then right-clicked one of the formula fields and chose "Find In Formulas" to open the formula workshop.  I drilled down to the formula field (it could be in any of the sections) and found the offending one.  Note that it could be a "normal" formula field or even one of the special formatting formulas (like "if this field's value is true, format as green").

image

I believe the "right way" to fix it is to handle NULLs properly using ISNULL() (which returns a boolean unlike the T-SQL ISNULL() ) or another appropriate method in Crystal for each and every one of your formulas.  This MSDN article describes that.  However, there exists another workaround as well which should be fine for most simple cases.  On the top of each formula when you click into the white field where the code actually lives, you can change this box:

Exceptions For Nulls

To say this:

Default Values for Nulls

If you choose "Default Values For Nulls", Crystal will substitute "" for null strings and an appropriate flavor of 0 for numeric fields.  Here's some documentation that oddly seems to skip booleans.

This dropdown selection is per-formula, so it might take a lot of work to go through and change every formula in the tree view (don't forget to look at the formatting formulas too!!!), but if your report is crashing with the dreaded red X and you don't know why, this may get it working again.

Wednesday, July 18, 2012

Enabling "Open Project Folder" for project types that don't support it

One annoying thing about SSIS (2008) and some of the other project types in Visual Studio is the lack of an "Open Project Directory..." option on the project right-click menu.  Visual Studio's "Tools" menu can provide a work-around this.
 
Go to Tools... External Tools... and add a new entry with the following properties:
Title = &Open Project Folder
Command = explorer.exe
Arguments = "$(ProjectDir)"
Initial Directory = "$(ProjectDir)"

image


Now, when I select my SSIS project, I can choose "Tools... Open Project Folder" (or ALT+T,O,<ENTER>), and the correct folder will open in Windows Explorer.
 
Using macros such as $(ProjectDir), $(SolutionDir), $(Configuration), and others can come in very handy when you find yourself doing the same types of things over and over in Visual Studio.  Here is the documentation for all other built-in VS macros:  http://msdn.microsoft.com/en-us/library/c02as0cs.aspx