How do I stop Visual Studio 2008 (VB.NET) from crashing when user control is added on to form designer?

Posted on 2009-02-10
Last Modified: 2013-11-27

I am using Visual Studio 2008 (VB.NET) and SQL CE.

I have a user control that crashes VS every time it is added to the form designer (I have included the code for your reference). I have had a go at debugging and it seems to be caused by the UVLevels_Load event which triggers a whole bunch of other Sub's to run and in particular the GetUVLevels Sub seems to throw an exception (The database file cannot be found. Check the path to the database. [ Data Source = C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ALERTnet.sdf ]) when opening the database connection.

I'm guessing that the connection string "Data Source=|DataDirectory|\ALERTnet.sdf" is producing the wrong file path when in the designer as the control works correctly in run time.

If I'm right, how do I fix this? If I'm wrong, what is causing the problem?


Imports System

Imports System.IO

Imports System.Net

Imports System.Text.RegularExpressions

Imports System.Data.SqlServerCe

Public Class UVLevels

    Dim conALERTnet As New SqlCeConnection("Data Source=|DataDirectory|\ALERTnet.sdf")

    Private Sub UVLevels_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim strURL As String = ""

        Me.picLoading.Visible = True

        Me.WebBrowser_UV.Navigate(New Uri(strURL))

    End Sub

    Private Sub WebBrowser_UV_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser_UV.DocumentCompleted

        Me.Timer_DocumentLoaded.Enabled = True

    End Sub

    Private Sub Timer_DocumentLoaded_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer_DocumentLoaded.Tick


        Me.Timer_DocumentLoaded.Enabled = False

    End Sub

    Private Sub GetUVLevels(ByVal strHTML As String)

        Dim RowMatch As Match

        Dim CellMatch As Match

        Dim strUVStationID As String

        Dim strUVLevel As String

        Dim strUVLastUpdated As String

        Dim cmd As SqlCeCommand = conALERTnet.CreateCommand()

        Dim cmdResult As Integer

        Dim strInput As String

        'A regex pattern that finds everything inside the html <tr> and </tr> tags (table row).

        Dim RowPattern As String = "<tr\b[^>]*>(?:(?>[^<]+)|<(?!tr\b[^>]*>))*?</tr>"

        'A regex pattern that finds everything inside the html <td> and </td> tags (table cell).

        Dim CellPattern As String = "<td\b[^>]*>(?:(?>[^<]+)|<(?!td\b[^>]*>))*?</td>"

        'Opens a connection to the ALERTnet database.


        'Gets the HTML Source Code from the body of website specified by strURL and puts it into a string.

        strInput = Me.WebBrowser_UV.Document.Body.InnerHtml.ToString

        'Gets the first table row that matches the RowPattern regex.

        RowMatch = Regex.Match(strInput, RowPattern, _

                        RegexOptions.IgnoreCase Or RegexOptions.Compiled)

        'Loops through each table row match.

        Do While RowMatch.Success

            'Gets the first table cell that matches the CellPattern regex within the current row match.

            CellMatch = Regex.Match(RowMatch.ToString, CellPattern, _

                        RegexOptions.IgnoreCase Or RegexOptions.Compiled)

            'Gets the UV Station Name from the first cell match,

            'strips the html tags by calling the stripHTML function,

            'converts the result to an upper case string

            'and stores in the strUVStationID string.

            strUVStationID = stripHTML(CellMatch.Value).ToString.ToUpper

            'Gets the next table cell that matches the CellPattern regex within the current row match.

            CellMatch = CellMatch.NextMatch()

            'Gets the UV Level from the cell match,

            'strips the html tags by calling the stripHTML function

            'and stores in the strUVLevel string.

            strUVLevel = stripHTML(CellMatch.Value)

            'Gets the next table cell that matches the CellPattern regex within the current row match.

            CellMatch = CellMatch.NextMatch()

            'Gets the last UV Level update time from the cell match,

            'strips the html tags by calling the stripHTML function

            'and stores in the strUVLastUpdated string.

            strUVLastUpdated = stripHTML(CellMatch.Value)

            cmd.CommandText = "UPDATE UVLevels SET UVLevel = '" & strUVLevel & "', UVLastUpdated = '" & Format(Now, "dd/MM/yyyy") & " " & strUVLastUpdated & "' WHERE UVStationID = '" & strUVStationID & "'"

            cmdResult = cmd.ExecuteNonQuery()

            If cmdResult = 0 Then

                cmd.CommandText = "INSERT INTO UVLevels (UVStationID, UVLevel, UVLastUpdated) VALUES ('" & strUVStationID & "','" & strUVLevel & "','" & strUVLastUpdated & "')"

                cmdResult = cmd.ExecuteNonQuery()

            End If

            RowMatch = RowMatch.NextMatch()


        Me.picLoading.Visible = False

        If conALERTnet IsNot Nothing Then



            conALERTnet = Nothing

        End If

    End Sub

    Function stripHTML(ByVal strHTML As String)

        Dim strRegEx As String = "<[^>]*>"

        Dim R As New Regex(strRegEx)

        strHTML = R.Replace(strHTML, "")

        Return strHTML

    End Function

