What should you do?

You create an application by using the Microsoft .NET Framework 3.5 and Microsoft
ADO.NET. The application uses a Microsoft SQL Server 2005 database. To open a
connection to the database, you write the following code segment. (Line numbers are
included
for reference only.)
01 private void GetOpenOrders() {
02 try {
03 //open SqlConnection and execute command.
04 }
05 catch (SqlException exp) {
06
07 }
08 }
The connection generates error messages and raises an exception. You use a ListBox control
named lstResults to display the error messages.You need to add a list item in the lstResults
control for each connection-related error message returned by the SqlConnection object.
What should you do?

You create an application by using the Microsoft .NET Framework 3.5 and Microsoft
ADO.NET. The application uses a Microsoft SQL Server 2005 database. To open a
connection to the database, you write the following code segment. (Line numbers are
included
for reference only.)
01 private void GetOpenOrders() {
02 try {
03 //open SqlConnection and execute command.
04 }
05 catch (SqlException exp) {
06
07 }
08 }
The connection generates error messages and raises an exception. You use a ListBox control
named lstResults to display the error messages.You need to add a list item in the lstResults
control for each connection-related error message returned by the SqlConnection object.
What should you do?

A.
Insert the following code segment at line 06.
foreach (SqlError error in exp.Errors)
{
lstResult.Items.Add(error.Message);
}

B.
Insert the following code segment at line 06.
string[] errors = exp.Message.Split(new char[]{‘,’});
foreach (string error in errors) {
if (error.IndexOf(“ConnectionError:”) > -1) {
lstResult.Items.Add(error);
}
}

C.
Insert the following code segment at line 06.
string[] errors = exp.StackTrace.Split(new char[]{‘,’});
foreach (string error in errors) {
if (error.IndexOf(“ConnectionError:”) > -1) {
lstResult.Items.Add(error);
}
}

D.
Insert the following code segment at line 06.
LogException(exp.Message);
Add the following method to the application.
private void LogException(Exception exp) {
if (exp.InnerException != null) {
LogException(exp.InnerException);
}
lstResult.Items.Add(exp.Message);
}

Explanation:
This class is created by the .NET Framework Data Provider for SQL Server when an error occurs. An instance of SqlError is created and managed by the SqlErrorCollection, which in turn is created by the SqlException class.

Messages with a severity level of 10 or less are informational and indicate problems caused by mistakes in information that a user has entered. Severity levels from 11 through 16 are generated by the user, and can be corrected by the user. Severity levels from 17 through 25 indicate software or hardware errors. When a level 17, 18, or 19 error occurs, you can continue working, although you might not be able to execute a particular statement.

The SqlConnection remains open when the severity level is 19 or less. When the severity level is 20 or greater, the server usually closes the SqlConnection. However, the user can reopen the connection and continue. In both cases, a SqlException is generated by the method executing the command.

For more information on errors generated by SQL Server, see SQL Server Books Online.

SqlError Class http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlerror.aspx



Leave a Reply 0

Your email address will not be published. Required fields are marked *