Free Tool: CheckBoxList(For)

ASP.NET MVC's HtmlHelp has a TextBoxFor, a DropDownListFor, and even a HiddenFor method…but it doesn't have a CheckBoxListFor method. CheckBoxList(For) by Mikhail Tsennykh fills that gap and makes it easy to generate a list of checkboxes for an array of objects to let users can select which objects they want.

More

Posted by Peter Vogel on 06/21/20130 comments


Visual Studio Tip: Write a Property with Just a Name and a DataType

Sometimes auto-implemented properties won't do the job, and you need a full property declaration; for instance, if you want to include a call to NotifyPropertyChanged in your property's setter or if you want a read-only property in Visual Basic. However, just because you need a full property, you don't have to type all the required code in.

More

Posted by Peter Vogel on 06/11/20130 comments


A Best Practice for Authenticating Users in ASP.NET MVC 4

If your site has even one or two actions where access is restricted to particular users, the smart thing to do is to restrict access to all the actions on your site and then selectively permit access to those actions that all users are allowed to request. That way, an error of omission (forgetting to make a method available) simply prevents users from accessing some action.

More

Posted by Peter Vogel on 06/05/20130 comments


A For ... Each Extension for LINQ

A lot of the time, when I write a LINQ statement, I follow it with a For ... Each loop that processes each item in the query:

Dim db As New NorthwindModel
Dim ords = From o In db.Orders
           Where o.OrderDate > Date.Now
           Select o

For Each Ord As Order In ords
  '…do something with each selected order
Next
More

Posted by Peter Vogel on 05/31/20130 comments


Free Tool: Minify Text Files on the Fly with Chirpy

The faster you download your Web application's various text files (.CSS, .js), the faster your page appears to your users. One of the best things you can do is minify those files: remove whitespaces, tabs, meaningful names, and everything else that makes code easier for human beings to read. If you've upgraded to ASP.NET 4.5, you can use its bundling features. But if you haven't upgraded, minifying your files is a pain -- unless you have Chirpy.

More

Posted by Peter Vogel on 05/29/20130 comments


Configure Code Analysis in Visual Studio

I like well-written code, and I especially like it when I've written it. But I don't always live up to my own expectations. So I've used various tools (FxCop, CodeRush, ReSharper) to give me some objective feedback on the quality of my code. Starting with Visual Studio 2010, Microsoft built the latest version of their tools into Visual Studio: Visual Studio 2012 Ultimate (and up) and Visual Studio 2012 Professional (and up). From Visual Studio's Analyze menu, you can select Run Code Analysis on individual projects or on your whole solution.

More

Posted by Peter Vogel on 05/28/20130 comments


Use Enumerated Values with Bit Flags to Handle Multiple Options

If your code uses a set of options and you don't use enumerated values, you can end up writing opaque code like this which passes an unexplained "magic number" to a method:

cust.Update(1)

With enumerated values, your code becomes easier to read because you can pass a named value:

More

Posted by Peter Vogel on 05/07/20130 comments


Free Yourself with a Dependency Injection Container

I'm now doing a column for MSDN magazine about how design patterns help developers solve typical business problems. The column is called "Patterns in Practice", but it could just as easily have been called "Thinking with Objects." As I was writing this month's Patterns in Practice column, I was thinking about how much easier my life has become since I've started using Inversion of Control (IoC)/Dependency Injection Containers. But when, as a consultant, I work with other developers, I find that these tools aren't all that common.

More

Posted by Peter Vogel on 04/30/20130 comments


Don't Create Pages in SharePoint; Use User Controls

Because the Master Page for your SharePoint site is kept in the hive (and because Visual Studio isn't smart enough to follow the MasterPageFile attribute that points to the Master Page in the hive) when creating a page in Visual Studio, you can't switch to Design View. For those of you with a great grasp of HTML that may not be a problem. Because of my shaky grasp of HTML and CSS, when I work in Source View I'm frequently surprised at what shows up in the browser when I view my page.

More

Posted by Peter Vogel on 04/22/20130 comments


Free Tool: Try Out SQL Queries and Explore Databases with QueryExpress

As an independent consultant, I move from one client's computer to another. Because of that, I frequently find myself on computers where I need to browse a database but don't have a copy of SQL Server Management Studio installed. If you want a quick replacement for Management Studio, try QueryExpress. It's so small and downloads so quickly that you'll think it hasn't download at all. QueryExpress also requires no installation: just drop it on your desktop and double-click its icon to run it.

More

Posted by Peter Vogel on 04/15/20130 comments


Customize Visual Studio Menus

There are probably lots of items on the Visual Studio menus and toolbars that you never use: Why not get rid of them? The odds are also good that there are items on the Visual Studio menus that you use frequently but are buried on some submenu. For instance, I want to know why the Views menu is cluttered up with windows I never use like Team Explorer (most of my clients don't use Team Foundation Server) and Properties (I always use F4). And why is the Immediate Window (which I use all the time) buried on the Other Windows menu off the Debug menu?

More

Posted by Peter Vogel on 04/05/20130 comments


Measure Performance with the Stopwatch Class

I like it when my code runs faster, but I don't want to work at it (if my application is running too slow, my time is usually better spent enhancing my data access rather than tweaking code). But if I do rewrite any code to make it run faster, I want to know that I made a difference. This has led me, after all these years, to the System.Diagnostics.Stopwatch class.

To use the Stopwatch class, just call its StartNew method, run your test, then call its Stop method. You can extract the elapsed time from the Stopwatch's ElapsedMilliseconds property then display it or write it to a log file. This example puts the result in a TextBox:

More

Posted by Peter Vogel on 03/21/20130 comments


Subscribe on YouTube