Databases Reference
In-Depth Information
3. Run the program by pressing Ctrl+F5, and then click the Database Exception-2
button. You'll see the message box in Figure 16-9. Click OK to close the message
box, click OK to close the next one, and then close the window.
Figure 16-9. Stored procedure database exception message
How It Works
The stored procedure tries to insert a new employee into the Employees table.
insert into employees
(
employeeid,
firstname
)
values (50, 'Cinderella')
However, since the EmployeeID column in the Employees table is an IDENTITY col-
umn, you can't explicitly assign a value to it.
n Tip Actually, you can—as the message indicates—if you use SET IDENTITY_INSERT employees OFF
in the stored procedure before you attempt the INSERT . This would allow you to insert explicit EmployeeID
values, but this seldom is, or should be, done.
When this SQL error occurs, the specific SqlException catch clause traps it and dis-
plays the information. The finally block then closes the connection.
It's possible for stored procedures to encounter several errors. You can trap and
debug these using the SqlException object, as you'll see next.
Search WWH ::




Custom Search