Coffee and Code

The Life and Times of Jeff Woodman

Home >> Tag: development

Entries Tagged with “development”

First Previous 1 2 3 4 Next Last  Displaying 1 - 5 of 20 entries.

ASP.Net - Supporting Currency Input

Posted: 05/13/2011 01:23 PM | permaLink

Tags: development asp.net regex

Question:
I have an ASP.Net application that allows users to enter currency amounts, which are stored in the SQL Server database with the type money. How can I support input of USD currency values, including the dollar sign and thousands separators, within the context of ASP.Net databound controls such as ListView, and declarative data access controls such as SqlDataSource?

Answer:
There are a few little tricks you need to know to support currency input in this scenario.

1. The most important, magical trick I've found is to simply declare your parameter with no Type or DbType attribute.
Example: <asp:parameter name="TuitionPerCredit" />

2. Use a RegularExpressionValidator control with a really solid, well tested expression for validating the currency input The expression I've been using is applicable to US currency, so if you're coding for another culture, the advice in #1 still applies but you'll need a regex that applies to your specific currency format

The currency regex I use is below:

\$?-?([1-9]{1}[0-9]{0,2}(\,\d{3})(.\d{0,2})?|[1-9]{1}\d{0,}(.\d{0,2})?|0(.\d{0,2})?|(.\d{1,2}))$|^-?\$?([1-9]{1}\d{0,2}(\,\d{3})(.\d{0,2})?|[1-9]{1}\d{0,}(.\d{0,2})?|0(.\d{0,2})?|(.\d{1,2}))$|^(\$?([1-9]{1}\d{0,2}(\,\d{3})*(.\d{0,2})?|[1-9]{1}\d{0,}(.\d{0,2})?|0(.\d{0,2})?|(.\d{1,2})))$

The regex above will validate entries like: $3, $3.00, $3,000, and $3000.00 It's not perfect, because it will validate something like $3000[.] (a digit at the end without the following two digits). However, I've found that SQL Server ignores the trailing period, so it's not a serious issue. The regex was posted on StackOverflow.com by the user JohnM, at the following link:
http://stackoverflow.com/questions/354044/what-is-the-best-u-s-currency-regex

Finally, one additional word of advice: If you application has many areas where currency values can be entered or edited, centralize your Regex pattern inside of a resource file. That way, whenever you need to apply a RegularExpressionValidator control, you can obtain the pattern for use in the ValidationExpression property like so:

<asp:regularexpressionvalidator>
   ID="TuitionRegularExpressionValidator
   ControlToValidate="InputTextBox"
   ErrorMessage="The value entered in the 'Tuition' field is not a valid USD currency value."
   ValidationExpression="<%$ Resources:CommonValidation, Currency %>"
   runat="server" />

I am counting down to a full week off work beginning Friday, May 13th at 5:00 PM sharp. Oh yeah!


For Performance Issues, Firebug Rocks

Posted: 03/20/2008 01:05 PM | permaLink | Comment on this entry!

Tags: development asp.net firebug tools

Firebug!
Do you want real, ultimate, client-side debugging power? Get Firebug today.

This week I tackled some performance issues with my new weblog. I had noticed that the weblog took an awfully long time to load. So, I navigated to my weblog and launched Firebug. Firebug's network monitor quickly informed me that:

  • The request to the weblog page itself was averaging over 1 second in duration.
  • The size of the HTML response for the page was in excess of 50 Kb.
  • AJAX resources and Script.aculo.us libraries were unneccessarily loaded on every request - these are only needed in an authenticated context.

Damn, I loves me some Firebug!

Using this information, I determined that the ViewState (A hidden form tag on the page) content was sometimes as large as 23 Kb! In other words, the ViewState information accounted for almost 50% of the markup on the page.

Some minor code changes resulted in a HUGE reduction in the ViewState size. For one particular weblog entry, the viewstate was reduced from 23552 bytes to 236 bytes - a 98.99% difference!

In addition to the viewstate optimizations, I also did the following:

  • Reduced the total number of items that are fetched for each request to the page - From 17 to 5, on average.
  • Implemented caching where I could to reduce round-trips to the database (From 7 round-trips to 1 round-trip per request).
  • Replaced the Archive TreeView with a simple repeater. The TreeView is expensive to initialize and populate server side, and emits tables which are a Web 2.0 no-no in most cases.

If you're a web developer, Firebug will make your life easier. For ASP.Net developers, I also recommend Binary Fortress's ViewState Helper for Internet Explorer - This tool will decode viewstate for you so you can examine the contents, as well as giving you the ViewState size and percentage of the page's content that the ViewState consumes.


Developer in Isolation Seeks Brain Candy

Posted: 03/04/2008 06:59 PM | permaLink | Comment on this entry!

Tags: development web-design inspiration

I turn 31 tomorrow! Another year gone by - and I'm still just a big kid. When will I finally grow up? Never, hopefully. I took the day off and the party begins TONIGHT.

In Need of Variety

