Microsoft KB Archive/329247

= BUG: You cannot open a SQLConnection in a Timer event when you debug in Visual Studio =

Article ID: 329247

Article Last Modified on 5/18/2007

-

APPLIES TO


 * Microsoft ADO.NET 2.0
 * Microsoft .NET Framework 1.1 Service Pack 1
 * 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
 * Microsoft Visual Studio .NET 2003 Enterprise Developer
 * Microsoft Visual Studio .NET 2003 Academic Edition
 * Microsoft Visual Studio .NET 2002 Professional Edition
 * Microsoft Visual Studio .NET 2002 Enterprise Architect
 * Microsoft Visual Studio .NET 2002 Enterprise Developer
 * 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 &quot;Applies to&quot; section.



Steps to reproduce the behavior
 Start Visual Studio .NET. On the File menu, point to New, and then click Project. Click Visual Basic Projects under Project Types, and then click Windows Application under Templates. Name the project MySqlApplication, and then click OK.  In the Code View window of Form1, replace the existing code with the following code: Public Class Form1 Inherits System.Windows.Forms.Form


 * 1) Region &quot; Windows Form Designer generated code &quot;

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 = &quot;Form1&quot; Me.Text = &quot;Form1&quot;

End Sub

Public Sub CreateSqlConnection Dim myConnection As New SqlClient.SqlConnection 'replace the connection string with the your connection string to SQL Server myConnection.ConnectionString = &quot;Data Source=myserver;Integrated Security=SSPI;Initial Catalog=northwind&quot; ' Opens a database connection with the property settings specified by the ConnectionString. myConnection.Open ' Closes the connection to the database. myConnection.Close End Sub
 * 1) End Region

Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick Timer1.Enabled = False CreateSqlConnection End Sub End Class </li> Add a breakpoint at CreateSqlConnection in the Timer1_Tick event procedure.</li> Press F5 to debug the project. Program execution breaks at breakpoint.</li> Press F11 to step into the CreateSqlConnection event procedure.</li> Press F10 until you reach myConnection.Open.</li> Press F10. You receive the error message that is mentioned in the &quot;Symptoms&quot; section.</li></ol>

<div class="references_section">