I have a VB .NET-program where I select a record, change the value and save the new value to the database using a ODBC-connection. The record is locked during this operation.
When I start the same application on another client, the Select-query hangs, because the record is locked. How can I set a time-out to fire an exception on this second client, so that I can show a message that the record is in use?
This is my code:
When I start the same application on another client, the Select-query hangs, because the record is locked. How can I set a time-out to fire an exception on this second client, so that I can show a message that the record is in use?
This is my code:
VB.NET:
Public Class Form1
Public gobjODBCConn As Odbc.OdbcConnection
Public gobjODBCCmd As Odbc.OdbcCommand
Public gobjODBCDataAdapter As OleDb.OleDbDataAdapter
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
gobjODBCConn = New Odbc.OdbcConnection
gobjODBCConn.ConnectionString = "uid=dba;pwd=sql;DSN=Test"
gobjODBCConn.Open()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = LoadNumber()
End Sub
Private Function LoadNumber() As String
Dim SqlLot As String
Dim strArtName As String
Dim rdr As Odbc.OdbcDataReader
SqlLot = "Select " _
& " * from Article with (XLOCK) " _
& " where ArticleId = 24"
gobjODBCCmd = gobjODBCConn.CreateCommand
gobjODBCCmd.CommandText = SqlLot
On Error GoTo QueryErr
rdr = gobjODBCCmd.ExecuteReader
rdr.Read()
strArtName = rdr("ArtDescr")
LoadNumber = strArtName
Exit Function
QueryErr:
MsgBox("Error...", MsgBoxStyle.Critical)
Exit Function
End Function
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
' Dim er As rdoError
Dim SqlLot As String
Dim RowCount As Long
Dim rdoLotList As Object
On Error GoTo UpdateError
SqlLot = "Update Article" _
& " set ArtDescr = '" & TextBox2.Text & "'" _
& " where ArticleId = 24"
gobjODBCCmd = gobjODBCConn.CreateCommand
gobjODBCCmd.CommandText = SqlLot
gobjODBCCmd.ExecuteNonQuery()
Exit Sub
UpdateFailed:
MsgBox("Update failed " + Err.Description)
Exit Sub
UpdateError:
Resume UpdateFailed
End Sub
End Class
Last edited: