Link to home
Start Free TrialLog in
Avatar of Rowel Virgo
Rowel VirgoFlag for Philippines

asked on

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
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

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

ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rowel Virgo

ASKER

I notice also the clone. First I open the connection and then close.. not clone. Thank you sir. I removed it already,
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.