Coffee and Code

The Life and Times of Jeff Woodman

Home >> Tag: asp.net

Entries Tagged with “asp.net”

First Previous 1 2 Next Last  Displaying 1 - 5 of 6 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.


6,608 Ways to Abuse your PC

Posted: 12/12/2007 06:00 PM | permaLink

Tags: development humor asp.net

Wow, I've gone a whole month without posting anything to this blog. I am LAZY.

I'm currently well-caffeinated and happy, other than having to deal with some minor gastrointestinal misery for the past couple of days. Oh, our telephone line is on the blink (again). This tends to happen when we get a lot of snow; last winter we went through 5 or 6 days without a phone line. Oddly enough, my internet connection still works sometimes. Time to call the landlady!

So I've been [helping] a friend out with an ASP.Net knowledge base application for the past few days. A couple of nights ago I was working on a module that uploads Flash-format tutorials to the application. Anyway, at one point when I was testing the module, I did an upload and waited for the response.... After about 10 seconds I figured something was screwed up; No page was coming back and I had to Ctrl+Alt+Delete out of Firefox.

I fixed the problem that night, but didn't find out until the next day what had actually happened: due to some hasty coding and inadvertent recursion, my application had created 6,608 copies of an uploaded SWF, nearly filling up my hard drive...

I am a danger to myself and to my computer.


The Straight Dope

Posted: 03/24/2007 02:03 AM | permaLink

Tags: development personal humor asp.net blog

So this teenage kid and his mammy were standin' in front of me in line at Walgr**ns. He says to his mom, 'I gotta start shaving. Clean-shaven men get all the best jobs.'

And here I am, lookin' all haggard with 3 days worth of stubble and camo pants on.... wild-ass hair... so I say to the mammy: 'Well, I guess I'm outta luck!' And the mom says - I love this part - 'Oh, you see, my son means a STRAIGHT job.'

Ho ho.

Anyway, I found this link at leftslipper's website while searching for how to bind an ObjectDataSource to an object factory. The article not only told me what I needed to keep my new weblogger project rolling, but offers an alternative way to handle objects: namely acquiring them rather than constantly creating and destroying them.


Adventures in Databinding

Posted: 02/10/2007 06:23 PM | permaLink

Tags: development asp.net tech

Here are a couple of ASP.Net databinding / formatting tricks I've picked up recently:

1. I recently had to bind to a telephone number in a GridView that was stored in the database as a 10-character string. I ran into problems trying to use a DataFormatString property to format the output as (555) 555.5555. Turns out that format strings generally work better with numbers than strings.... duh.

C# Solution: Convert.ToInt64(string).ToString('(000) 000.0000') did the trick. I can't remember where I found the solution, but it's really useful! Sure beats writing a custom method to re-format the number.

2. When using a BoundField to display a DateTime, the DataFormatString property will only work when the HtmlEncode property is set to false.

First Previous 1 2 Next Last  Displaying 1 - 5 of 6 entries.