December 2006 - Posts

An ASP.NET 2.0 BlackJack Game
22 December 06 02:57 PM | Scott Mitchell

Every now and then I like to give myself a fun little programming challenge. I'll randomly pick some enjoyable task, like programming a game or blogging engine or what not, and impose a time limit, anywhere from an hour to an afternoon. Such little challenges keep the fun in programming and help me mentally recharge. After spending a week of programming boring data entry forms, such toy projects help remind me why I enjoy computer science and programming!

Anyway, a few months ago I decided to see if I couldn't create a simple online Blackjack game that would allow a single person to come to the site, enter their name and starting bank role, and play Blackjack against a dealer. If you're interested in checking out this little toy application, the result of an afternoon of enjoyable coding, you can download the complete source code from here. There's also a light 4Guys article that provides some light details on the code, Creating a Quick and Dirty Online Blackjack Game.

If you decide to download and poke through this application, please do bear in mind that it was purposefully rushed, as I was racing against the clock to complete the project. Consequently, the code may be rather tattered in places, there are likely many spots where it should be refactored, and there are next to no comments. Let's just say that it's not a showcase of 'best practices', unless you're MacGyver and need to program a blackjack game in under four hours or a nuclear missle is going to explode.

See the Blackjack Wikipedia entry for rules and basic strategy.

A Video Presentation of Implementing a Layered Architecture Using Separate Class Library Projects
18 December 06 09:16 PM | Scott Mitchell

My Working with Data in ASP.NET 2.0 tutorial series looks at building a layered architecture and demonstrates working with data against that architecture. The tutorials implement the architecture as classes in the website's App_Code folder. Ideally, these layers would be implemented as separate Class Library Projects in order to aid with development, deployment, reusability, and code maintenance. But we chose to use the App_Code approach to lessen the learning curve and to make the articles more accessible (you cannot create Class Library projects in Visual Web Developer).

Anywho, a common question I've received over the months is, “Just how do I implement the architecture as separate Class Library projects?“ Part of the challenge is that implementing the DAL as a separate Class Library places the connection string settings in the project's Properties settings instead of reading them from the website's Web.config file.

I've been tinkering with the idea of using Camtasia to create demos / instructional material / additional content for the written word. It's been on my to-do list for several months, but today I decided to sink in 30 minutes and try my hand at it. In any event, what follows is my first attempt, and not surprisingly, it is pretty low quality. As such, I'm wondering what settings folks who create professional-looking demos using Camtasia use to make it look nicer. I recorded at about 900x800 pixels and had it converted into an AVI using the highest sound and video quality. Granted, the resulting AVI that Camtasia produced was very crisp. It was in uploading the video to Google Video that ended up causing the blurry output. And my microphone is nothing fancy.

So, in short, the following video is a fine example of getting what you pay for. Hope this helps someone! Any feedback on whether it's worth it to persue learning more about this medium to improve the quality of the presentation? Or should I stick to the written word?

Filed under:
Maintain Scroll Position on Postback in ASP.NET 2.0
14 December 06 09:33 AM | Scott Mitchell

When working with a lengthy web page that spans many vertical “pages,” scrolling down and then doing something that causes a postback results with the posted back page sitting back up at the top rather than auto-scrolling down to the location where the postback was triggered. With ASP.NET 1.x, this problem can be easily overcome with a bit of JavaScript and server-side code. See Maintaining Scroll Position on Postback for more information.

ASP.NET 2.0 makes maintaining scroll position on postback much easier, although it's not a well-document or oft-discussed feature. When I show this little tip at User Group talks, to clients, or to my class, I'd say more than 90% of the people didn't know about this, are impressed, and wonder aloud, “Why isn't this common knowledge?”

Anyway, to have a single page remember scroll position on postback, simply set the MaintainScrollPositionOnPostback attribute in the @Page directive to True. That's it! The page will then automagically inject the needed JavaScript and server-side logic to make this feature a reality.

<%@ Page Language="..." MaintainScrollPositionOnPostback="true" ... %>

Alternatively, you can enable this feature for all pages in the site by setting it in the <pages> element in Web.config.

<pages maintainScrollPositionOnPostBack="true" />

See my article Client-Side Enhancements in ASP.NET 2.0 for more information on this feature, as well as additional client-side features added to ASP.NET 2.0!

Filed under:
January's Toolbox Column Now Online
14 December 06 09:25 AM | Scott Mitchell

My 13th Toolbox column in the January 2007 issue of MSDN Magazine is now avaiable online. The January issue examines three products:

  • CodeSMART - extends the Visual Studio IDE to improve developer proficiency.
  • ZipForge.NET - programmatically create, unzip, repair, and dissect ZIP files.
  • HttpWatch - an IE plugin to inspect HTTP headers, cookies, caching, and POST data.

This month's issue reviewed Microsoft Visual Studio 2005 Unleashed, by Lars Powers and Mike Snell. From the review:

For developers programming against large, high-level frameworks, mastery of the development environment is increasingly essential. Like many .NET developers, I spend hours inside of Visual Studio each and every day. The more I learn about Visual Studio options, shortcuts, windows, toolbars, and features, the more productive I become and the less time I spend fighting with the IDE.

One book I've found particularly helpful in demystifying the Visual Studio environment is Microsoft Visual Studio 2005 Unleashed (Sams, 2006). In this 870-page tome, Lars Powers and Mike Snell meticulously explore the multitude of menus, configuration settings, project types, and features that comprise Visual Studio 2005..

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

Returning the First Day of the Month from a Given Date/Time value in SQL Server
05 December 06 09:19 PM | Scott Mitchell

First off, it's been a long time since my last post, close to a month! Things have been very busy between the new batches of Working with Data in ASP.NET 2.0 tutorials, the monthly Toolbox column, other miscellaneous writings (working on an article on JSON for MSDN Online with my favorite coauthor Atif Aziz), and a couple ongoing consulting jobs. And of course the holiday season is right around the corner! Sadly, this blog falls a little low on the list of priorities. But I digress - enough excuse-making for one entry. Let's get to some technical content!

SCENARIO: Given a date/time value in SQL Server, you need to compute the first of the month in T-SQL. Perhaps you have a stored procedure and are passed in some date/time value and need to store the first of the month in a particular record. How do you do this?

SOME OPTIONS: Sadly, T-SQL doesn't have a DateSerial-like built-in function. DateSerial, in VBScript, allowed you to construct a date object by passing in the year, month, and day as three parameters. If T-SQL did have such a function, we could just do:

DATESERIAL(YEAR(dateValue), MONTH(dateValue), 1)

However, T-SQL does allow for date values to be constructed as strings, so you could append the year, a hyphen, then month, a hypen, and the string “01” to end up with a string like “2006-12-01”. So that is one option.

Another one that feels a bit less dirty is to use the DATEADD function to subtract the current date minus 1. For example, if today is December 12, 2006 December 16, 2006, to get the first of the month take the current date and subtract the day (16) minus 1. That is, subtract 15 days. Voila, you're now at December 1, 2006! This can be accomplished with the following T-SQL statement:

DATEADD(dd, -(DAY(dateValue) - 1), dateValue)

Computing the first of the month from a given date/time value is also useful if you need to compute the last day of the previous month. This can be computed by subtracting one day from the first of the month (simply omit the -1 in the above statement).

For more T-SQL date/time-related recipes check out Date and Time Manipulation in SQL Server 2000 by Manuj Bahl.

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.