My current job consists almost entirely of programming. I actually picked this job specifically because it would allow me to focus on development - whereas in my previous job, my duties included graphic design, web design, ASP coding, Flash and ActionScript, server administration and database administration a sh**load of stuff.

I primarily design web applications for our intranet, which allows me to play with UI design a bit, but let's face it: you have to rein in your creative instincts quite a bit in the context of a corporate intranet. While this focused environment has allowed me to improve my development skills, I find myself jonesing once again for creative web design.

For the past two weeks or so, I've been dedicating some time to trolling the various CSS / web design galleries and web designer sites; I needed to crawl out of my digital coffin, put my ear to the ground, and get back in touch with the design community. Mind you, I am NOT a professional web designer. My designs don't come close to those produced by artists with real talent and skills to back it up. Even so, the process of web design is a lot of fun for me, and it's a great way to shift gears after writing volumes of code at work. I also like to promote CSS-based design and the use of W3C standards - two concepts that have the potential to make life easier for web designers and developers alike.

One of the first questions I had as I ventured into the wild was: What new and exciting things are designers doing these days?

Some answers:

Misdirected Energy

My biggest flash of inspiration came from this article at A List Apart (WTF!?!?!? There's a Web 3.0?? I'm not used to Web 2.0 yet!!!) Zeldman makes mention of Pareto's 80/20 principle in relation to web design; specifically remarking on the success of developers who, eschewing complex development methodologies and overblown, complex application designs, instead focused on creating elegant, easy-to-use, feature-rich applications that people find fun and enjoyable to use. There is much wisdom is this...

In the process of designing the current incarnation of my weblog utility for this site, I put way too much focus on the architecure, the "under the hood" stuff that users never see. I've got pluggable data sources, via the Provider model. I'm using XML serialization to generate RSS feeds. I'm patching in to the HTTP pipline to rewrite URLs. I've got a litany of business objects that my application uses. Abstract this, interface that. Ad nauseum.

I ended up with a weblogger that is useable, even over-powered, but it's feature-poor and doesn't encourage community involvement. I focused my energy on the 80% of the work that didn't matter as much. (Is this starting to sound like a future re-write project? I think it is.) A case of not seeing the forest through the trees, perhaps.


Script.aculo.us is Mir.aculo.us!

Posted: 02/17/2008 02:18 PM | permaLink

Tags: development props javascript

scriptaculous.jpg Scriptaculous is an uber-cool set of javascript libraries geared toward rich, interactive user interface design. It's built on top of Prototype, another fantastic javascript framework,  and delivers a ton of easy-to-use, cross-browser effects, transitions, and other user interface tools.

I don't know why it took me so long to get into scriptaculous... maybe it's the do-it-yourself punker in me that avoids such conveniences (But why rewrite all of that code, when scriptaculous is well-written, robust, and free??) At any rate, I finally downloaded the libraries a few days ago while out searching for a good drag 'n drop script. Now, I feel like kicking my own ass for not downloading scriptaculous sooner!

The scriptaculous website has many usage examples, but I thought it was a little difficult to find specific usage examples; there were a couple of instances where I resorted to viewing the source code on the demo pages. All that aside, in many cases you can enable the scriptacular fanciness with one line of javascript (such as registering a draggable element on a page):

Not bad! And here's a quickie function that toggles between SlideUp and SlideDown:

function doSlide(id)
{
    var elem = document.getElementById(id);
    if (elem.style.display == "none")
        new Effect.SlideDown(id);
    else
        new Effect.SlideUp(id);
}

Script.aculo.us is, in my humble opinion, a worthwhile addition to any web developer or designer's arsenal.


Project: Complete. Bottle: Empty

Posted: 01/22/2008 12:31 AM | permaLink | Comment on this entry!

Tags: development personal

I'd like to welcome you to my new weblog page! I'm publishing this entry from my new hand-rolled weblog editor, which has been far too long in coming.

I'm rather proud of this new version - it's a vast improvement over my last weblogging utility (in production from late 2005 until last night) and for one thing gives me freedom to publish images and things like those YouTube widgets.

You'll have to pardon the spartan aesthetic I've got going on here! The reason the page looks so austere at present (future viewers: black text on a white background and no images), is that I'm working on making the look-and-feel editable on the fly from the editor.

The reason this project took me so darn long (almost 2 years) is that I was using the project to get hands-on experience with a bunch of new concepts I was learning during that timespan. Before too long, the project became moderately complex, and I found myself faced with a few architectural quandries that, quite frankly, made me queasy. Hence, I took long "sabbaticals" in between weeklong bouts of furious coding. :-)

Anyway, more later! It's late, but there's still time to savor on this Martin Luther King Jr.'s Day holiday.

Oh, and P.S., I converted most of the rest of my website to ASP.Net pages as well... There are a couple of links missing from the code toolbox page (AJAXGrid and RssAggregator) but I will have those patched up very soon. Fixed!
First Previous 1 2 3 4 Next Last  Displaying 1 - 5 of 20 entries.