473,809 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using SQL Server Timestamp

I have a database that I don't want to lock. I decided that before any
updates can occur I would check a timestamp value and ensure that nobody
else updated before I did (avoiding the 'last update wins' scenario).

I have a problem, I can read the Timestamp from the db when I read the
record. I currently use the data to pre-fill a form (gee go figure ;) ) and
the user changes some values and updates.

I don't know what to DO with the timestamp value while I am holding it. I
have tried putting it into a hidden value on the form but the value does not
seem to translate back and forth.

Do I have to store it as in memory as part of the session or can I somehow
convert to and from text??

I have about 0 experience working with byte arrays so the best answer is one
with an example.

Thanks.
Nov 18 '05 #1
1 8002
Hope this was what you were expecting! Here is some code from my project
that using a "timestamp" (aka rowversion)

"A future release of Microsoft® SQL ServerT may modify the behavior of the
Transact-SQL timestamp data type to align it with the behavior defined in
the standard. At that time, the current timestamp data type will be replaced
with a rowversion data type.

Microsoft® SQL ServerT 2000 introduces a rowversion synonym for the
timestamp data type. Use rowversion instead of timestamp wherever possible
in DDL statements. rowversion is subject to the behaviors of data type
synonyms."

(As far as what to do with it, that depends. I shove mine into ViewState
when the page load, and then read it again after postback)
Example of typical sproc:
CREATE PROCEDURE usp_SomeSPROC(
@MyID int,
@RowVersion rowversion OUTPUT,
<blah blah>
)
AS
SET NOCOUNT ON

BEGIN TRAN

DECLARE @ErrorSave int

DECLARE @CurrRowVersion rowversion

SELECT @CurrRowVersion = [RowVersion] FROM MyTable WHERE MyID = @MyID

IF @CurrRowVersion <> @RowVersion
BEGIN
ROLLBACK
RETURN -1
END

<do some tsql statements here>

IF @@ERROR <> 0
SET @ErrorSave = @@ERROR

SELECT @RowVersion = [RowVersion] FROM MyTable WHERE MyID = @MyID

IF @ErrorSave <> 0
ROLLBACK
ELSE
COMMIT
Back in my code, typical SQL method:
This is all psuedo code...

Public RowVersion(8) As Byte

Public Sub MyMethod(<blah blah>)
Dim cn As SqlConnection = New
SqlConnection(C onfigurationSet tings.AppSettin gs("ConnectionS tring"))
Dim cmd As SqlCommand = New SqlCommand("usp _SomeSPROC", cn)
cmd.CommandType = CommandType.Sto redProcedure

cmd.Parameters. Add("@MyID", Me.MyID)
cmd.Parameters. Add("RETURN_VAL UE", SqlDbType.Int). Direction =
ParameterDirect ion.ReturnValue

With cmd.Parameters. Add("@RowVersio n", SqlDbType.Times tamp)
.Value = Me.RowVersion
.Direction = ParameterDirect ion.InputOutput
End With

Try
cn.Open()
cmd.ExecuteNonQ uery()

If CInt(cmd.Parame ters("RETURN_VA LUE").Value) = -1 Then
' this row has been modified by somebody else!!!
Throw New ModifiedByAnoth erException
End If

Me.RowVersion = CType(cmd.Param eters("@RowVers ion").Value,
Byte())

Catch ex As SqlException
Throw New Exception("erro r while doing mymethod!", ex)
Finally
cn.Close()
End Try

End Sub

Public Class ModifiedByAnoth erException

Inherits System.Applicat ionException

Public Sub New()
MyBase.New("Tim esheet was modified by another user!")
End Sub

Public Sub New(ByVal InnerException As Exception)
MyBase.New("Tim esheet was modified by another user!",
InnerException)
End Sub

End Class

HTH,
Greg
"Roger Twomey" <ro******@vnet. on.ca> wrote in message
news:%2******** ********@TK2MSF TNGP14.phx.gbl. ..
I have a database that I don't want to lock. I decided that before any
updates can occur I would check a timestamp value and ensure that nobody
else updated before I did (avoiding the 'last update wins' scenario).

