Wednesday, May 13, 2009

Learn jquery

Tired of your websites looking years old? Want a bit of that "Web 2.0" look-n-feel?

I highly recommend web developers learn jquery. The overhead of the included JavaScript (15K) and the learning curve are both so minimal that there shouldn't be an excuse for not using it.

There are not a lot of books out there on jquery yet (at least not around May 2009), but I would recommend this one.

Learning jQuery 1.3.

It's a pretty good book. The index is bad, but the coverage of jquery in the book should be well worth the read.

Thursday, April 16, 2009

Web developers should know CSS

I speak from experience when I say that web developers should understand the basics of cascading style sheets (CSS). Granted, when I first looked at CSS and tried using it for page layouts a few years ago, I couldn't understand why anyone would use CSS for layouts when we all used tables instead*.

Well, years later I'm convinced that tables are for presenting tabular data, not page layouts.

Developers don't have to be CSS gurus, but they should understand what a "div" really is and how a "float" style attribute works. They need to understand the "boxing model" and how IE handles it differently than do other browsers. CSS is much more than just applying different colors and text attributes.

If you want to take your web development skills up a notch, I recommend the following book I purchased a few years back. It is short, concise, and easy to read. Plus, as far as books go, it's pretty cheap.

CSS Mastery from Amazon

Update: A major advantage to knowing CSS is that it will make the adoption of tools such as jquery much easier!

*About the only time I favor a table layout is when I have to bottom-align tabular data and don't want to set a fixed height for content.

Tuesday, March 17, 2009

Windows Explorer - Setting default size and location

Want to set the default size and window location for Windows Explorer on Vista or later?

I've got a new monitor that is larger than my old one. When I would open Windows Explorer the first thing I would always do is resize it to larger size.

I found out you can set the default Explorer size and windows location by simply holding the SHIFT key down when closing the window. Presto!

Sunday, March 8, 2009

Catch (Exception e): Right or wrong?

I've long heard that you shouldn't throw a type Exception in C#. I try to always throw a type derived from Exception.

In addition, I've also read many brief snippets over the years that says you shouldn't write C# code like:

catch(Exception e)
{
...
}

I recently read the best explanation of this in Microsoft's MSDN Magazine (Feb. 2009). The article is entitled Handling Corrupted State Exceptions and would highly recommend those programming in C# to read the full article. It also provides details about how exception handling will change a bit in .NET 4.0.

A read well worth it!

http://msdn.microsoft.com/en-us/magazine/dd419661.aspx

Tuesday, February 17, 2009

ASP.NET CodeBehind vs. CodeFile

I use Visual Studio 2008. Once and a while I get a .NET error that causes me to modify the @Page directive from CodeBehind to CodeFile, or visa-versa.

Folks tend to give all types of lengthy explanations on when to use one vs. the other. Here is a quick explanation...

.NET Web Sites ---> Use CodeFile
.NET Web Apps ---> Use CodeBehind

Done. Any questions?

Tuesday, February 3, 2009

Remove / Uninstall old devices (network card)

I've run into this twice during the past month so I figured I had better post something about it.

Once and a while I remove something like a network or graphics card from a computer. If I do this while the computer is off, then the next time the computer is turned on, the old device is still there, sometimes hidden. Attempts to install a replacement device may either fail because of "not enough resources" or something else like a PCI slot conflict.

First, before uninstalling a device, use the device manager and perform an Uninstall. If you do that, the information below won't be needed. But if you are like me and remove device while computers are off (normally old ones sitting in closets), you may need to do the following...

Start the computer and open a console window (may want to run with Admin privs for Vista or later operating systems).

Set the following environment variable:

set devmgr_show_nonpresent_devices=1

Now open the device manager via the console window (must be done via the console window since this is where the new environment variable exists).

devmgmt.msc

On the View menu, enable "Show Hidden Devices" then find the old/removed device and uninstall it.

For best results, reboot!

Saturday, January 31, 2009

Obtaining computer SID and Processor ID using C#

Sometimes you have to track a unique identifier for an application (e.g. license key registration). Here are two small C# methods to get the Windows SID (Security ID) and the motherboard processor ID (may be more than one).

using System;
using System.Linq;
using System.Management;
using System.Security.Principal;

