Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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
Medium Priority
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 = "http://www.arpansa.gov.au/uvindex/index.cfm"
        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
  • 5
  • 4
LVL 41

Expert Comment

ID: 23613767
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

ID: 23618883
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?

Assisted Solution

Thunder724 earned 200 total points
ID: 23622818
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?
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.


Author Comment

ID: 23639630
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.

Expert Comment

ID: 23640328
Data Source=|DataDirectory|\ & "ALERTnet.sdf "

Author Comment

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

Expert Comment

ID: 23640430
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

ID: 23640474
Sorry, I don't understand what you mean.

Expert Comment

ID: 23644658

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

Scott_Y earned 0 total points
ID: 24035334
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 is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

578 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