IF Condition in VB.net - Please Help

   Public Sub WORK1()
                'read DID from database
        Dim cmd1a As MySqlCommand = conn.CreateCommand()
        cmd1a.CommandText = "SELECT (did) FROM eliteprototype.tblewhreceive WHERE DID = '" & TextBox1.Text & "' "
        cmd1a.CommandType = CommandType.Text
        Dim count1 = cmd1a.ExecuteScalar()
        cmd1a.Clone()
        Label6.Text = count1
        'read Gcode from database
        Dim cmd2a As MySqlCommand = conn.CreateCommand()
        cmd2a.CommandText = "SELECT (gcode) FROM eliteprototype.tblewhreceive WHERE gcode = '" & TextBox2.Text & "' "
        cmd2a.CommandType = CommandType.Text
        Dim count2 = cmd2a.ExecuteScalar()
        cmd2a.Clone()
        Label8.Text = count2
        'read Quantity frm database
        'read Gcode from database
        Dim cmd3a As MySqlCommand = conn.CreateCommand()
        cmd3a.CommandText = "SELECT (inventory) FROM eliteprototype.tblewhinventory WHERE gcode = '" & TextBox2.Text & "' "
        cmd3a.CommandType = CommandType.Text
        Dim count3 = cmd3a.ExecuteScalar()
        cmd3a.Clone()
        Label7.Text = count3
      End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If TextBox2.Text = "" AndAlso TextBox1.Text = "" Then
            MsgBox("Can't proceed with blank data", vbInformation, "System says:")
        Else
            WORK1()
            TextBox1.Text = ""
            TextBox1.Select()
            TextBox2.Text = ""
            TextBox2.Enabled = False
        End If
    End Sub

Open in new window


Above is my code, I want an If condition that control the Public Sub, example:

If WORK1() is successful then
proceed
Else
msgbox(promt that the WORK1() is not succesful)
End If
Rowel VirgoVisual Studio .NETAsked:
Who is Participating?
 
ste5anSenior DeveloperCommented:
This requires a function and error handling, which is completley missing. Also you should refactor your code. Separate your logic from your UI. Also you allow SQL injection. Also what are your Clone() calls for? They seem unnecessary.

E.g. this should be sufficient:

Private Function GetScalar(ASql As String, ACondition As String) AS String

  Dim command As MySqlCommand = conn.CreateCommand()

  command.CommandText = ASql
  command.CommandType = CommandType.Text
  command.Parameters.AddWithValue "@Condition", ACondition
  GetScalar = command.ExecuteScalar()

End Function

Public Function WORK1() As Boolean

  WORK1 = False
  Try
    Label6.Text = GetScalar("SELECT did FROM eliteprototype.tblewhreceive WHERE DID = @Condition;", TextBox1.Text)
    Label8.Text = GetScalar("SELECT gcode FROM eliteprototype.tblewhreceive WHERE gcode = @Condition;", TextBox2.Text)
    Label7.Text = GetScalar("SELECT inventory FROM eliteprototype.tblewhinventory WHERE gcode = @Condition", TextBox2.Text)
    WORK1 = True
  Catch
    'optional error messages.
  End Try

End Sub

Open in new window

1
 
Éric MoreauSenior .Net ConsultantCommented:
And do you know that Work1 is successful or not? An exception?

   Public function WORK1() as boolean
try
                'read DID from database
        Dim cmd1a As MySqlCommand = conn.CreateCommand()
        cmd1a.CommandText = "SELECT (did) FROM eliteprototype.tblewhreceive WHERE DID = '" & TextBox1.Text & "' "
        cmd1a.CommandType = CommandType.Text
        Dim count1 = cmd1a.ExecuteScalar()
        cmd1a.Clone()
        Label6.Text = count1
        'read Gcode from database
        Dim cmd2a As MySqlCommand = conn.CreateCommand()
        cmd2a.CommandText = "SELECT (gcode) FROM eliteprototype.tblewhreceive WHERE gcode = '" & TextBox2.Text & "' "
        cmd2a.CommandType = CommandType.Text
        Dim count2 = cmd2a.ExecuteScalar()
        cmd2a.Clone()
        Label8.Text = count2
        'read Quantity frm database
        'read Gcode from database
        Dim cmd3a As MySqlCommand = conn.CreateCommand()
        cmd3a.CommandText = "SELECT (inventory) FROM eliteprototype.tblewhinventory WHERE gcode = '" & TextBox2.Text & "' "
        cmd3a.CommandType = CommandType.Text
        Dim count3 = cmd3a.ExecuteScalar()
        cmd3a.Clone()
        Label7.Text = count3
return true
catch ex as exception
'what do you do with the execption?
return false
end try
      End function

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If TextBox2.Text = "" AndAlso TextBox1.Text = "" Then
            MsgBox("Can't proceed with blank data", vbInformation, "System says:")
        Else
            if WORK1() then
            TextBox1.Text = ""
            TextBox1.Select()
            TextBox2.Text = ""
            TextBox2.Enabled = False
else
messagebox.show("work1 has a problem")
end if
        End If
    End Sub

Open in new window

1
 
Rowel VirgoVisual Studio .NETAuthor Commented:
I notice also the clone. First I open the connection and then close.. not clone. Thank you sir. I removed it already,
0
 
ste5anSenior DeveloperCommented:
An explicit Close() is only necessary, as long as you want to reuse the command again in the same scope, aka. the same methods body. When my function exits, the scope is left and .Net automatically closes and clean-ups the command object during garbage collection. Thus here it is not necessary.
1
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.