End Class

Open in new window

Question by:Scott_Y
    LVL 41

    Expert Comment

    Maybe the associated *.Designer.vb file got corrupted (or is missing).?
    Does that file exist and does it have a line like:
        Inherits System.Windows.Forms.UserControl

    Author Comment

    The file doesn't seem to be corrupted as it works in run time just not in the form designer. I can use the designer to design the control without any problems and I can use the form designer without the control on it but, when I add the control to the form designer it crashes.

    I have tried adding the line:
    Imports System.Windows.Forms.UserControl

    and rebuilding but, it didn't seem to make any difference.

    It still seems to me to be something to do with the file path to the database. I changed this line:

    Dim conALERTnet As New SqlCeConnection("Data Source=|DataDirectory|\ALERTnet.sdf")

    Dim conALERTnet As New SqlCeConnection("Data Source=C:\Documents and Settings\user\My Documents\Visual Studio 2008\Projects\ALERTnet\ALERTnet\bin\Debug\ALERTnet.sdf")
    and it fixed the problem but, causes another one by having a hard coded connection string. The problem seems to be that "|DataDirectory|" is grabbing the wrong file path in the designer (the file path of Visual Studio - "Data Source = C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ALERTnet.sdf"  as opposed to the file path of the project) but this is not a problem in run time.
    How can I fix this?
    LVL 4

    Assisted Solution

    Is the database path hard coded in the app; and does the client also have that same file in the dame location?
    Does the client have access to that location of the OS?

    Author Comment

    No, the database path is not hard coded in the app. The database path in the app is:
    Data Source=|DataDirectory|\ALERTnet.sdf
    I changed the database path (hard coded it) for debugging purposes and this fixed the problem, but, I don't want to hard code the database path.
    Client access to the same file in the same location is possible but cannot be guaranteed. Hence, not wanting to hard code the database path.
    LVL 4

    Expert Comment

    Data Source=|DataDirectory|\ & "ALERTnet.sdf "

    Author Comment

    No luck. It didn't like the first part of the connection string. The error was:
    Comma, ')', or a valid expression continuation expected.
    LVL 4

    Expert Comment

    ok, try using the
    Environment.SpecialFolder.System namespace instead of [DataDirectory]
    Change .System to whatever you need and add on to it if required.

    Author Comment

    Sorry, I don't understand what you mean.
    LVL 4

    Expert Comment


    Environment.SpecialFolder is part of the .net framework and can be used to reference "Special" Operating system folders.  By entering a '.' after the "specialFolder" part, intelesence of Visual Studio will present to you a bunch of options indicating the special folder that you want to use e.g.. Windows, My Documents, etc....

    I have found it easier to create a variable for the data source and then use that variable with in the connections string.  I have to admit I have not used a file based connection string in years as I have been 100% using SQL Server; but the mechanics are similar.

    Dim MYConnStr$ = Environment.SpecialFolder.<ChooseWhatYouNeedHere> & "\ALERTnet.sdf"
     Dim conALERTnet As New SqlCeConnection("Data Source=" & MYConnStr$ & "")"

    Accepted Solution

    I have managed to solve it myself. The solution turned out to be blindingly obvious and I can't believe I didn't try it in the first place. I just used a simple try/catch around the data connection which trapped the error during design time and still allowed the code to execute at runtime when it had access to the actual datasource. Something I should have done in the first place. That'll teach me not to be complacent late at night.

    Thanks for your help anyway.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
    The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now