VB.NET application not halting with an error message

Declan_Basile
Declan_Basile used Ask the Experts™
on
In Visual Studio Professional 2015, if there's an runtime error in the form load event of a vb.net application, the form continues to open without running the rest of the code, and I don't see a report of any error in either the immediate window or the error list window.  I tested this by purposefully setting a connection object with an incorrect connection string.  How can I get the program to halt and give me an error message instead of skipping the rest of the code and opening the form?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2014

Commented:
Do you have error handling implemented in that section of code (i.e. a Try-Catch statement)?

Author

Commented:
No.  Not yet.  I was hoping to get it to stop execution and give me an error message  whenever there's an exception and there's no error handling code,  like what happens when I'm programming in VBA.  Is there a way to configure it to work that way?
Most Valuable Expert 2012
Top Expert 2014

Commented:
Generally speaking you should get an error message, depending on the severity of the error. I don't know if there's any configuration needed - at least, I've never really had to configure it. It's just worked that way (but I don't always get an error, even when I think I should).

Are you working in DEBUG configuration?

Can you show the code that you're expecting to error out?
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Author

Commented:
"Debug" is selected from the drop down that has "Release" and "Configuration Manager" as other options.  I'm just clicking "Start" to run the program.  I purposely made the connection string "strConn" incorrect, and the program didn't execute the rest of the code but still opened the form and didn't give me an error.  Also, I unintentionally enclosed the rowfilter value in single quotes instead of double quotes (vwRt.RowFilter = 'WorkOrderId = 52073') and again the program didn't execute the rest of the lines of code beyond that but opened the form without an error message.

    Private Sub Returns_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim dtWO As New DataTable
        Dim dtRt As New DataTable

        Using cn As New SqlConnection(strConn)
            Using cmd As New SqlCommand("Select WorkOrderId, [Number] FROM WorkOrders Where " &
            "[Number] like '%?' or [Number] like '%R' Order By NumberSort DESC", cn)
                Using da As New SqlDataAdapter(cmd)
                    cn.Open()
                    da.Fill(dtWO)
                    cmd.CommandText = "Select ReturnId, [DateCode] + '-' + [SerialNo] As SN, " &
                    "WorkOrderId From Returns"
                    da.Fill(dtRt)
                End Using
            End Using
        End Using
        cbxWorkOrderId.ValueMember = "WorkOrderId"
        cbxWorkOrderId.DisplayMember = "Number"
        cbxWorkOrderId.DataSource = dtWO
        cbxWorkOrderId.SelectedIndex = -1

        cbxReturnId.ValueMember = "cbxReturnId"
        cbxReturnId.DisplayMember = "SN"
        vwRt = New DataView(dtRt)
        vwRt.RowFilter = "WorkOrderId = 52073"
        cbxReturnId.DataSource = vwRt
        cbxReturnId.SelectedIndex = -1

    End Sub

Open in new window

Shaun VermaakTechnical Specialist
Awarded 2017
Distinguished Expert 2018

Commented:
Are you actually calling Returns_Load()?
Most Valuable Expert 2012
Top Expert 2014

Commented:
If you put a breakpoint in, does the debugger stop at the breakpoint?

If a connection string is invalid, then the debugger should throw an error when the connection is opened. If that's not happening, then I'd suspect something wrong with your installation of VS (or Windows).

I'd be curious what would happen if you implement a Try - Catch. Does the error handler also miss that error?

Author

Commented:
Sorry for the late response.  I was busy with another post for another problem and I didn't realize you had responded.  When I put in a breakpoint the program hangs for about 30 seconds trying to open a connection to an instance of SQL Server that doesn't exist, then skips the rest of the lines of code and opens the form without throwing any errors.  Using try/catch, however, does throw an exception.
Most Valuable Expert 2012
Top Expert 2014
Commented:
I'm not sure why it would do that - not throw an error when it can't open a connection - but the simple fix is to add error handling. I use MZTools, which can add your custom error handler easily.

Author

Commented:
Thanks.  I use MZ tools for Access.  I didn't know it was available for VB.net.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial