Saturday, June 30, 2007

Install an ASP.NET Assembly into the GAC

To add an assembly to the global assembly cache manually (specifically the ASP.NET AJAX assemply), go to Start > All Programs > Microsoft Visual Studio 2005 > Visual Studio Tools > Visual Studio 2005 Command Prompt. Type the following comman and insert the path/version you have:

>cd C:\Program Files\Microsoft ASP.NET\ASP.NET 2.0 AJAX Extensions\[version number] gacutil -i System.Web.Extensions.dll

Declare ASP.NET Controls in web.config Instead of Individual Pages

Instead of declaring controls on individual ASPX pages, declare them once in web.config.

<system.web>
<pages>
<controls>
<add tagPrefix="myCompany" src="~/ctl/usercontrol.ascx" tagName="MyControl"/>
<add tagPrefix="otherCompany" assembly="OtherControlAssembly"/>
</controls>
</pages>
</system.web>

Monday, June 25, 2007

A$$hole Driven Development

Scott Berkun has a funny post all of the different development methodologies out there. I'd say where I'm working now is somewhere between...
Development By Denial (DBD): Everybody pretends there is a method for what’s being done, and that things are going ok, when in reality, things are a mess and the process is on the floor. The worse things get, the more people depend on their denial of what’s really happening, or their isolation in their own small part of the project, to survive.
…and…

Shovel-Driven Development: Get it out the door as quickly as possible, cut-n-paste from anything that you find that works on Google, if it works it’s ready. Closely related to “Duct-tape Driven Design”

…with a healthy smattering of…

Decapitated Chicken Process: A time honored micromanagement technique where each day managers identify a drastic emergency and require developers drop what they are doing (and whatever process they are using) and immediately attend to the latest conflagration. Since this does the double duty of creating new bugs and making other tasks fall behind, fires become easier and easier for managers to spot and then freak out about. Practically a standard in the games industry.

Sad, but true.

http://www.scottberkun.com/blog/2007/asshole-driven-development/

Sunday, June 17, 2007

ASP.NET Dynamic Array

This is one approach to creating a dynamic array in ASP.NET:

public string[] CreateArray()
{
ArrayList myArrayList = new ArrayList();
// Connect to the database and create a data reader (myReader)
while (myReader.Read())
{
// various lines commented out
myArrayList.Add((string) myReader["name"]);
}
return (string[])myArrayList.ToArray(typeof(string));
}

ASP.NET Custom Validator for a Comma Separated List of Email Addresses

In your ASPX page:

<asp:CustomValidator ID="txtToCustom" runat="server" ControlToValidate="txtTo"
Display="Dynamic" ErrorMessage="One of the &quot;To:&quot; email
addresses you provided is not in the proper format or is not separated by a comma."
OnServerValidate="txtToCustom_ServerValidate">*</asp:CustomValidator>

In your code beside page:

protected void txtToCustom_ServerValidate(object source, ServerValidateEventArgs args)
{
args.IsValid = ValidateToAddresses(args.Value);
}

protected bool ValidateToAddresses(string addresses)
{
string[] myAddresses = Tokenize(addresses);
foreach (string address in myAddresses)
{
if (!IsEmail(address.Trim())) return false; // If it's not a valid address, return false.
}
return true; // If none of the addresses returned false, return true.
}

protected bool IsEmail(string address)
{
/* This regular expression is provided by the .NET Framework and is the same
* as the one used to check the from address. If that changes for any reason
* this should be updated to match. */
Regex emailRegEx = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*",
RegexOptions.IgnoreCase);
if (emailRegEx.IsMatch(address)) return true;
return false;
}

protected string[] Tokenize(string addresses)
{
/* This is pulled out as a separate function because I expect it to mature
* and change over time, and it will probably to move into a class library
* at some point. */
Regex separatorRegEx = new Regex(",");
return separatorRegEx.Split(addresses);
}

There is at least one very big assumption here: the domains provided by the user are correct. If you'd like to check these, there is a bit of code at http://www.codeproject.com/aspnet/Valid_Email_Addresses.asp that offers an approach.

Also, to do this, you need some sort of approach to string tokenization. Unlike Java, C# does not have any helper classes for this. There are basic approaches to this problem at http://en.csharp-online.net/CSharp_Regular_Expression_Recipes%E2%80%94A_Better_Tokenizer and http://www.dotnetwatch.com/page229.aspx.