Avatar of Declan Basile
Declan Basile
Flag 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 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?
Visual Basic.NETMicrosoft Visual Studio

Avatar of undefined
Last Comment
Declan Basile

8/22/2022 - Mon
Scott McDaniel (EE MVE )

Do you have error handling implemented in that section of code (i.e. a Try-Catch statement)?
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?
Scott McDaniel (EE MVE )

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?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Declan Basile

"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

Shaun Vermaak

Are you actually calling Returns_Load()?
Scott McDaniel (EE MVE )

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?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Declan Basile

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.
Scott McDaniel (EE MVE )

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Declan Basile

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