January 2009 - Posts

Building a BabyCam
27 January 09 10:46 AM | Scott Mitchell | with no comments

My wife and I are in the midst of 'sleep training' our daughter, trying our best to get her accustomed to taking adequate naps during the day. This usually involves going through the daytime nap routine, moving her to her crib, and then leaving the room. This is usually immediately followed by crying for a period of time, which we pick up on our baby monitor, but then there is silence, blessed silence! But because our baby monitor is audio only, we are left wondering - is she really asleep or is she just lying there, staring off into space? And if, 45 minutes later, we hear some noises, is that Alice rousing from her nap or is she just shifting around in her sleep? Do we go check up on her and risk waking her from a light slumber? Oh, to be a new parent.

If capitalism is to be any guide, it is clear that we are not the only parents who would benefit from a video monitor. There are several video baby monitor models on the market, and there are countless wireless surveillance cameras available, from low-grade wireless web cams to highend, night-vision security cameras that would not look out of place affixed to the exterior of a bank. But what fun is buying something when you can spend half a day jerry-rigging your own solution? If you have a spare computer, a wireless network, and a web cam you can build your own BabyCam!

The BabyCam my wife and I built is composed of the following hardware:

  • A spare laptop that was bought circa 2001.
  • A Sony PCMCIA wireless network card that had been previously used with this laptop, as this laptop is so dated that it does not have integrated WiFi support.
  • An old Logitech WebCam 4000 that I picked up a few years back and had sitting in the closet.

To get the BabyCam going, simply hook up the web cam to the computer and position them in the baby's room as desired. To broadcast the images captures by the web cam to computers in other rooms (or, potentially, any computer on the Internet), you'll need some software. I use Microsoft Windows Media Encoder 9, which is free and can be downloaded from http://www.microsoft.com/windows/windowsmedia/download/AllDownloads.aspx. Once you have the software installed it's pretty easy to get it going - you set it up to broadcast a live recording, specify the video and audio source, and choose to have the broadcast 'pull from the encoder' (rather than setting up a Media Server).

That's all there is to it! Once you've got the broadcast going, you can view it from any other computer in your home network by opening Windows Media Player, going to File --> Open URL and entering the appropriate URL, which, by default, will be http://machineName:8080, although you can configure what port is used for the broadcast. And if you are interested in sharing BabyCam with the world, you will need either a static IP address or a service like DynDNS and then configure your router to properly forward incoming requests on a specific port to the BabyCam computer.

There's a great step-by-step guide on setting up the Windows Media Encoder software along with tips on how to open the BabyCam to Internet-wide access at: Setting Up a Live Webcam Feed.

Filed under:
February's Toolbox Column Now Online
24 January 09 09:01 AM | Scott Mitchell | with no comments

My Toolbox column in the February 2009 issue of MSDN Magazine is available online. The February issue examines two frameworks for implementing design patterns:

  • Castle ActiveRecord - the active record pattern is a common design pattern employed by data-driven applications for modeling a database table or view in terms of a class. In a nutshell, classes with both instance and static members are used to model tables, where the static methods operate on the entire table and instance members are used to work with a specific record. The Castle ActiveRecord project is an open-source library for using this pattern in .NET applications.
  • PostSharp - aspect-oriented programming (AOP) is a programming model designed to help with those scenarios where functionality extends across multiple modules. AOP lets the developer build aspects, which are modules that handle some concern, such as logging or security. The aspect can then declaratively (or programmatically) be applied to other modules in the application with instructions to execute at some action point in the lifetime of the application, such as before a particular method executes, in the face of an unhandled exception, and so forth. PostSharp in a free, open-source library for implementing AOP techniques in the .NET Framework. (Note: Earlier this month the creator of PostSharp, Gael Fraiteur, announced a new commercial license for the product, although there will continue to be an open-source and free community edition.)

There are no Blogs of Note or book reviews in this issue.

Enjoy! - http://msdn.microsoft.com/en-us/magazine/dd419658.aspx

As always, if you have any suggestions for products, blogs, or books to review for the Toolbox column, please send them to toolsmm@microsoft.com.

