Machine Setup: Turn-off clicking sound when browsing folders
In the past I usually have had the sound turned off on my computer but recently I have been leaving it on. With the sound on, browsing folders in Explorer or navigating in Internet Explorer would play a clicking sound whenever changing folders or navigating to a new page. That gets annoying very quickly so here is how I shut it off on Windows Server 2008 (and probably Windows Vista too):
Start > Control Panel > Sound
In the Sound Control Panel, select the Sounds tab and then in the Program panel scroll all the way down to the “Windows Explorer” program (it will be near the bottom).
Under the Windows Explorer program is a “Start Navigation” sound:
Select this one (you can use the Test Button to confirm that you have the correct sound) and then change the Sounds dropdown near the bottom to “(None)”.
Hit Apply and OK your way ok of the Sound Control Panel and now no more clicking sound while browsing folders in Explorer or while navigating in Internet Explorer.
This is getting added to my Machine Setup checklist for when I rebuild my development environment.
How to quickly comment or uncomment lines of code in Visual Studio
When I am developing I am always trying to reduce the amount of time that my hands come off of the keyboard and have to move to the mouse. One of the frequent functions that I noticed required me to move my hand off of the keyboard to the mouse is commenting or uncommenting lines of code.
Visual Studio has two buttons in the toolbar to comment or uncomment a line of code (they are part of the Text Editor or HTML Source Editing Toolbars):
You can also achieve the same result by using the key combination Ctrl-K, Ctrl-C to comment out the currently selected lines or Ctrl-K, Ctrl-U to uncomment the currently selected lines.
Ctrl-K, Ctrl-C means to hold down the Control key and then press K and then while still holding down the Control key press C. Ctrl-K, Ctrl-U means to hold down the Control key and then press K and then while still holding down the Control key press U.
I have found this key combination to be difficult at first to remember (in the past I tended to always just instinctively go for my mouse) but now I have remembered to use that key combination regularly and have found it very helpful.
How to send email when port 25 is blocked
I run Windows Server 2008 as my development environment and use the local SMTP service to send emails for any website or application that I am developing and that needs to send email. Recently I was working on a Billing Application that needed to send emails and I wanted to send several to myself for testing, but for some reason the emails were not being sent.
I followed this article from Scott Forsyth about how to debug smtp issues. After connecting using Telnet and manually sending a message, I saw the email message sitting there in my queue (C:\inetpub\mailroot\Queue) and not going out.
Next I checked the event log and found this message (I was sending to a gmail account for testing):
Message delivery to the host 'XXX.XX.XXX.XX' failed while delivering to the remote domain 'gmail.com' for the following reason: The remote server did not respond to a connection attempt.
This indicated that my local smtp server was not able to connect to gmail’s mail server. I use Verizon Residential DSL as my Internet Service Provider (ISP). Some time in 2009, Verizon Residential DSL began blocking port 25. This caused my local development machine SMTP server to not be able to send email.
An easy workaround that I found was to send using my Verizon DSL email account. To do this you need to specify port 587 and also your credentials for your Verizon account:
System.Net.Mail.SmtpClient smtpClient = null;
smtpClient = new System.Net.Mail.SmtpClient("outgoing.verizon.net", 587);
smtpClient.Credentials = new System.Net.NetworkCredential("firstname.lastname@example.org", "password");
Verizon lets you create multiple email accounts so it was easy to create a secondary account just for me to send through instead of using my primary account.
Machine Setup: How to enable Line Numbers in Visual Studio and SQL Server Management Studio
I like to have line numbers visible when working in both Visual Studio and SQL Server Management Studio. By default, line numbers are turned off in both environments. One of the first things I do when setting up a new development machine is to open Visual Studio and SQL Server Management Studio, enable line numbers, and then close both of them so that the setting is saved for the future.
In Microsoft Visual Studio, to enable line numbers open the Options dialog by going to Tools > Options…
Then expand the Text Editor node and then the All Languages node. Now you will see a Line Number checkbox under the Display section on the right-hand side. Check this box to display line numbers.
It is a nearly identical procedure to enable line numbers in Microsoft SQL Server Management Studio:
Tools > Options… > Text Editor > All Languages > Line Numbers checkbox
How to quickly format a C# class file – Ctrl-K, Ctrl-D
In C# as you are typing in a class file the indenting of your code can easily get out of alignment making the code hard to read. Where does your class, if block, or method begin and end??
You can manually go and fix each line but there is a much simpler way to correct the indenting. You can use Ctrl-K, Ctrl-D to format the entire document. Now your code will be reformatted to the correct indenting for your page.
Couple things to note:
- If there are errors in the structure of your code (missing closing brackets, missing semi-colon, etc.) then this will not work. This will still work with other errors such as undeclared variables.
- If you are working on legacy code (for example fixing a bug) and there is a different indenting structure, it is good practice to stick with the legacy indenting and not modify the indenting of the entire file just because you fixed a bug in one line of code. You should try to minimize your code change to only what was necessary to fix the bug (don’t reformat the entire file). The next person that does a diff on your check-in with the previous check-in will thank you.
- I also leave the Visual Studio indentation settings alone and stick with the defaults. If you have a different indentation style than a code file that was previously checked, this process could also cause the subsequent diff to show lots of red.
- You can use Ctrl-K, Ctrl-F on smaller blocks of text, so if you have a strange indenting style then highlighting only your change can correct the indentation on the selected lines and not the entire file.
- These can be found under the Edit > Advanced Menu:
- Cutting and Pasting is also another way to get the content formatted quickly:
Use Ctrl+. (period) to trigger Visual Studio’s automatic add using statement context menu
When you type class names that are missing the correct namespace reference, Visual Studio will help you out by giving you a context menu that you can use to automatically add the correct using statement to the top of your class.
For instance, in the example below, the Utility class exists in a different namespace that is not currently referenced in the class. Visual Studio gives you the red squiggly to show there is an error but there also is a small little line at the end of the class name:
You can take your mouse and hover just over that line and you will get a little drop-down context menu:
Which is a very nice feature where Visual Studio will automatically add the using statement to the top of your file (or insert the full reference):
The feature to add the using statement to your class is great in itself but getting your mouse into just the right spot to click that little line is pretty tricky (and also can break your development train of thought).
As the tool tip says, you can hit Shift+Alt+F10 to get the context menu but Ctrl+. (period) will also pop up that menu and it is a much easier key combination to use.
All of Kramer’s entrances on Seinfeld
Every single Kramer entrance:
Thanks to the blog The Big Picture by Barry Ritholtz for pointing me there.
Timestamp string with milliseconds from a System.DateTime
You can easily convert a System.DateTime object into a string based on the current date and time by using the ToString() method of the DateTime object. For instance:
DateTime d = DateTime.Now;
String s = d.ToString("yyyyMMdd-HHmmss");
Will produce a string with the value of “20091018-232013” assuming the date/time was 10/18/2009 11:20:13PM at the time the code was executed.
But did you know you can get the milliseconds of that System.DateTime object by using the “fff” format string:
DateTime d = DateTime.Now;
String s = d.ToString("yyyyMMdd-HHmmss.fff");
Will produce a string with the value of “20091018-232013.456” assuming the date/time was 10/18/2009 11:20:13.456PM at the time the code was executed.
I use this all the time when I need to append a timestamp to a log, a log filename, or just anything else that needs a quick way to turn a System.DateTime object into a string with milliseconds too.
Parse a non-standard date string into a System.DateTime object
The other day I had a date that was in the form of yyyyMMdd and I needed to parse that string into a System.DateTime object. Just trying to parse the string into a System.DateTime object would not work:
String dateString = "20091016";
DateTime d = DateTime.Parse(dateString);
The above code results in an exception:
FormatException: String was not recognized as a valid DateTime.
I was guaranteed to have the date string in the form of yyyyMMdd so my initial thought was to use Substring to break it into the individual year, month, and day parts and create a new System.DateTime object from those pieces.
But then I discovered that you can use the DateTime.ParseExact method and a System.Globalization.DateTimeFormatInfo object to specify the pattern for the date that is being parsed.
Here is how I was able to parse a non-standard date string into a System.DateTime.
di = new System.Globalization.DateTimeFormatInfo();
di.FullDateTimePattern = "yyyyMMdd";
String dateString = "20091016";
DateTime d = DateTime.ParseExact(dateString, "F", di);
By the way, this is also a great way to parse a credit card expiration date that is in the form of MMyy to a System.DateTime. Just use a pattern of MMyy for the FullDateTimePattern property.
osql.exe and unicode files – how to save your sql scripts with encoding
osql.exe is a great application for running sql scripts in a batch. I use a batch file to execute multiple sql scripts that I use to rebuild my current application database from scratch. When developing a brand new application, deploying a database in this way makes it really easy to recreate the database just like it will be created on Day 1 when you build out the Production environment. This requires scripting out all of your sql objects and then also having a way to execute all of those sql scripts easily. That is where osql.exe comes in handy.
But osql.exe does have one issue that I ran into this week where it does not like UTF-8 (codepage 65001) or UTF-7 (codepage 65000) encoded files. And sometimes you need to include unicode characters in your sql scripts. At first I thought osql just did not support unicode but that is not the case… it just does not like the UTF-8 or UTF-7 encoding.
Trying to run a UTF-8 (codepage 65001) or UTF-7 (codepage 65000) encoded file with osql.exe will give you errors such as:
Incorrect syntax near '+'.
Incorrect syntax near ' '.
Incorrect syntax near 'ï'.
Saving the same file with Unicode Encoding (codepage 1200) will work just fine. Here is how to save sql scripts in Microsoft SQL Server Management Studio with a particular encoding (you can also use this method to see what type of encoding the file is saved in in the first place). One other thing to note is that Visual Studio has this same type of Save As… functionality.
From the Microsoft SQL Server Management Studio (or Visual Studio) File menu choose Save [FILENAME] As…
Then when the Save File As dialog appears you will see a little black arrow (inverted triangle) as part of the Save button.
Clicking the just the inverted triangle portion of the button will give you a menu.
Choosing the Save with Encoding… option will then present you with an Advanced Save Options dialog.
Here is where you can specify the encoding to use for the file. For osql.exe make sure you specify either Western European (Windows) – Codepage 1252 or Unicode – Codepage 1200. Do not select UTF-8 (codepage 65001) or UTF-7 (codepage 65000) or osql.exe will give errors when trying to parse the file.