MUST+Web, a complete translation end-end of Access to SQL Server and for Visual Studio. Click to see demonstration web site
MUST Logo

Formatting date and currency data in ASP.net

This article has been written from a GB globalization perspective, but the same ideas apply when using formatting for other countries. What follows assumes you are working with ListView, GridView, DetailView or FormView controls and you need better control over date and currency formatting.

Web.config

The first step to configuring currency and date formatting is to make a minor addition to the web.config file, and example of which is shown below.




Top of Page
Basic formatting

When you are displaying data using a GridView or other controls you will find that currency data is shown without the currency symbol to four decimal places, and datetime data is displayed with both date and time; as shown below.



Below we have some formatting examples which give more control over formatting when working with currency and dates.







These result in the following layouts.




Top of Page
Editing and Inserting

Editing and inserting data is a little more complicated when it comes to currency, if you format the currency to include the currency symbol then you will need to add some program code. If you are prepared to avoid displaying the currency symbol but want to control the number of decimal places, then no additional code is required, examples shown below.




But if you wish to include the currency symbol, you will need to add program code which removes the currency symbol during the updating event for the control.




Placing the above in an edit or insert item template will result in errors such as the following.



Below is a link to sample code in C# which adds the necessary conversion code to avoid these errors.

C# Sample Code

We have below included our own sample code in VB, this example was designed with a LINQ data source, and we have found it necessary to convert both the old and new values during the updating event.



Top of Page

MUST+WEB converting Access Databases to .net with Visual Studio or Visual Studio Express. Supports development in Visual Basic and C# using either SQL, LINQ or EntityData Model data sources. Click below to improve your development productivity.

Top of Page

by Andrew Couch Office Access MVP, Director ASC Associates andy@ascassociates.biz