Filed under:
Redirecting with the Click of a Button
22 January 09 01:06 PM | Scott Mitchell | with no comments

Every ASP.NET developer, at some point or another, has created a page with a Button that, when clicked, redirects the user to some other page. This is typically implemented by adding a Button control to the page, creating a Click event handler, and adding a Response.Redirect, perhaps passing along some user input through the querystring. There are two shortcomings with this approach:

  1. It involves an extra round trip to the server for the Button postback, which just generates a redirect message.
  2. If there is an associated TextBox where the user enters some value and that is transmitted via the querystring, you need to worry about what happens if the user presses the Enter key while focused in this TextBox. There's a postback, most likely, but what Button is considered clicked? This issue becomes more apparent if you have several TextBoxes on a page, each with a Button, that, when clicked, takes the user to some other page passing along the related TextBox's value through the querystring. See Enter and the Button Click Event for a thorough discussion on this topic...

The good news is that a jot of JavaScript can help surmount both of these shortcomings. I've created a simple custom server control that packages this JavaScript and behavior into a single, easy to use control, which I call RedirectButton. You can learn more about this control, as well as download the complete source code, at RedirectButton - Redirect Users With the Click of a Buton.

Happy Programming!

Check out more of my free ASP.NET server controls at My Code Projects page!

Filed under:
SQL Tip: Displaying a Padded Number
18 January 09 08:10 AM | Scott Mitchell | with no comments

A common requirement for reports is to left or right pad a number so that it conforms to a certain format or so that all the numbers in a column are aligned. Padding a string in VB or C# code is a cinch thanks to the String class's PadLeft and PadRight methods. But what if you need to apply the padding directly in your SQL SELECT query?

Microsoft T-SQL includes an STR function that takes as input a numeric value and returns a string. You can optionally provide a length, which indicates how many characters the resulting string contains, with the numeric value right-aligned. For example, the statement:

PRINT '|' + STR(1, 3) + '|'

Outputs:

| 1|

In other words, the STR function displays the numeric value - 1 - as a string of three characters. Because 1 only requires one character, the first two characters are spaces. If needed, you can use SQL's REPLACE function to replace the spaces with some alternate character. For example, the following left pads the passed in number with 0s.

PRINT REPLACE(STR(1, 3), ' ', '0') -- Outputs 001

In general, you can left pad a number using the following pattern:

REPLACE(STR(numberToPrint, totalLength), ' ', 'characterToLeftPadWith')

And now a challenge for you, my dear reader: Can you come up with a general pattern for a T-SQL statement for right padding a number? The inputs would be numberToPrint, totalLength, and characterToRightPadWith.

Filed under:
HTTP Redirection Tip
05 January 09 08:26 AM | Scott Mitchell | with no comments

I'm writing a series of concise montly web development tips over at DotNetSlackers.com. My first tip, published in December, looked at caching data for the lifespan of a request, which is a quick and easy way to improve the performance of data-driven web applications that have pages where the same data is requested multiple times per request. My most recent tip takes a deeper look at HTTP redirection.

Every ASP.NET developer is familiar with Response.Redirect, but do you know what happens behind the scenes when you do a Response.Redirect? In a nutshell, it sends an HTTP 302 status back to the client with the redirection URL. The 302 status code is used to indicate that information has temporarily moved; there is another HTTP status code (301) that indicates that a resource has permanently moved. You should use a 301 redirect when using URL rewriting, “retiring” old URLs, or doing a website structure re-org.

For more information check out the tip, An In-Depth Look at HTTP Redirection.

Filed under:
More Posts

Archives

My Books

  • Teach Yourself ASP.NET 4 in 24 Hours
  • Teach Yourself ASP.NET 3.5 in 24 Hours
  • Teach Yourself ASP.NET 2.0 in 24 Hours
  • ASP.NET Data Web Controls Kick Start
  • ASP.NET: Tips, Tutorials, and Code
  • Designing Active Server Pages
  • Teach Yourself Active Server Pages 3.0 in 21 Days

I am a Microsoft MVP for ASP.NET.

I am an ASPInsider.