Asp. NET database programming Access connection failed

Source: Internet
Author: User

Errors and failures inevitably occur in the application. What happens if someone adds a new level of folder to the MDB directory and "updates" The server's directory tree? What happens if the MDB name is changed? What if the MDB file is corrupted? Each of these issues will prevent a successful connection to the data and result in a failed page. Good programming practices suggest that any failure should be treated as carefully as possible.

Before discussing the actual command, we need to understand that the AccessDataSource control is derived from the SqlDataSource Control. In most cases, this is only a background issue. However, when handling exceptions, you must use objects that actually exist in (and thus named) SQL data source objects.

The technology used for soft landing will be triggered when the AccessDataSource control experiences the onselected event. When the GridView requests data from a data source control, this event is triggered internally. The code that handles the connection error checks the exception parameters passed over from the data source control. The AccessDataSource control does not have its own name for this parameter, so only the name sqldatasourcestatus-eventarguments can be used. If the exception parameter is empty, it means nothing happens. If the exception parameter has a value, the value is checked. If the parameter is an OLE DB exception type, the text of the warning label on the page will be prompted. Remind you again of the use of the term. If there is a accessexception type, it will be clearer, but in practice there is no such type. You can use more mundane OleDbException objects and end scripts with commands that handle exceptions. This will allow the GridView to continue rendering, although there is no data and to prevent the regular ASP.net 2.0 failure page with a light brown background. Because the GridView does not get any data, it will display a replacement table with only one cell that displays the message in its Emptydatatext property.

If you're having trouble making these steps, don't be pessimistic; the next exercise will be a demonstration. Now, just cut and paste the code into the page. Later in this book, I will discuss how to create a replacement page for the GridView in the case of a failed connection and the details of handling the error event.

Try #4--processing AccessDataSource Connection failed

(1) In the Ch02 folder, create a file named Tio-4-connectionfailure-cs.aspx. In Design view, add a AccessDataSource control to the page that points to Northwind, and the control selects all columns from the table.

(2) Add the GridView to display the information in the data source control. Also, add a Label control and name it "message."

(3) Now switch to Source view and make some changes to the markup, as shown in the following highlighting code. If there is a tag part, it is deleted. The code left should look like the following:

<html>
<head id="Headl" runat="server">
<title>Chapter 2 TIO #4 Connection Failure to Access in C#</title>
</head>
<body>
<h3>Chapter 2 TIO #4 Connection Failure to Access in C#</h3>
<form id="forml" runat="server">
<asp:label ID="Message" runat="server"/><br/><br/>
<asp:gridview id="GridViewl" runat="server"
datasourceid="AccessDataSourcel"
AutoGenerateColumns="true"
EmptyDataText="No data records were returned" />
<asp:AccessDataSource ID="AccessDataSourcel" Runat="server"
selectcommand="Select * From Products"
datafile="~/App_Data/Northwind.mdb"
OnSelected="AccessDataSourcel_Selected"
/>
</form>
</body></html>

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.