473,382 Members | 1,357 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Concurrency Checking - SQL Server Rowversion - Stored Procedures ASP.NET Command

I'm trying to test for concurrency, using a SQL Stored Procedure on a
RowVersion (timestamp) Field. The vb code I'm using is as follows

Dim cmd As New SqlCommand("ConcurrencyCheck", cn)
Dim intID as integer = MyDataset.Tables("MyTable").Rows(0).Item("ID")
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("@ReturnValue", SqlDbType.Int)
cmd.Parameters(0).Direction = ParameterDirection.ReturnValue
cmd.Parameters.Add("@ID", intID)

Dim concurParam As New SqlParameter
concurParam.ParameterName = "@Concurrency"
concurParam.Value =
MyDataset.Tables("MyTable").Rows(0).Item("Concurre ncyValue")
concurParam.SqlDbType = SqlDbType.Binary
cmd.Parameters.Add(concurParam)

cn.Open()

If cmd.ExecuteScalar() > 0 Then
Record Found ...
Else
Record not found ...
End If

cmd.Dispose()
cn.Close()
cn.Dispose()

Prior to submitting the form, I run a different stored procedure which
populates the "MyDataset" from the same row using:
SELECT * FROM MyTable WHERE ID = @ID

The code in the stored procedure that submits the form is:
SELECT Count(*) FROM MyTable WHERE ID = @ID AND ConcurrencyValue =
@Concurrency

How do I convert the originally retrieved data (RowVersion) back to a true
binary in ASP.NET to send it back to SQL? It currently is storing the field
as an array, for some reason. Thus, when I test this procedure and now
(without a doubt) no one has modified the record since, it ALWAYS returns
"record not found"

Thanks in advance for any help on this.

Bari
Nov 20 '05 #1
5 2689
a sql timestamp should come across as an 8 byte binary array. be sure to
specify size of 8. sql wants an 8 byte literal.

SELECT Count(*) FROM MyTable
WHERE ID = @ID
AND ConcurrencyValue = 0x000000000000906A

-- bruce (sqlwork.com)
"Bari Allen" <ba**@sos.REMOVETHIS.state.ga.us> wrote in message
news:ON**************@tk2msftngp13.phx.gbl...
I'm trying to test for concurrency, using a SQL Stored Procedure on a
RowVersion (timestamp) Field. The vb code I'm using is as follows

Dim cmd As New SqlCommand("ConcurrencyCheck", cn)
Dim intID as integer = MyDataset.Tables("MyTable").Rows(0).Item("ID")
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("@ReturnValue", SqlDbType.Int)
cmd.Parameters(0).Direction = ParameterDirection.ReturnValue
cmd.Parameters.Add("@ID", intID)

Dim concurParam As New SqlParameter
concurParam.ParameterName = "@Concurrency"
concurParam.Value =
MyDataset.Tables("MyTable").Rows(0).Item("Concurre ncyValue")
concurParam.SqlDbType = SqlDbType.Binary
cmd.Parameters.Add(concurParam)

cn.Open()

If cmd.ExecuteScalar() > 0 Then
Record Found ...
Else
Record not found ...
End If

cmd.Dispose()
cn.Close()
cn.Dispose()

Prior to submitting the form, I run a different stored procedure which
populates the "MyDataset" from the same row using:
SELECT * FROM MyTable WHERE ID = @ID

The code in the stored procedure that submits the form is:
SELECT Count(*) FROM MyTable WHERE ID = @ID AND ConcurrencyValue =
@Concurrency

How do I convert the originally retrieved data (RowVersion) back to a true
binary in ASP.NET to send it back to SQL? It currently is storing the
field
as an array, for some reason. Thus, when I test this procedure and now
(without a doubt) no one has modified the record since, it ALWAYS returns
"record not found"

Thanks in advance for any help on this.

Bari

Nov 20 '05 #2
"Bari Allen" <ba**@sos.REMOVETHIS.state.ga.us> wrote in message
news:ON**************@tk2msftngp13.phx.gbl...
How do I convert the originally retrieved data (RowVersion) back to a true
binary in ASP.NET to send it back to SQL? It currently is storing the
field
as an array, for some reason. Thus, when I test this procedure and now
(without a doubt) no one has modified the record since, it ALWAYS returns
"record not found"


Here is some code I grabbed that is using RowVersion and converting back and
forth...

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

Greg

Nov 20 '05 #3
Bari,

what I do is that I convert the TimeStamp value in a BigInt value which I
can easily pass back and forth to my stored proc. An example would look like
the following:

CREATE PROCEDURE dbo.spTest
(
@OIDTest uniqueidentifier
, @SomeValue as int

, @VersionOld bigint = 0 output
)

