Note Do not use the code sample that is described in this article in a production environment. The code sample is provided only as an illustration. This code sample is released under the terms of the Microsoft Public License (MS-PL).
Difficulty level

Download information
To download this code sample, click the following link:Technical overview
This sample includes the following two pages:- Default
- Image
In both the InsertItemTemplate template and the EditItemTemplate template, there is a FileUpload control for getting the specified image file when you perform an insert operation or an update operation. The following code uses a FileUpload control:
FileUpload uploadPicture = (FileUpload)fvPerson.FindControl("uploadPicture");
if (uploadPicture.HasFile)
{
cmd.Parameters.Add("@Picture", SqlDbType.VarBinary).Value = uploadPicture.FileBytes;
}
else
{
cmd.Parameters.Add("@Picture", SqlDbType.VarBinary).Value = DBNull.Value;
}
<asp:Image ID="imgPicture" runat="server"
ImageUrl ='<%# Eval("PersonID","Image.aspx?PersonID={0}") %>' Width="128px"
Height="128px" />
The Image page retrieves the image data from the database based on the value in a query string. Then, the Image page writes the byte array to the HTTP output stream to display the image.
Note For more information about how to create the sample application, see the Readme.txt file that is included in the download package.
Technology category
- ASP.NET
Languages
This code sample contains the following programming languages.Language | Project Name |
Visual C# | CSASPNETFormViewUpload |
Visual Basic .NET | VBASPNETFormViewUpload |
Prerequisites
- This sample application was created by using Visual Studio 2008 with Service Pack 1 installed.
- An instance of SQL Server 2005 or of SQL Server 2008 is required.
Note If your SQL Server instance is not an Express edition, attach the database file (.mdf) that is included in the App_Data folder, and then modify the connection string in the Web.config file.
Related links:
Working with User Instances
How to: Attach a Database (SQL Server Management Studio)
How To: Connect to SQL Server Using Windows Authentication in ASP.NET 2.0
How To: Connect to SQL Server Using SQL Authentication in ASP.NET 2.0