Business Logic> data layer> sqldatasource> handle SQL command execution errors

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.