---
uid: web-pages/overview/getting-started/introducing-aspnet-web-pages-2/updating-data
title: "Introducing ASP.NET Web Pages - Updating Database Data | Microsoft Docs"
author: tfitzmac
description: "This tutorial shows you how to update (change) an existing database entry when you use ASP.NET Web Pages (Razor). It assumes you have completed the series th..."
ms.author: aspnetcontent
manager: wpickett
ms.date: 05/28/2015
ms.topic: article
ms.assetid: ac86ec9c-6b69-485b-b9e0-8b9127b13e6b
ms.technology: dotnet-webpages
ms.prod: .net-framework
msc.legacyurl: /web-pages/overview/getting-started/introducing-aspnet-web-pages-2/updating-data
msc.type: authoredcontent
---
Introducing ASP.NET Web Pages - Updating Database Data
====================
by [Tom FitzMacken](https://github.com/tfitzmac)
> This tutorial shows you how to update (change) an existing database entry when you use ASP.NET Web Pages (Razor). It assumes you have completed the series through [Entering Data by Using Forms Using ASP.NET Web Pages](https://go.microsoft.com/fwlink/?LinkId=251582).
>
> What you'll learn:
>
> - How to select an individual record in the `WebGrid` helper.
> - How to read a single record from a database.
> - How to preload a form with values from the database record.
> - How to update an existing record in a database.
> - How to store information in the page without displaying it.
> - How to use a hidden field to store information.
>
>
> Features/technologies discussed:
>
> - The `WebGrid` helper.
> - The SQL `Update` command.
> - The `Database.Execute` method.
> - Hidden fields (``).
## What You'll Build
In the previous tutorial, you learned how to add a record to a database. Here, you'll learn how to display a record for editing. In the *Movies* page, you'll update the `WebGrid` helper so that it displays an **Edit** link next to each movie:
![WebGrid display including an 'Edit' link for each movie](updating-data/_static/image1.png)
When you click the **Edit** link, it takes you to a different page, where the movie information is already in a form:
![Edit Movie page showing movie to be edited](updating-data/_static/image2.png)
You can change any of the values. When you submit the changes, the code in the page updates the database and takes you back to the movie listing.
This part of the process works almost exactly like the *AddMovie.cshtml* page you created in the previous tutorial, so much of this tutorial will be familiar.
There are several ways you could implement a way to edit an individual movie. The approach shown was chosen because it's easy to implement and easy to understand.
## Adding an Edit Link to the Movie Listing
To begin, you'll update the *Movies* page so that each movie listing also contains an **Edit** link.
Open the *Movies.cshtml* file.
In the body of the page, change the `WebGrid` markup by adding a column. Here's the modified markup:
[!code-html[Main](updating-data/samples/sample1.html?highlight=6)]
The new column is this one:
[!code-html[Main](updating-data/samples/sample2.html)]
The point of this column is to show a link (`` element) whose text says "Edit". What we're after is to create a link that looks like the following when the page runs, with the `id` value different for each movie:
[!code-css[Main](updating-data/samples/sample3.css)]
This link will invoke a page named *EditMovie*, and it will pass the query string `?id=7` to that page.
The syntax for the new column might look a bit complex, but that's only because it puts together several elements. Each individual element is straightforward. If you concentrate on just the `` element, you see this markup:
[!code-html[Main](updating-data/samples/sample4.html)]
Some background about how the grid works: the grid displays rows, one for each database record, and it displays columns for each field in the database record. While each grid row is being constructed, the `item` object contains the database record (item) for that row. This arrangement gives you a way in code to get at the data for that row. That's what you see here: the expression `item.ID` is getting the ID value of the current database item. You could get any of the database values (title, genre, or year) the same way by using `item.Title`, `item.Genre`, or `item.Year`.
The expression `"~/EditMovie?id=@item.ID` combines the hard-coded part of the target URL (`~/EditMovie?id=`) with this dynamically derived ID. (You saw the `~` operator in the previous tutorial; it's an ASP.NET operator that represents the current website root.)
The result is that this part of the markup in the column simply produces something like the following markup at run time:
[!code-xml[Main](updating-data/samples/sample5.xml)]
Naturally, the actual value of `id` will be different for each row.
## Creating a Custom Display for a Grid Column
Now back to the grid column. The three columns you originally had in the grid displayed only data values (title, genre, and year). You specified this display by passing the name of the database column — for example, `grid.Column("Title")`.
This new **Edit** link column is different. Instead of specifying a column name, you're passing a `format` parameter. This parameter lets you define markup that the `WebGrid` helper will render along with the `item` value to display the column data as bold or green or in whatever format that you want. For example, if you wanted the title to appear bold, you could create a column like this example:
[!code-html[Main](updating-data/samples/sample6.html)]
(The various `@` characters you see in the `format` property mark the transition between markup and a code value.)
Once you know about the `format` property, it's easier to understand how the new **Edit** link column is put together:
[!code-html[Main](updating-data/samples/sample7.html)]
The column consists *only* of the markup that renders the link, plus some information (the ID) that's extracted from the database record for the row.
> [!TIP]
>
> **Named Parameters and Positional Parameters for a Method**
>
> Many times when you've called a method and passed parameters to it, you've simply listed the parameter values separated by commas. Here are a couple of examples:
>
> `db.Execute(insertCommand, title, genre, year)`
>
> `Validation.RequireField("title", "You must enter a title")`
>
> We didn't mention the issue when you first saw this code, but in each case, you're passing parameters to the methods in a specific order — namely, the order in which the parameters are defined in that method. For `db.Execute` and `Validation.RequireFields`, if you mixed up the order of the values you pass, you'd get an error message when the page runs, or at least some strange results. Clearly, you have to know the order to pass the parameters in. (In WebMatrix, IntelliSense can help you learn figure out the name, type, and order of the parameters.)
>
> As an alternative to passing values in order, you can use *named parameters*. (Passing parameters in order is known as using *positional parameters*.) For named parameters, you explicitly include the name of the parameter when passing its value. You've used named parameters already a number of times in these tutorials. For example:
>
> [!code-csharp[Main](updating-data/samples/sample8.cs)]
>
> and
>
> [!code-css[Main](updating-data/samples/sample9.css)]
>
> Named parameters are handy for a couple of situations, especially when a method takes many parameters. One is when you want to pass only one or two parameters, but the values you want to pass are not among the first positions in the parameter list. Another situation is when you want to make your code more readable by passing the parameters in the order that makes the most sense to you.
>
> Obviously, to use named parameters, you have to know the names of the parameters. WebMatrix IntelliSense can *show* you the names, but it cannot currently fill them in for you.
## Creating the Edit Page
Now you can create the *EditMovie* page. When users click the **Edit** link, they'll end up on this page.
Create a page named *EditMovie.cshtml* and replace what's in the file with the following markup:
[!code-cshtml[Main](updating-data/samples/sample10.cshtml)]
This markup and code is similar to what you have in the *AddMovie* page. There's a small difference in the text for the submit button. As with the *AddMovie* page, there's an `Html.ValidationSummary` call that will display validation errors if there are any. This time we're leaving out calls to `Validation.Message`, since errors will be displayed in the validation summary. As noted in the previous tutorial, you can use the validation summary and the individual error messages in various combinations.
Notice again that the `method` attribute of the `` tag in the markup:
[!code-html[Main](updating-data/samples/sample19.html)]
This markup uses the same syntax for an `` element that you've seen elsewhere. The URL includes `~` to mean "root of the website."
## Testing the Movie Update Process
Now you can test. Run the *Movies* page, and click **Edit** next to a movie. When the *EditMovie* page appears, make changes to the movie and click **Submit Changes**. When the movie listing appears, make sure that your changes are shown.
To make sure that validation is working, click **Edit** for another movie. When you get to the *EditMovie* page, clear the **Genre** field (or **Year** field, or both) and try to submit your changes. You'll see an error, as you'd expect:
![Edit Movie page showing validation errors](updating-data/_static/image4.png)
Click the **Return to movie listing** link to abandon your changes and return to the *Movies* page.
## Coming Up Next
In the next tutorial, you'll see how to delete a movie record.
## Complete Listing for Movie Page (Updated with Edit Links)
[!code-cshtml[Main](updating-data/samples/sample20.cshtml)]
## Complete Page Listing for Edit Movie Page
[!code-cshtml[Main](updating-data/samples/sample21.cshtml)]
## Additional Resources
- [Introduction to ASP.NET Web Programming by Using the Razor Syntax](https://go.microsoft.com/fwlink/?LinkID=202890)
- [SQL UPDATE Statement](http://www.w3schools.com/sql/sql_update.asp) on the W3Schools site
>[!div class="step-by-step"]
[Previous](entering-data.md)
[Next](deleting-data.md)