Microsoft KB Archive/320142

= Sample code in the SqlTransaction class and SqlException class Help documentation is incorrect =

Article ID: 320142

Article Last Modified on 9/15/2005

-

APPLIES TO


 * 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 Q320142



SUMMARY
The &quot;SqlTransaction Class&quot; and the &quot;SqlException Class&quot; Visual Studio .NET Help documentation incorrectly demonstrates how to use the Console.WriteLine method.

If you have the Microsoft Visual Studio .NET documentation installed on your computer, these Help topics are available from the following locations:

SqlTransaction Class

ms-help://MS.MSDNVS/cpref/html/frlrfsystemdatasqlclientsqltransactionclasstopic.htm

SqlException Class

ms-help://MS.MSDNVS/cpref/html/frlrfsystemdatasqlclientsqltransactionclasstopic.htm

These Visual Studio .NET Help topics are also available at the following Microsoft Developer Network (MSDN) Library Web sites:

SqlTransaction Class

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDataSqlClientSqlTransactionClassTopic.asp

SqlException Class

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDataSqlClientSqlExceptionClassTopic.asp



SqlTransaction Class
The following code is incorrect in the &quot;SqlTransaction Class&quot; Help documentation: Console.WriteLine(&quot;Error: {1}&quot;, e.Message) Console.WriteLine(&quot;Error reported by {1}.&quot;, e.Source) When you use the Console.WriteLine method with a format string, the argument parameters should be 0-based, not 1-based. You receive the following error message when you run the code in the &quot;SqlTransation Class&quot; Help documentation:

An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll Additional information: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

To resolve this problem, replace the argument parameters with 0 to correctly reference the 0-based array as follows: Console.WriteLine(&quot;Error: {0}&quot;, e.Message) Console.WriteLine(&quot;Error reported by {0}.&quot;, e.Source)

SqlException Class
The argument parameters of Console.WriteLine should be 0-based, not 1-based. You receive the following error message when you run the code in the &quot;SqlException Class&quot; Help documentation:

An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll Additional information: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

Additionally, the second sample in the &quot;SqlException Class&quot; Visual Studio .NET Help documentation includes three problems:  The code issues a &quot;Catch e As SqlException&quot; statement that is followed by a &quot;Dim e As SqlException&quot; statement. This throws an error because the object already exists.  The errors count property is incorrectly referenced in the following line of code: For i = 0 To myErrors.Errors.Count - 1   When the code references the SqlErrorsCollection, the code incorrectly uses square brackets instead of parentheses in the following line of code: myErrors[i].Number 

To resolve this problem, replace the code in the &quot;SqlException Class&quot; Help documentation sample with the following code: Public Sub ThrowSqlException Dim myConnString As String = &quot;user id=sa;password=;database=northwind;server=badserver&quot;

Dim myConnection As New SqlConnection(myConnString) Try myConnection.Open Catch e As SqlException Dim myErrors As SqlErrorCollection = e.Errors Dim f As SqlError For Each f In myErrors Console.WriteLine(&quot;Class: {0}&quot;, f.Class) Console.WriteLine(&quot;Error #{0}: {1} on line {2}.&quot;, f.Number, f.Message, f.LineNumber) Console.WriteLine(&quot;Error reported by {0} while connected to {1}&quot;, f.Source, f.Server) Console.WriteLine(&quot;Neither record was written to database.&quot;) Console.WriteLine(&quot;Errors collection contains:&quot;) Dim i As Integer For i = 0 To myErrors.Count - 1 Console.WriteLine(&quot;Class: {0}&quot;, myErrors(i).Class) Console.WriteLine(&quot;Error #{0}: {1} on line {2}.&quot;, myErrors(i).Number, myErrors(i).Message, f.LineNumber) Console.WriteLine(&quot;Error reported by {0} while connected to {1}&quot;, myErrors(i).Source, myErrors(i).Server) Next i     Next f   End Try End Sub 'ThrowSqlException