namespace ConsoleTest
{
class Program
{
static void Main(string[] args)
{
new Program().Run(args);
}

private void Run(string[] args)
{
string sid = UserSecurityId();
Console.WriteLine("The SID is: {0}", sid);

string[] processorIds = ComputerProcessorId();
foreach (string id in processorIds)
{
Console.WriteLine("Processor ID: {0}", id);
}
}

public string UserSecurityId()
{
return WindowsIdentity.GetCurrent().User.AccountDomainSid.ToString();
}

public string[] ComputerProcessorId()
{
return new ManagementObjectSearcher("Select * from Win32_Processor")
.Get()
.OfType<ManagementObject>()
.Select(o => o["ProcessorID"].ToString())
.ToArray();
}
}
}

Sunday, January 25, 2009

TFS Lightweight Scrum Template Graphic

Here is an 17" x 11" graphic of the TFS Lightweight Scrum template. It is pretty much the same as the one on the codeplex site (which is terrible quality). The scaling of the graphic is such that you can place it on 8.5" x 11" paper as well.

Enjoy.

Click here for graphic.

Sunday, January 18, 2009

Converting Office 2007 Excel to XML using C#

Don't want Office 2007 installed on your web server to access Excel 2007 content?

Here is a Visual Studio C# solution that demonstrates how to read an Excel 2007 workbook, converting it to XML. You don't need Office 2007 installed in order to do this! Once Excel content is in XML format you can process it easier using transformations tools or other language code.

The solution code uses .NET 3.5 XML support and LINQ. The solution also contains an example console application that will take an Excel 2007 file name on the command-line and convert it to XML via the console. Everything should run out-of-the-box using an example Excel 2007 file that is included; just run the example application and accept the default file name to read.

The quickest way to learn how to use the ExcelReader class, and its options, is to examine the brief Program.cs file in PCarver.OfficeXml.ExampleExcel project. You an register for events to transform cell contents and ignore certain rows (see example code).

Download only the binary files for converting Excel to XML:

Click here for binaries/executables

Download the full Visual Studio Solution:

Click here for Visual Studio 2008 Solution

If you have any bug fixes or nice enhancements, please post a comment and let me know.

Information from Microsoft on these formats is found at:

http://msdn.microsoft.com/en-us/library/aa338205.aspx.

Friday, December 19, 2008

Select random songs for MP3 player

I recently upgraded the firmware on my old Nano Plus MP3 player. As a result, the Windows Media Player, running under Server 2008, won't detect the device any more. Since I periodically toss around 50 random songs on my player for running purposes, and the fact that I'm too cheap to buy another compatible player, I wrote a quick-n-dirty lil' C# Windows Forms application to do this.

The application will simply select n random songs given a bunch of different input and output parameters. The UI looks like:



You have a few nice options like providing regular expression filters for including or excluding certain files. Whatever settings you use will be saved in an XML file in your application data folder.

You can download the C# solution by clicking here. It's not the best code, but hey, I needed it before a race tomorrow.

Tuesday, December 2, 2008

Avoiding Embedded T-SQL in Query String

I've seen folks trying to hack a web site by passing large amounts of hex-encoded T-SQL commands embedded withing a query string...and it's a bit disturbing. I like to be a bit proactive when it comes to this type of thing so I tend to use an a custom HTTP module that I register in my web.config. I write a class and place the file in my App_Code folder, then register it in my web.config like:

<httpModules>
<add name="RestrictionHttpModule" type="HttpModule.RestrictionHttpModule"/>
</httpModules>

The C# class looks something like this:

using System;
using System.Web;

namespace HttpModule
{
public class RestrictionHttpModule : IHttpModule
{
public RestrictionHttpModule()
{
}

#region IHttpModule Members

public void Dispose()
{
}

public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(Application_BeginRequest);
}

private void Application_BeginRequest(object source, EventArgs e)
{
HttpContext context = ((HttpApplication)source).Context;

// watch out for t-sql commands that may be embedded
string query = context.Request.Url.Query.ToLower();
if (!string.IsNullOrEmpty(query))
{
if (query.Contains(";declare") ||
query.Contains("exec(") ||
query.Contains("cast(") ||
query.Contains("convert("))
{
context.Response.StatusCode = 403; // forbidden
}
}
}

#endregion
}
}


This is just an example. My actual handler is a bit more robust. I include code that also allows me to reject remote host IPs, remote host names, and referrer names. Since my global exception handler email includes the full URL for any exceptions, it's easy for me to see which type of remote sites are attempting to hack, login without credentials, etc.

Can't RDP? How to enable / disable virtual machine firewall for Azure VM

Oh no!  I accidentally blocked the RDP port on an Azure virtual machine which resulted in not being able to log into the VM anymore.  I did ...