-1

I'm learning to update a record in a SQL Server table after checking for concurrency violation using a RowVersion column (timestamp data type). However, I'm not able to extract the existing RowVersion value to a suitable variable type. I get the following error and the relevent code is also mentioned below.

Error:

enter image description here

' UPDATE with concurrency check
Public Sub Update(user As UserModel, rowVer As Byte)

    Dim sql As String = "UPDATE TB_USERS SET " &
                    "FullName = @FullName, " &
                    "Password = @Password, " &
                    "UserType = @UserType, " &
                    "NIC = @NIC, " &
                    "Address = @Address, " &
                    "Telephone = @Telephone, " &
                    "Level = @Level, " &
                    "LoginStatus = @LoginStatus, " &
                    "Locked = @Locked " &
                    "WHERE user_Id = @UserId AND RowVersion = @RowVersion"

    Using con As New SqlConnection(connectionString)
        Using cmd As New SqlCommand(sql, con)
            cmd.Parameters.AddWithValue("@FullName", user.FullName)
            cmd.Parameters.AddWithValue("@Password", user.Password)
            cmd.Parameters.AddWithValue("@UserType", user.UserType)
            cmd.Parameters.AddWithValue("@NIC", user.NIC)
            cmd.Parameters.AddWithValue("@Address", user.Address)
            cmd.Parameters.AddWithValue("@Telephone", user.Telephone)
            cmd.Parameters.AddWithValue("@Level", user.Level)
            cmd.Parameters.AddWithValue("@LoginStatus", user.LoginStatus)
            cmd.Parameters.AddWithValue("@Locked", user.Locked)
            cmd.Parameters.AddWithValue("@UserId", user.Id)
            cmd.Parameters.AddWithValue("@RowVersion", rowVer)

            con.Open()
            Dim rowsAffected = cmd.ExecuteNonQuery()
            MsgBox(rowsAffected)
        End Using
    End Using
End Sub

Private Sub cmdUpdateUserData_Click(sender As Object, e As EventArgs) Handles cmdUpdateUserData.Click

    Dim con As New SqlConnection(connectionString)
    Dim sql As String = "SELECT user_id, RowVersion FROM TB_USERS where user_name= '" & txtNewUserName.Text & "'"
    Dim da As New SqlDataAdapter(sql, con)
    Dim dt As New DataTable()
    da.Fill(dt)

    Dim repo As New UserRepository()
    repo.Update(GetUserFromForm(), dt.Rows(0)("Rowversion"))

End Sub
2

1 Answer 1

6

The error message says that you're trying to perform an invalid conversion from type Byte(), i.e. Byte array, to type Byte. Given that your rowVer parameter is type Byte, that suggests that dt.Rows(0)("RowVersion") returns a value that is type Byte(). In that case, either fix the type of that column or the type of that parameter.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.