-1

I have done data validation.So now when proper data is not entered it will show an message box showing error but also it shows the following message box where i want to block or hide the message box from popping up. Here is my code below. Please show me my error and give me a solution. Thanks

    Dim digit As Integer
    Dim text As String
    Try
        digit = CInt(txtsid.Text) & CInt(txtsph.Text)
        Text = CStr(txtsfn.Text) & CStr(txtsln.Text) & CStr(txtsem.Text) & CStr(txtint.Text)
    Catch ex As Exception
        MessageBox.Show("Please Type In A Proper Information")
    End Try
    Dim result As Integer = MessageBox.Show("Are you sure you want to proceed?", "Proceed", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
    If result = DialogResult.Yes Then
        cmdInsert.CommandText = "Insert into student Values(" + txtsid.Text + ",'" + txtint.Text + "','" + txtsfn.Text + "','" + txtsln.Text + "', '" + cmbgen.Text + "', " + txtsph.Text + ", '" + txtsem.Text + "');"
        cmdInsert.CommandType = CommandType.Text
        cmdInsert.Connection = cnnOLEDB
        cmdInsert.ExecuteNonQuery()
        UserHomepage.Show()
        Me.Hide()
    ElseIf result = DialogResult.No Then
        Me.Show()
        UserHomepage.Hide()
    End If
End Sub
  • 1
    https://stackoverflow.com/q/332365/8024897 – ZiggZagg Jun 23 '18 at 11:41
  • 1
    It's considered bad practice to use Try..Catch instead of performing proper validation. Exception handling should only be use to catch problems that are completely unexpected. Data input is not one of those situations. You need to manually check the data yourself. It makes for much more maintainable and understandable code in the future. – David Wilson Jun 23 '18 at 17:40

1 Answers1

0

Simply Return or Exit Sub to exit the Sub:

Try
    digit = CInt(txtsid.Text) & CInt(txtsph.Text)
    Text = CStr(txtsfn.Text) & CStr(txtsln.Text) & CStr(txtsem.Text) & CStr(txtint.Text)
Catch ex As Exception
    MessageBox.Show("Please Type In A Proper Information")
    Return ' --- here
End Try

That would end execution of the whole Sub (function, method, whatever you want to call it in VB) and return control back to the caller.


Note: There's a better way to do what you're trying to do. One should not use exceptions for logic flow. And parsing integers is logic flow. Instead of using CInt() and checking for exceptions, take a look at Int32.TryParse(). For example:

Dim sid As Int32
Dim result As Boolean = Int32.TryParse(txtsid.Text, sid)
If Not result
    ' show error
End If

It may end up being more code, but that's not inherently a bad thing. It's still better code.

And I don't think you need to call CStr() on things which are already strings...


Also Note: Your code is wide open to something called SQL Injection. Take a look at how (and why) to create Parameterized Queries (in other environments often called Prepared Statements). Basically, whereas you currently execute user input as code, you should instead treat user input only as values.

David
  • 208,112
  • 36
  • 198
  • 279
  • Hey, What do u mean by the Return statement, is it return to the current form? if it is how to use the return statement to the current form? – ganeshwaran Jun 23 '18 at 12:30
  • @ganeshwaran: `Return` (much like `Exit Sub`) will exit the function on that line. If you have a `Function` which returns something, you'd return that value on that line. A `Sub` doesn't return anything, so just use the `Return` statement by itself to exit it. Take a look here as well: https://stackoverflow.com/questions/1004657/syntax-exit-sub-or-return-in-vb-net-subroutines – David Jun 23 '18 at 12:47