First look at the front-end code:
1 <% @ page Language = "C #" autoeventwireup = "true" codefile = "demo23.aspx. cs" inherits = "demo23" %>
2
3 <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5 <HTML xmlns = "http://www.w3.org/1999/xhtml">
6 7 <title> demonstrate how to handle SELECT statement execution errors </title>
8 9 <body>
10 <Form ID = "form1" runat = "server">
11 <div>
12 <asp: Label id = "label1" runat = "server" text = "label"> </ASP: Label>
13 <asp: gridview id = "gridview1"
14 runat = "server" performanceid = "sqlperformance1">
15 </ASP: gridview>
16 <asp: sqldatasource id = "sqlperformance1" runat = "server"
17 connectionstring = "<% $ connectionstrings: chtnorthwind %>"
18 selectcommand = "select * from [authors222]"
19 onselected = "sqldatasource1_selected"> </ASP: sqldatasource>
20 </div>
21 </form>
22 </body>
23 24
Authors222 is a non-existent table.
Let's look at the background code:
1 using system;
2 using system. collections;
3 using system. configuration;
4 using system. Data;
5 using system. LINQ;
6 using system. Web;
7 using system. Web. Security;
8 using system. Web. UI;
9 using system. Web. UI. htmlcontrols;
10 using system. Web. UI. webcontrols;
11 using system. Web. UI. webcontrols. webparts;
12 using system. xml. LINQ;
13
14 public partial class demo23: system. Web. UI. Page
15 {
16 protected void page_load (Object sender, eventargs E)
17 {
18
19}
20 protected void sqldatasourceincluselected (Object sender, sqldatasourcestatuseventargs E)
21 {
22 if (E. Exception! = NULL)
23 {
24 label1.text = E. Exception. message;
25
26 E. exceptionhandled = true; // This indicates that the exception has been handled by the developer.
27}
28}
29}
30
NOTE: If E. exceptionhandled is set to false, or if it is not set, the exception is not assigned to label1, which is the same as a common error.