Android Question [Solved] Problem with Return True and EndTransaction

Luis Felipe

Member
Licensed User
Longtime User
Hi everybody,

It seems I've got a problem with that piece of code :
B4X:
Sub AddEntry As Boolean
    Dim Query, Query2 As String
    'add the entry
    'a NULL for the ID column increments the primary key automatically by one
    'we use ExecNonQuery2 because it's easier, we don't need to take care of the data types
   
    'Se prueba si hay valor en los campos de numero de Linea
    If edtCodigoLinea1.Text.Trim.Length <> 0 OR edtCodigoLinea2.Text.Trim.Length <> 0 OR edtCodigoLinea3.Text.Trim.Length <> 0 OR edtCodigoLinea4.Text.Trim.Length <> 0 Then   
    Query = "INSERT INTO ca_enpar VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
    Query2 = "INSERT INTO ca_enparli VALUES (?, ?, ?, ?, ?, ?)"
'    Log(Query)
'    Log(Query2)
    Log("Id:"&ID_BD_Parada)
    Log("Linea:"&edtCodigoLinea1.Text)
    Log("L15T:"&L15T)
    Log("Horario:"&HorarioL1)
    Log("Plano:"&PlanoL1)
    Log("Tarifa:"&TarifaL1)
    Log("L25T:"&L25T)
    Log("Horario2:"&HorarioL2)
    Log("Plano2:"&PlanoL2)
    Log("Tarifa2:"&TarifaL2)
    End If
   
    'Grabamos los datos de la L1
    If edtCodigoLinea1.Text.Trim.Length > 0 Then
        'Grabar los datos de la cabecera de la Parada
        Main.SQL1.BeginTransaction
        Try
        Main.SQL1.ExecNonQuery2(Query, Array As String(ID_BD_Parada, dbEmpInspector, dbCodigoInspector, dbCodigoParada, dbFecha, dbHora, edtObse.Text.Trim, Enviado))
        Main.SQL1.ExecNonQuery2(Query2, Array As String(ID_BD_Parada, edtCodigoLinea1.Text, L15T, HorarioL1, PlanoL1, TarifaL1))
        Main.SQL1.TransactionSuccessful
        Log("Parada L1 grabada")
        Main.SQL1.EndTransaction
        Return True
        Catch
        Log(LastException.Message)
        Main.SQL1.EndTransaction
        Return False
        End Try
    End If   

    'Grabamos los datos de la L2, si hay datos
    If edtCodigoLinea2.Text.Trim.Length > 0 Then
        'Grabar los datos de la cabecera de la Parada
        Main.SQL1.BeginTransaction
        Try
        Main.SQL1.ExecNonQuery2(Query, Array As String(ID_BD_Parada, dbEmpInspector, dbCodigoInspector, dbCodigoParada, dbFecha, dbHora, edtObse.Text.Trim, Enviado))
        Main.SQL1.ExecNonQuery2(Query2, Array As String(ID_BD_Parada, edtCodigoLinea2.Text, L25T, HorarioL2, PlanoL2, TarifaL2))
        Main.SQL1.TransactionSuccessful
        Log("Parada L2 grabada")
        Main.SQL1.EndTransaction
        Return True
        Catch
        Log(LastException.Message)
        Main.SQL1.EndTransaction
        Return False
        End Try
    End If   
   
    'Grabamos los datos de la L3, si hay datos
    If edtCodigoLinea3.Text.Trim.Length > 0 Then
        'Grabar los datos de la cabecera de la Parada
        Main.SQL1.BeginTransaction
        Try
        Main.SQL1.ExecNonQuery2(Query, Array As String(ID_BD_Parada, dbEmpInspector, dbCodigoInspector, dbCodigoParada, dbFecha, dbHora, edtObse.Text.Trim, Enviado))
        Main.SQL1.ExecNonQuery2(Query2, Array As String(ID_BD_Parada, edtCodigoLinea3.Text, L35T, HorarioL3, PlanoL3, TarifaL3))
        Main.SQL1.TransactionSuccessful
        Log("Parada L3 grabada")
        Main.SQL1.EndTransaction
        Return True
        Catch
        Log(LastException.Message)
        Main.SQL1.EndTransaction
        Return False
        End Try
    End If   
   
    'Grabamos los datos de la L4, si hay datos
    If edtCodigoLinea4.Text.Trim.Length > 0 Then
        'Grabar los datos de la cabecera de la Parada
        Main.SQL1.BeginTransaction
        Try
        Main.SQL1.ExecNonQuery2(Query, Array As String(ID_BD_Parada, dbEmpInspector, dbCodigoInspector, dbCodigoParada, dbFecha, dbHora, edtObse.Text.Trim, Enviado))
        Main.SQL1.ExecNonQuery2(Query2, Array As String(ID_BD_Parada, edtCodigoLinea4.Text, L45T, HorarioL4, PlanoL4, TarifaL4))
        Main.SQL1.TransactionSuccessful
        Log("Parada L4 grabada")
        Main.SQL1.EndTransaction
        Return True
        Catch
        Log(LastException.Message)
        Main.SQL1.EndTransaction
        Return False
        End Try
    End If   
End Sub

The problem is that depending on there is a value (o not) in 1 o 4 EditText, I do two inserts (that's why I use a BeginTransaction) between 1 and 4 times.

If I don't put the EndTransaction none of the 2 inserts (of each If Then statement to check if there is a value) does not write any Insert (that's logic).

And if I don't put the Return True after each possible Transaction, as my Sub function is waiting for a boolean, I won't never leave this Sub. And as I don't know if I'm going to have 1 or more transaction right after the first transaction done and the Return True read, the app exit this Sub function so if there is a second transaction it will never go through it :-(.

I don't know if it's clear but any help apreciated.
Thanx.
 

Luis Felipe

Member
Licensed User
Longtime User
B4X:
Sub AddEntry As Boolean
Dim Result As Boolean = True
Try
   
   Success = Success AND True
Catch
   Success = False
End Try

Try
   
   Success = Success AND True
Catch
   Success = False
End Try

Try
   
   Success = Success AND True
Catch
   Success = False
End Try

Return Success
End Sub

Erel I imagine that I have to write that instead :
B4X:
Dim Success As Boolean = True
right ?
I seems to be working fine.
Thank you so much for your help.
 
Upvote 0
Top