using timestamp (SQL Server) to maintain concurrency in ASP.NET

G

Guest

Hi:

I am trying to use timestamp field of SQL Server to maintain concurrency. My
problem is how do I store the timestamp value in my webform. The hidden field
does not work as I get some cast error. If I can not store the value, how
should I return the timestamp to the database (with other values) to compare
with the current timestamp to check for updates.

You answers will be greatly appreciated.

Thanks
 
I

Igor Kramaric

Maybe by using the session object:

First request:

dtTimeStampVar DateTime;
dtTimeStampVar = // here you read the timestamp value from your SQL server
Session["timestamp"] = dtTimeStampVar


Next request:

dtTimeStampVar DateTime;
if (Session["timestamp"] != null)
dtTimeStampVar = Session["timestamp"]



Igor
 
G

Guest

Hi Igor:

Thanks for the reply. I think timestamp (SQL Server DataType) is not
equivalent to DateTime in .NET? So if we know the data type may be we can use
the session variable. I need to look for the correct equivalent datatype in
..NET.

Any other approach?



Igor Kramaric said:
Maybe by using the session object:

First request:

dtTimeStampVar DateTime;
dtTimeStampVar = // here you read the timestamp value from your SQL server
Session["timestamp"] = dtTimeStampVar


Next request:

dtTimeStampVar DateTime;
if (Session["timestamp"] != null)
dtTimeStampVar = Session["timestamp"]



Igor

mybappy said:
Hi:

I am trying to use timestamp field of SQL Server to maintain concurrency.
My
problem is how do I store the timestamp value in my webform. The hidden
field
does not work as I get some cast error. If I can not store the value, how
should I return the timestamp to the database (with other values) to
compare
with the current timestamp to check for updates.

You answers will be greatly appreciated.

Thanks
 
G

Greg Burns

Some misc. code to work with a SQL timestamp (aka rowversion):

I sometimes like making it an attribute on my page:

Here I store the original value and the rowversion...

<asp:foobar runat=server RowVersion='<%#
ByteToString(Container.DataItem("RowVersion")) %>' OriginalValue='<%#
Container.DataItem("ApprovedHours") %>' />

Protected Function ByteToString(ByVal rowVersion As Byte()) As String
Return Convert.ToBase64String(rowVersion)
End Function

Dim ApproveHours As Int16 =
CType(CType(grid.Items(selectedIndex).FindControl("lblApproveHours"),
Label).Attributes("OriginalValue"), Int16)
Dim sRowVersion As String =
CType(CType(grid.Items(selectedIndex).FindControl("lblApproveHours"),
Label).Attributes("RowVersion"), String)
Dim byteRowVersion() As Byte = Convert.FromBase64String(sRowVersion)

Here is how to use with a parameter...

Dim rowVersion() As Byte

With cmd.Parameters.Add("@RowVersion", SqlDbType.Timestamp)
.Value = rowVersion
.Direction = ParameterDirection.InputOutput
End With

rowVersion = CType(cmd.Parameters("@RowVersion").Value, Byte())

HTH,
Greg
 
J

Jeff Louie

mybappy... Also check out MSDN magazine Sept 2004 p21 Handling Data
Concurrency Using ADO.NET John Papa

Regards,
Jeff
I am trying to use timestamp field of SQL Server to maintain
concurrency.
 
G

Girish bharadwaj

If you dont mind stored procedures, you might want to consider embedding
this logic within one and your application call that SP for
insert/update/deletes.

Oh, for timestamp, I think you need to use Byte array.

--
Girish Bharadwaj
http://msmvps.com/gbvb
mybappy said:
Hi Igor:

Thanks for the reply. I think timestamp (SQL Server DataType) is not
equivalent to DateTime in .NET? So if we know the data type may be we can use
the session variable. I need to look for the correct equivalent datatype in
.NET.

Any other approach?



Igor Kramaric said:
Maybe by using the session object:

First request:

dtTimeStampVar DateTime;
dtTimeStampVar = // here you read the timestamp value from your SQL server
Session["timestamp"] = dtTimeStampVar


Next request:

dtTimeStampVar DateTime;
if (Session["timestamp"] != null)
dtTimeStampVar = Session["timestamp"]



Igor

mybappy said:
Hi:

I am trying to use timestamp field of SQL Server to maintain concurrency.
My
problem is how do I store the timestamp value in my webform. The hidden
field
does not work as I get some cast error. If I can not store the value, how
should I return the timestamp to the database (with other values) to
compare
with the current timestamp to check for updates.

You answers will be greatly appreciated.

Thanks
 
G

Guest

Thanks for all of your replies. Yes I could capture timestamp using byte
array byte[] and then stored the value in session variable. I think using
session variable is not a good idea so I will try to use viewstate later ...

Girish bharadwaj said:
If you dont mind stored procedures, you might want to consider embedding
this logic within one and your application call that SP for
insert/update/deletes.

Oh, for timestamp, I think you need to use Byte array.

--
Girish Bharadwaj
http://msmvps.com/gbvb
mybappy said:
Hi Igor:

Thanks for the reply. I think timestamp (SQL Server DataType) is not
equivalent to DateTime in .NET? So if we know the data type may be we can use
the session variable. I need to look for the correct equivalent datatype in
.NET.

Any other approach?



Igor Kramaric said:
Maybe by using the session object:

First request:

dtTimeStampVar DateTime;
dtTimeStampVar = // here you read the timestamp value from your SQL server
Session["timestamp"] = dtTimeStampVar


Next request:

dtTimeStampVar DateTime;
if (Session["timestamp"] != null)
dtTimeStampVar = Session["timestamp"]



Igor

Hi:

I am trying to use timestamp field of SQL Server to maintain concurrency.
My
problem is how do I store the timestamp value in my webform. The hidden
field
does not work as I get some cast error. If I can not store the value, how
should I return the timestamp to the database (with other values) to
compare
with the current timestamp to check for updates.

You answers will be greatly appreciated.

Thanks
 
G

Guest

Hi

Right I've decide to start giving a little back to the web
-> sorry about the delay

We've had the same 'issue' and we dug up that back in VB 6 the following
approach was used

The timestamp is essentailly a counter kinda like a database wide identity
and what they did was when you are returning the Timestamp from the database
it was cast as a bigint

So if we do the same in .NET we can throw the returned bigint into an Int64

This will take most of the hassle out of it as you'll be able to put it into
hidden text boxes etc

Cheers

Felix

PS If anyone knows of any flaws in this could you please let me know as we
are about to use it in a project
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top