views:

217

answers:

2

I have a table with three columns that I need to display on a ASP.NET page. (SQL Server 2005, ASP.NET 2.0)

id int
value varchar(50)
tstamp timestamp

I use the timestamp field to handle concurrency validation so it's for internal use only and will never be displayed to the end user. But I need to store it somewhere in order to do proper updates.

Here's my update sproc.

UPDATE ValueTable SET value = @value
WHERE (id = @id) AND (tstamp = @tstamp)

SELECT @tstamp=tstamp FROM ValueTable
WHERE id=@id

I use a SqlDataSource to connect to my database and the schema has all three columns. My grid view will only display two fields since the timestamp field is hidden (Visible=False)

When I profile my asp page it looks like it doesn't store the timestamp anywhere even though I have a "hidden" field in the table.

How would you store a timestamp value in general on a web page? It should never be displayed, but it is needed for any updates.

A: 

If your using a gridview control and template fields, then you could use a hidden field for each row to store your timestamp.

If you are passing your data to a DataTable, which you then bind to a GridView, then you could access it from the datatable. (As long as you pass all 3 columns to the datatable.

I'd suggest using a hidden field in a template field on a gridview control.

kevchadders
A: 

After a lot of playing around I think I found a half-decent solution.

The problem seems to be that fields that are "Visible=false" are not included or bound in any update or delete commands.

The HiddenField works fine for Update commands, so if you are not doing any Delete commands you should be fine by including a hidden field which is bound (Bind(TStamp)) to the timestamp column in a templated field.

The problem is that if you are doing Deletes as well it doesn't look at any bound, hidden fields.

What I came up with was to add the timestamp to the data keys of the grid view. That way it will be considered a composite key together with the ID.

So, in short, add the timestamp to the DataKeyNames of the GridView/DetailsView etc, and remove any visible fields. That seemed to do the trick.

Mats Fredriksson