Link to home
Start Free TrialLog in
Avatar of Declan Basile
Declan BasileFlag for United States of America

asked on

VB.NET application not halting with an error message

In Visual Studio Professional 2015, if there's an runtime error in the form load event of a 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?
Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

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


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?
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?
"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)
                    cmd.CommandText = "Select ReturnId, [DateCode] + '-' + [SerialNo] As SN, " &
                    "WorkOrderId From Returns"
                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

Are you actually calling Returns_Load()?
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?
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.
Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks.  I use MZ tools for Access.  I didn't know it was available for