AS

declare @Error int
declare @rowcount int

update tblTest set

SomeValue = @SomeValue

where OIDTest = @OIDTest and TimeStp = @VersionOld
select @error = @@error, @rowcount = @@rowcount

if @error > 0
begin
-- an error occurred
goto FunctionErrorWrite
end

if (@rowcount = 1)
begin
-- everything is fine

-- get a new timeStamp
select @VersionOld = convert(bigint, TimeStp)
from tblTest
where OIDTest = @OIDTest
-- Transaktion durchführen
goto FunctionExit
end
else
begin
goto FunctionErrorWrite
end
/* SET NOCOUNT ON */
FunctionExit:
RETURN 0

FunctionErrorUnknown:
RETURN -1

FunctionErrorWrite:
RETURN -2

FunctionErrorWrongID:
RETURN -3

FunctionErrorInterimChanged:
RETURN -4

Does this help you?

Regards

Daniel Walzenbach


"Bari Allen" <ba**@sos.REMOVETHIS.state.ga.us> schrieb im Newsbeitrag
news:ON**************@tk2msftngp13.phx.gbl...
I'm trying to test for concurrency, using a SQL Stored Procedure on a
RowVersion (timestamp) Field. The vb code I'm using is as follows

Dim cmd As New SqlCommand("ConcurrencyCheck", cn)
Dim intID as integer = MyDataset.Tables("MyTable").Rows(0).Item("ID")
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("@ReturnValue", SqlDbType.Int)
cmd.Parameters(0).Direction = ParameterDirection.ReturnValue
cmd.Parameters.Add("@ID", intID)

Dim concurParam As New SqlParameter
concurParam.ParameterName = "@Concurrency"
concurParam.Value =
MyDataset.Tables("MyTable").Rows(0).Item("Concurre ncyValue")
concurParam.SqlDbType = SqlDbType.Binary
cmd.Parameters.Add(concurParam)

cn.Open()

If cmd.ExecuteScalar() > 0 Then
Record Found ...
Else
Record not found ...
End If

cmd.Dispose()
cn.Close()
cn.Dispose()

Prior to submitting the form, I run a different stored procedure which
populates the "MyDataset" from the same row using:
SELECT * FROM MyTable WHERE ID = @ID

The code in the stored procedure that submits the form is:
SELECT Count(*) FROM MyTable WHERE ID = @ID AND ConcurrencyValue =
@Concurrency

How do I convert the originally retrieved data (RowVersion) back to a true
binary in ASP.NET to send it back to SQL? It currently is storing the
field
as an array, for some reason. Thus, when I test this procedure and now
(without a doubt) no one has modified the record since, it ALWAYS returns
"record not found"

Thanks in advance for any help on this.

Bari

Nov 20 '05 #4
Thank you to everyone that replied.

When I tried to convert a value from a field in the dataset's table to byte,
I got an error, because the implicit conversion wasn't allowed from an array
(the default conversion sent back from the timestamp column).

Thus, I went with Daniel's suggestion to convert the value to a BigInt
within the retrieval stored procedure's select statement. That way, I can
store the entire recordset in a dataset, when it is returned. Then, I send
back the BigInt value to the stored procedure that tests for concurrency
(for updates). This worked great.

Thank you, Daniel!

"Bari Allen" <ba**@sos.REMOVETHIS.state.ga.us> wrote in message
news:ON**************@tk2msftngp13.phx.gbl...
I'm trying to test for concurrency, using a SQL Stored Procedure on a
RowVersion (timestamp) Field. The vb code I'm using is as follows

Dim cmd As New SqlCommand("ConcurrencyCheck", cn)
Dim intID as integer = MyDataset.Tables("MyTable").Rows(0).Item("ID")
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("@ReturnValue", SqlDbType.Int)
cmd.Parameters(0).Direction = ParameterDirection.ReturnValue
cmd.Parameters.Add("@ID", intID)

Dim concurParam As New SqlParameter
concurParam.ParameterName = "@Concurrency"
concurParam.Value =
MyDataset.Tables("MyTable").Rows(0).Item("Concurre ncyValue")
concurParam.SqlDbType = SqlDbType.Binary
cmd.Parameters.Add(concurParam)

cn.Open()

If cmd.ExecuteScalar() > 0 Then
Record Found ...
Else
Record not found ...
End If

cmd.Dispose()
cn.Close()
cn.Dispose()

Prior to submitting the form, I run a different stored procedure which
populates the "MyDataset" from the same row using:
SELECT * FROM MyTable WHERE ID = @ID

The code in the stored procedure that submits the form is:
SELECT Count(*) FROM MyTable WHERE ID = @ID AND ConcurrencyValue =
@Concurrency

How do I convert the originally retrieved data (RowVersion) back to a true
binary in ASP.NET to send it back to SQL? It currently is storing the field as an array, for some reason. Thus, when I test this procedure and now
(without a doubt) no one has modified the record since, it ALWAYS returns
"record not found"

Thanks in advance for any help on this.

Bari

Nov 20 '05 #5
Good to hear :-) You are welcome!

Cheers,

Daniel Walzenbach

"msnews.microsoft.com" <ba**@sos.REMOVETHIS.state.ga.us> schrieb im
Newsbeitrag news:%2***************@TK2MSFTNGP10.phx.gbl...
Thank you to everyone that replied.

When I tried to convert a value from a field in the dataset's table to
byte,
I got an error, because the implicit conversion wasn't allowed from an
array
(the default conversion sent back from the timestamp column).

Thus, I went with Daniel's suggestion to convert the value to a BigInt
within the retrieval stored procedure's select statement. That way, I can
store the entire recordset in a dataset, when it is returned. Then, I
send
back the BigInt value to the stored procedure that tests for concurrency
(for updates). This worked great.

Thank you, Daniel!

"Bari Allen" <ba**@sos.REMOVETHIS.state.ga.us> wrote in message
news:ON**************@tk2msftngp13.phx.gbl...
I'm trying to test for concurrency, using a SQL Stored Procedure on a
RowVersion (timestamp) Field. The vb code I'm using is as follows

Dim cmd As New SqlCommand("ConcurrencyCheck", cn)
Dim intID as integer = MyDataset.Tables("MyTable").Rows(0).Item("ID")
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("@ReturnValue", SqlDbType.Int)
cmd.Parameters(0).Direction = ParameterDirection.ReturnValue
cmd.Parameters.Add("@ID", intID)

Dim concurParam As New SqlParameter
concurParam.ParameterName = "@Concurrency"
concurParam.Value =
MyDataset.Tables("MyTable").Rows(0).Item("Concurre ncyValue")
concurParam.SqlDbType = SqlDbType.Binary
cmd.Parameters.Add(concurParam)

cn.Open()

If cmd.ExecuteScalar() > 0 Then
Record Found ...
Else
Record not found ...
End If

cmd.Dispose()
cn.Close()
cn.Dispose()

Prior to submitting the form, I run a different stored procedure which
populates the "MyDataset" from the same row using:
SELECT * FROM MyTable WHERE ID = @ID

The code in the stored procedure that submits the form is:
SELECT Count(*) FROM MyTable WHERE ID = @ID AND ConcurrencyValue =
@Concurrency

How do I convert the originally retrieved data (RowVersion) back to a
true
binary in ASP.NET to send it back to SQL? It currently is storing the

field
as an array, for some reason. Thus, when I test this procedure and now
(without a doubt) no one has modified the record since, it ALWAYS returns
"record not found"

Thanks in advance for any help on this.

Bari


Nov 20 '05 #6

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

Similar topics

2
by: Yves Touze | last post by:
Hi All, I'm trying to migrate from SQL Server 7.0 to SQL Server 2000. I've got some ASP page which call VB components that retrieve shaped recordsets from SQL Server using the MSDATASHAPE...
4
by: Charlie Williams | last post by:
I am having difficulty performing updates and deletions on an Access database using the Update() method of the OleDBDataAdapter. I can insert rows without a problem, but I get a concurrency...
2
by: xAvailx | last post by:
I have a requirement that requires detection of rows deleted/updated by other processes. My business objects call stored procedures to create, read, update, delete data in a SQL Server 2000 data...
5
by: Steve | last post by:
Visual Studio 2003 .Net / C# / SQL 2000 I am trying to work out the best way to ensure data concurrency in my application. All updates and inserts etc are done via stored procedures. When a...
7
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...
8
by: Mike Kelly | last post by:
I've chosen to implement the "optimistic concurrency" model in my application. To assist in that, I've added a ROWVERSION (TIMESTAMP) column to my main tables. I read the value of the column in my...
5
by: Bari Allen | last post by:
I'm trying to test for concurrency, using a SQL Stored Procedure on a RowVersion (timestamp) Field. The vb code I'm using is as follows Dim cmd As New SqlCommand("ConcurrencyCheck", cn) Dim...
4
by: Andrew Robinson | last post by:
I am working on a system system that requires optimistic concurrency within a web app. At first I thought this would be easy. We generate our own entities and dal/service layer but I now see that...
7
by: John | last post by:
Hi I have asked this question before but have not received any clear answer. I have a winform app with bound controls via dataadapter/dataset combination.I have a dbconcurrency exception ex. Now...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.