Access Connection for ASP. NET database programming failed

Source: Internet
Author: User

Errors and failures always occur in applications. If someone adds a new folder on the MDB directory and updates the directory tree of the server, what will happen? What if the MDB name is changed? What if the MDB file is damaged? Each of the above problems will block successful data connection and lead to a failure page. Good programming practices show that you need to be cautious with any failures as much as possible.

Before discussing the actual commands, we need to know that the AccessDataSource control is derived from the SqlDataSource control. In most cases, this is only a background issue. However, when an exception is handled, you must use an object that actually exists in the SQL data source object (named after it.

The code used by the soft landing technique will be triggered when the AccessDataSource control goes through the OnSelected event. When the GridView requests data from the data source control, this event is triggered internally. The code that handles connection errors will check the exception parameters passed from the data source control. The AccessDataSource control does not have its own name for this parameter, so you can only use the name sqlperformancestatus-EventArguments. If the exception parameter is null, nothing happens. If the exception parameter has a value, this value is checked. If the parameter is of the OLEDB Exception type, a message is displayed in the text of the warning label on the page. Remind you to pay attention to the use of terms again. If there is an AccessException type, it will be clearer, but unfortunately this type is not actually used. You can use a more common OleDbException object and end the script with a command to handle exceptions. This will allow the GridView to continue rendering, although there is no data, and prevent normal ASP. NET 2.0 failure pages 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 attribute.

Do not be pessimistic if you encounter difficulties in performing these steps. The next exercise will be used as an example. Now, you only need to cut and paste the code into the page. Later in this book, I will discuss how to create a replacement page for the GridView when the connection fails and how to handle error events in detail.

  Try it #4 -- An error occurred while processing the AccessDataSource connection

(1) In the ch02 folder, create a file named TIO-4-ConnectionFailure-CS.aspx. In the Design view, add an AccessDataSource control pointing to Northwind to the page, which selects all columns from the table.

(2) Add a GridView to display information in the data source control. Similarly, add a tag control and name it "Message ".

(3) switch to the Source view and make some changes to the tag, as shown in the highlighted code below. If there is a <columns> tag, delete it. The code left behind should be as follows:

<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 # <Form id = "forml" runat = "server">
<Asp: label ID = "Message" runat = "server"/> <br/>
<Asp: gridview id = "GridViewl" runat = "server"
Performanceid = "access0000cel"
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> (4) Check the page. When you view products sold by Northwind, there should be no problems.

(5) Now we will add code to solve the connection problem. Go to the top of the page and enter the following script. The first example is written in C #, and the second example is written in VB. Enter one of them.

<% @ Page language = "C #" %>
<Script runat = "server">

Void AccessDataSourcel_Selected (object sender, SqlDataSourceStatusEventArgs e)
{
If (e. Exception! = Null)
{
If (e. Exception. GetType () = typeof (System. Data. OleDb. OleDbException ))
{
Message. Text = "There was a problem opening a connection to
Database. Please contact the system administrator for this site .";

// Optionally set GridViewl. Visible = false;

E. ExceptionHandled = true;
}
}
}

</Script>
<Html>...
The following is a script written in VB.

<% @ Page Language = "VB" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.1 // EN"
Http://www.w3.org/TR/xhtmlll/DTD/xhtmlll.dtd>

<Script runat = "server">
Sub AccessDataSourcel_Selected (ByVal sender As Object, ByVal e
SqlDataSourceStatusEventArgs)
If (Not e. Exception Is Nothing) Then
If TypeOf e. Exception Is System. Data. OleDb. OleDbException Then
Message. Text = "There was a problem opening a connection to the database. Please contact the system administrator for this site ."

'Optionally set GridViewl. Visible = False
E. ExceptionHandled = True
End If
End If
End Sub

</Script>
<Html>...


(6) Save and run the page. Because our actual connection is still intact, there is no problem yet. Close your browser.

(7) Now, move the MDB File of Northwind from \ App_Data to the C: \ Temp folder, and the connection will fail. Alternatively, you can modify the code to try to connect to Southwind. mdb. Run the page and note that the browser will display a decent failure message.

(8) If you have moved Northwind. mdb, move it back to the C: \ BegAspNetDb \ App _ Data folder.

Example #4 -- An error occurred while processing the AccessDataSource connection

First, remember that the AccessDataSource control is a biological component of SqlDataSource and uses a series of exceptions applied to the OLEDB data source. Therefore, when an object with the SQL or OLEDB name rather than the Access name is referenced, don't be surprised.

Observe the three changes that failed to process the connection on the page. First, a property of the GridView data source control is added. A message is displayed when the GridView does not obtain any data from the data source control. Second, a property of the data source control is added, which calls the Data_Selected event handler when the OnSelected event occurs. Note that this is in the DataSource event. Although you did not directly select the AccessDataSource control (or present it to the user), the selection still occurs internally when the GridView requests data from the data source control. Third, you have written this script.

The script will receive several parameters, one of which is an exception. There is no object named AccessDataSourceStatusEventArgs. However, you can obtain an internal basic object derived from Access-DataSource: SQL DataSource. The SqlDataSource object has a status parameter that can be submitted to the AccessDataSource control. If no problem exists, the exception list is empty. If there is an exception, the code will perform a test to check the type of the thrown exception. Similarly, there are no objects like Access Exception. However, AccessDataSource puts exceptions into a more common object called OleDb Exception. It is assumed that all exceptions in this set are caused by connection failure. Our code will respond and report some friendly failure announcements to the tag named Message.

The biggest trick in this Code is to always make the object use three different names. An Access file (MDB) is used as the data source and the AccessDataSource control is used. However, the underlying SqlDataSource is used as the event parameter. Finally, a common OLEDB exception set is used. Many errors occur when an ASP. NET 2.0 object named Access is used in all cases.

 

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.