Sample code in the SqlTransaction class and SqlException class Help documentation is incorrect (320142)



The information in this article applies to:

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

SUMMARY

The "SqlTransaction Class" and the "SqlException Class" 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: These Visual Studio .NET Help topics are also available at the following Microsoft Developer Network (MSDN) Library Web sites:

MORE INFORMATION

SqlTransaction Class

The following code is incorrect in the "SqlTransaction Class" Help documentation:
Console.WriteLine("Error: {1}", e.Message)
Console.WriteLine("Error reported by {1}.", 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 "SqlTransation Class" 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("Error: {0}", e.Message)
Console.WriteLine("Error reported by {0}.", 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 "SqlException Class" 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 "SqlException Class" Visual Studio .NET Help documentation includes three problems:
  • The code issues a "Catch e As SqlException" statement that is followed by a "Dim e As SqlException" 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 "SqlException Class" Help documentation sample with the following code:
Public Sub ThrowSqlException()
   Dim myConnString As String = "user id=sa;password=;database=northwind;server=badserver"

   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("Class: {0}", f.Class)
         Console.WriteLine("Error #{0}: {1} on line {2}.", f.Number, f.Message, f.LineNumber)
         Console.WriteLine("Error reported by {0} while connected to {1}", f.Source, f.Server)
         Console.WriteLine("Neither record was written to database.")
         Console.WriteLine("Errors collection contains:")
         Dim i As Integer
         For i = 0 To myErrors.Count - 1
            Console.WriteLine("Class: {0}", myErrors(i).Class)
            Console.WriteLine("Error #{0}: {1} on line {2}.", myErrors(i).Number, myErrors(i).Message, f.LineNumber)
            Console.WriteLine("Error reported by {0} while connected to {1}", myErrors(i).Source, myErrors(i).Server)
         Next i
      Next f
   End Try
End Sub 'ThrowSqlException   
				

REFERENCES

For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:

309544 BUG: BeginTransaction() Changes Isolation Level for Subsequent Transactions

316667 BUG: SqlDataReader Does Not Propagate Deadlock Exceptions in SqlClient

310351 HOW TO: Roll Back Updates After an Error When You Are Using a DataAdapter and a DataSet in ADO.NET and Visual Studio .NET


Modification Type:MinorLast Reviewed:9/15/2005
Keywords:kbvs2002sp1sweep kbbug kbdocerr kbpending kbSqlClient kbSystemData KB320142