I have a problem, I can read the Timestamp from the db when I read the
record. I currently use the data to pre-fill a form (gee go figure ;) )
and
the user changes some values and updates.

I don't know what to DO with the timestamp value while I am holding it. I
have tried putting it into a hidden value on the form but the value does
not
seem to translate back and forth.

Do I have to store it as in memory as part of the session or can I somehow
convert to and from text??

I have about 0 experience working with byte arrays so the best answer is
one
with an example.

Thanks.

Nov 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
1657
by: Phoenix | last post by:
Here's a challenge that is killing me: I've got 2 web servers and a SQL Server and about 5,000 'users' who stay connected to the site all day. I have a page that is supposed to be 'real-time', so to do this, I have a 1px frame that refreshes every 15 seconds (so the other frame doesn't have to reload all the time--the top only reloads when a new record or a changed record hits the db). The real time data can be filtered in about 8...
4
6598
by: Prince Kumar | last post by:
I joined a company recently and they have a java program which hangs (does nothing) after a while. This is no way consistent. It could succeed quite a few times and can fail a few other times. There is no consistency when it fails. Could anyone here shed some light on how to debug/resolve the issue. I guess IBM looked at the issue and were not able to pinpoint where the issue is. When the program hangs and when force the DB2...
6
39838
by: bryan.seaton | last post by:
I have a delete statement that is not doing what I want it to do: Delete from LOG_TABLE where (DATE(LOG_TS)) < (DATE(CURRENT_DATE)- 21 DAYS); It is supposed to delete all records that are 21 days or older than the current system date. Instead it is deleting all new rows. LOG_TS is a timestamp but that should not matter since DATE returns just the date portion of a date or timestamp...right? I'm not a SQL guru but I can't see...
6
94555
by: Max | last post by:
Hi, I have SqlServer 2000 as back end and Access 2000 as front-end. All tables from Sqlserver are linked to Access 2000. I am having write conflict problem with one of my form which is bound to a query which consist of let say tableA , TableB and TableC (all are linked tables from SqlServer. While the form is open I am modifying TableA through code on Form_AfterUpdate Event and getting the following message.
3
2170
by: Fardude | last post by:
I have a few tables in SQL Server 2000 and a small Access 97 that has linked tables used to insert/update/delete into SQL Server 2k tables. Everything works fine except Updating one of these table through Access, I get "Write conflict error" which basicaly says since you have updated this record some other user has updated it. I know for sure this is not True, becuase noone else has access t this database. This does not happen to any...
6
1779
by: Greg Collins [MVP] | last post by:
For background, please refer to my original thread: http://groups.google.com/groups?selm=efe5w7tYEHA.3112%40tk2msftngp13.phx.gbl I've thought of a potential way around the issue, but I'm new to ASP.NET and C# and need the expertese of individuals with more experience in these languages than I have. I may be completely off target as I thought this up very early in the morning. Here's the process as I see it: 1. InfoPath saves out the...
7
4064
by: mybappy | last post by:
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.
0
1694
by: Jean-Michel POURE | last post by:
Dear friends, I am currently testing Ulogd ip traffic logging system with PostgreSQL. It works in conjunction with GNU/Linux iptables. The Ulogd project can be found here: http://gnumonks.org/gnumonks/projects/project_details?p_id=1 (If you try the project, fetch it from CVS as the PostgreSQL code was broken until today.)
1
6294
by: nickvans | last post by:
Hello generous SQL experts! I'm new to the SQL Server environment, but am trying to get up to speed. I am trying to help a coworker create a pivot table for use with an Infragistics chart. I have data in the form of: | GYR_ID | RCOUNT is a smalldatetime GYR_ID is an int which represents a category (1=green, 2=yellow, 3=red) RCOUNT is how many of that category are present
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10640
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10120
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7662
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.