Storing Uploaded Files in a Database or in the File System with ASP.NET 2.0

Posted by: Imar Spaanjaars at Saturday, January 6, 2007 4:12:08 PM in: ASP.NET 2.0

Update 2010/08/10: I just posted an article featuring a new version of this application based on the Provider Model design pattern. You can find the article here.

A common requirement in web sites is handling client files that are uploaded through the browser. Whether these files come from a protected Admin section where content managers can upload files, or from a public section doesn't matter; you need a way to retrieve and store these files in your system somewhere so they are available for viewers.

Allowing a user to upload a file is easy; just drop a FileUpload control on your page and .NET will handle the rest for you. However, it's after the file gets uploaded to the server where things get interesting. One of the things you have to take into account is where you're going to store the uploaded file. Two common places are the hard drive of your server and a database. There has been a lot of debate on the Internet about the best way to store your files. Some say the file system is the only acceptable option, while others really like the database solution.

In this article I'll show you how to store files both ways. I'll discuss the pros and cons of each solution, and show you the code you need to save your uploaded files. As a sample application, I'll build a small web site that allows you to upload files and store them at a location that you determine. You can decide where the files are stored through a simple setting in the web.config file. Depending on this setting, files are either stored directly at the hard drive of your server or in a database. You can even change this setting at run-time without affecting existing files.

Read on ...

Using the ValidatorCallout control in Combination with ASP.NET 2 Templated Controls

Posted by: Imar Spaanjaars at Saturday, December 16, 2006 3:50:45 PM in: ASP.NET 2.0

Now that the release date for the final version of ASP.NET AJAX 1.0 is getting closer (RC1 was released on December 14 2006), I decided to dig a little deeper in its feature set and see what it has to offer. I also looked at the ASP.NET AJAX Control Toolkit and the new controls that are part of this toolkit.

One of those new controls is the ValidatorCallout extender control that displays a big yellow call out with the error message.

Read on ...

Building a Simple Rating Control for ASP.NET 2.0

Posted by: Imar Spaanjaars at Wednesday, December 6, 2006 12:21:34 PM in: .NET General

UPDATE: 16-7-2008 I have updated the control and wrote a new article about it. This new release fixes a few bugs and comes with a better sample web site to try out the control. Check out the new article here.

To show visitors of your site how other visitors feel about the content you're presenting on your site, it's good idea to let your visitors rate your content.

Many web sites take this approach. For example, Amazon uses a 5 star rating approach to rate the articles they're selling. Microsoft's MSDN site uses a nine-bar graph to display the quality of their (technical) articles. My own site uses a five-bar graph to let users rate an article (in the left hand column). Since there are many uses for a rating feature, it makes sense to encapsulate the rating mechanism in a custom ASP.NET Server Control. This article shows you how to build such a control.

Read on ...

Changing the Login Control to Display a List of Users

Posted by: Imar Spaanjaars at Wednesday, December 6, 2006 10:07:10 AM in: ASP.NET 2.0

A while ago I saw this post on the P2P Wrox forum (near the middle of the thread) where the poster asked for a way to replace the standard TextBox in the asp:Login control with a list of user names from the system. He didn't mind the lowered security implications of this solution and instead wanted to make it a little easier for users to login. With his proposed solution, a user can pick a name from the list, enter a password and log in. I guess fewer things to remember means fewer calls to the helpdesk.

It turns out this is actually quite easy to do.

Read on ...

Retrieving the Parameter Marker Format When Using Generic Database Factories

Posted by: Imar Spaanjaars at Tuesday, September 19, 2006 7:57:27 PM in: .NET 3.5 General

With .NET 2, Microsoft introduced something called DbProviderFactories that allows you to work with databases in a generic fashion. That is, you don't create strongly typed objects like a SqlConnection or an OleDbConnection at design-time, but defer the decision what object to create till run-time. This way, you can write code that works against a number of different databases, like SQL Server, Oracle and OleDb databases like Microsoft Access.

