I don't have a lot of experience working with visual basic so please be gentle. I suppose to make work a program that was programmed by someone else (for many reasons that person won't keep working on it), so everything was fine until I reached this piece of code:
Function ExisteProceso02(ByVal CodigoBarra As String) As Boolean
Dim miComando As New SqlCommand
Dim miDataAdapter As New SqlDataAdapter
Dim Table As New DataTable
Dim strSQL As String
strSQL = "Select * From PCT_GuiasDespacho Where CodigoBarra = '" & CodigoBarra & "' And
PreAnalisis = 'SI'"
miComando.CommandText = strSQL
miComando.Connection = miConexion
miDataAdapter.SelectCommand = miComando
miDataAdapter.Fill(Table)
If Table.Rows.Count = 0 Then
ExisteProceso02 = False
Else
ExisteProceso02 = True
End If
miComando.Dispose()
miDataAdapter.Dispose()
Table.Dispose()
End Function
My problem is that this function is assigning itself a value and it isn't called anywhere else in the code (I use the go to definition.) but it seems to be doing something and I don't know what is it called this property so I can reach information about it.
Can someone please tell me how this works? Or at least what is it?
Thank you.
ExisteProceso02 = False
basically means "set the return value ofExisteProceso02
tofalse
". Don't worry: it isn't overwriting the function's definition or anything like that.SqlParameter
as it's vulnerable to SQL injection (i.e. it will break and reveal secret information ifCodigoBarra
contains an apostrophe).return If (Table.Rows.Count = 0, False, True)
. You just needSqlCommand
andExcuteScalar()
(if you actually need a method for this at all). TheSqlCommand
(and all the other objects - DataTable and DataAdapter - if you want to keep them) should be declared withUsing
statements, to dispose of them implicitly (no need to callDispose()
explicitly). Or use aTry/Finally
block and dispose of disposable object in theFinally
block, which is called in any case, even if exception are raised in the meanwhile.