BUG: You cannot open a SQLConnection in a Timer event when you debug in Visual Studio (329247)



The information in this article applies to:

  • Microsoft ADO.Net 2.0
  • Microsoft ADO.NET (included with the .NET Framework)
  • Microsoft Visual Studio 2005 Standard Edition
  • Microsoft Visual Studio 2005 Professional Edition
  • Microsoft Visual Studio .NET (2003), Professional Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2003), Academic Edition
  • Microsoft Visual Studio .NET (2002), Professional Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2002), Academic Edition

This article was previously published under Q329247

SYMPTOMS

When you debug, you receive the following error message if you step over the SqlConnection.Open function in a Timer event:
There is no source code available for the current location.

WORKAROUND

To work around this behavior, use the Timer component that the System.Timers namespace provides. The Timer component is a server-based timer. The Timer component permits you to specify a recurring interval where the Elapsed event is raised in your application. You can then handle this event to provide regular processing.

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the "Applies to" section.

MORE INFORMATION

Steps to reproduce the behavior

  1. Start Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. Click Visual Basic Projects under Project Types, and then click Windows Application under Templates.
  4. Name the project MySqlApplication, and then click OK.
  5. In the Code View window of Form1, replace the existing code with the following code:
    Public Class Form1
        Inherits System.Windows.Forms.Form
    
    #Region " Windows Form Designer generated code "
    
        Public Sub New()
            MyBase.New()
    
            'This call is required by the Windows Form Designer.
            InitializeComponent()
    
            'Add any initialization after the InitializeComponent() call
    
        End Sub
    
        'Form overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub
    
        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer
    
        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        Friend WithEvents Timer1 As System.Windows.Forms.Timer
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.components = New System.ComponentModel.Container()
            Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
            '
            'Timer1
            '
            Me.Timer1.Enabled = True
            Me.Timer1.Interval = 500
            '
            'Form1
            '
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(292, 266)
            Me.Name = "Form1"
            Me.Text = "Form1"
    
        End Sub
    
    #End Region
        Public Sub CreateSqlConnection()
            Dim myConnection As New SqlClient.SqlConnection()
    							
    							'replace the connection string with the your connection string to SQL Server
            myConnection.ConnectionString = "Data Source=myserver;Integrated Security=SSPI;Initial Catalog=northwind"
            
            ' Opens a database connection with the property settings specified by the ConnectionString.
            myConnection.Open()
          
           ' Closes the connection to the database.
            myConnection.Close()
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Timer1.Enabled = False
            CreateSqlConnection()
        End Sub
    End Class
    
  6. Add a breakpoint at CreateSqlConnection in the Timer1_Tick event procedure.
  7. Press F5 to debug the project. Program execution breaks at breakpoint.
  8. Press F11 to step into the CreateSqlConnection event procedure.
  9. Press F10 until you reach myConnection.Open().
  10. Press F10. You receive the error message that is mentioned in the "Symptoms" section.

REFERENCES

For more information about the Timer class, visit the following Microsoft Developer Network (MSDN) Web site:

Modification Type:MajorLast Reviewed:3/10/2006
Keywords:kbtshoot kberrmsg kbvs2002sp1sweep kbTimer kbbug kbDebug KB329247 kbAudDeveloper