One of the big differences between how these databases operate is the way you must name the parameters you send to a stored procedure or query. For example, SQL Server uses the @ symbol (@userName) and even allows you to leave out a prefix altogether. Microsoft Access in turn uses a question mark without a specific name while Oracle uses a colon as the prefix. Now, how do you deal with these differences when you don't know what database you're working with till run-time?

Read on ...

Using the Microsoft Access Providers to Replace the Built-In SQL Server Providers

Posted by: Imar Spaanjaars at Monday, August 21, 2006 8:30:59 PM in: ASP.NET 2.0

Update 2011/1/9: I just published a short article that shows you how to use the Microsoft Access Providers in an ASP.NET 4 web site.

The biggest features brought by ASP.NET 2.0 are most likely the new services for membership, roles, personalization and profiles. These services supply you with a lot of functionality out-of-the-box with little to no custom code. The services that ship with .NET 2.0 all use SQL Server as the data store (either the Express edition, or the full commercial versions) by means of a provider. But what if you can't use SQL Server, for example because your hosting company doesn't support it? In that case, you can use the Microsoft Access Providers, a shared source initiative released by Microsoft that allows you to use a Microsoft Access database for all the provider based features.

This article shows you how to acquire, compile and use these Microsoft Access Providers.

Read on ...

Building Smarter User Controls

Posted by: Imar Spaanjaars at Sunday, July 23, 2006 2:11:05 PM in: ASP.NET 2.0

User Controls are very common in today's ASP.NET websites, both in version 1.x as in version 2.0. Many sites use them to reuse content, like a menu, a header or a footer or even for partial page content, like a shop's product catalog.

However, User Controls can be used for more than simply displaying static, repeating content. With a bit of knowledge about how User Controls work, you can teach them a few tricks. By adding public properties to a User Control, they become much more powerful and easier to reuse.

In this article I'll show you how to build a User Control that displays information about an article, like a news item, similar to the Details section that you find at the top of most pages in my site.

Although I am using Visual Studio 2005 and ASP.NET 2.0 in this article, the same principles and code work for Visual Studio .NET 2002 / 2003 and ASP.NET 1.x as well.

Read on ...

Configuring an ASP.NET 2.0 Application to Work with Microsoft SQL Server 2000 or SQL Server 2005

Posted by: Imar Spaanjaars at Saturday, June 3, 2006 10:12:06 PM in: ASP.NET General
Over the past couple of weeks, I saw a number of posts on the Wrox Programmer to Programmer forum ( asking how to configure an ASP.NET 2.0 application to work with SQL Server 2000 or 2005. Since this is not an easy undertaking, and requires knowledge about ASP.NET 2.0, IIS and SQL Server, this article might help to familiarize you with some of the important concepts that you need to be aware of
Read on ...

Migrating to ASP.NET 2 - Part 5 - It's the Little Things - Or Why I Love ASP.NET 2.0

Posted by: Imar Spaanjaars at Tuesday, May 16, 2006 8:04:22 PM in: ASP.NET 2.0
This is the fifth and final part of a (long running) article series about migrating my ASP.NET 1.x Web site to ASP.NET 2. This installment focuses on the many little improvements in the .NET Framework in general and ASP.NET 2.0 and Visual Web Developer in particular.
Read on ...

Using Attributes to Improve the Quality and Usability of Your Code Part 2 - Improving Debugging

Posted by: Imar Spaanjaars at Saturday, May 13, 2006 6:51:31 PM in: ASP.NET 2.0

I am a big fan of attributes in .NET. Ever since I discovered them in .NET 1, I have been digging in the MSDN documentation to find more useful attributes that I can use to improve the quality of the code I write and design. With the release of .NET 2, the number of attributes has increased quite a bit. During my search I discovered a number of useful attributes that have helped me write better code so I thought it was time for a short article series about attributes. This installment, Part 2 - Improving Debugging shows you a number of useful attributes that you can apply to elements like methods and classes to improve your debugging experience.

Read on ... 1234567891011