ASP.NET Web Pages – Edit Data Page

[ This is a 4 part tutorial, previous tutorial read data page ]

In the Update.cshtml file insert following code:

var Id = Request["id"];
if (!Id.IsInt())

var db = Database.Open("StarterSite");
var SQLREAD = "SELECT * From Games Where Id=@0";
var data = db.QuerySingle(SQLREAD, Id);
var GameName = data.GameName;
var mTitle = data.mTitle;
var mDescription = data.mDescription;
var Text =  data.Text;

Similar to the read data page earlier, we load the current stored data for the requested page. The where clause will filter out the page (or row in this case).  The ID variable will be used to request the page you want to update, so for example the URL will look like update.cshtml?Id=1, and the row with the ID 1 will be loaded. The SQL command can be simply translated like this:

var SQLREAD = "SELECT * From Games Where Id=1";

The If Statement ensures that the value of ID is an integer. This is a security check; if the value is not an integer the user is redirected back to the homepage.

if (IsPost)
    GameName = Request["GameName"];
    mTitle = Request["mTitle"];
    mDescription = Request["mDescription"];
    Text = Request.Unvalidated("Text");
    Validation.RequireFields("GameName", "mTitle", "mDescription", "Text");
    var SQLUPDATE = "UPDATE Games SET GameName=@0, mTitle=@1, mDescription=@2, Text=@3 Where Id=@4";
    if (Validation.IsValid())
            db.Execute(SQLUPDATE, GameName, mTitle, mDescription, Text, Id);
            Response.Write("Data Saved!");
        }catch (Exception ex)

As with the create page we simply do the same thing except we are updating the data instead of creating a new row. We don’t update the release date column as that is not something which is supposed to be changed.


<form method="post">
<legend>Update Data</legend>
<label>Game Name</label>
<input type="text" value="@GameName" name="GameName"/>
<label>Meta Title</label>
<input type="text" value="@mTitle" name="mTitle"/>
<label>Meta Description</label>
<input type="text" value="@mDescription" name="mDescription"/>
<textarea class="ckeditor" name="Text">@Text</textarea>
<input type="submit"/>
<script src="/ckeditor/ckeditor.js"></script>

We use CKEditor to edit the page. As you can see, the elements have the value attribute; this will display the current data stored in the database.

ASP.NET WebPages - Update Data Page

[Continue, Delete Data Page]