Asp. NET database programming Access connection failed

Source: Internet
Author: User
Tags empty ole
Access|asp.net| Programming | data | Database errors and failures invariably 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 <columns> tag part, it is deleted. The code left should look like the following:

<title> Chapter 2 TIO #4 Connection failure to Access in C # </title>
<body>
<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> (4) Check the page; When viewing products sold by Northwind, there should be no problem.

(5) The code will now be added to handle the connection problem. Go to the top of the page and go to the following script. The first example is written in C #, and the second example is written in VB. into 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 is a problem opening a connection to the"
Database. Please contact the system administrator for this site. "

Optionally set gridviewl.visible = false;

E.exceptionhandled = true;
}
}
}

</script>
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 As
SqlDataSourceStatusEventArgs)
If (not e.exception are nothing) Then
If TypeOf e.exception is System.Data.OleDb.OleDbException Then
Message. Text = "There is 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>
(6) Save and run the page. Since our actual connection is still intact, there is no problem at the moment. Closes the browser.

(7) Now move the Northwind MDB file from \app_data to the C:\Temp folder, so 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 graceful failure message.

(8) If Northwind.mdb is moved, then move it back to the C:\BegAspNetDb\ app_ Data folder.

Example shows #4--processing AccessDataSource connection failure

First, recall that the AccessDataSource control is a SqlDataSource derivative and uses a series of exceptions applied to the OLE DB data source, so don't be surprised when you reference an object with SQL or OLE DB names instead of an access name.

Observe the three modifications made on the page that handle the connection failure. First, a property of the GridView data source control is added that displays a message when the GridView does not get any data from the data source control. Second, a property of the data source control is added that invokes the Data_selected event handler when the Onselected event occurs. Please note that this is in the DataSource event. Although the user does not directly select the AccessDataSource control (nor renders it to the user), the selection occurs internally when the GridView requests data from the data source control. Third, you have written this script.

The script will receive a few parameters, one of which is an exception. There is no object named Accessdatasourcestatuseventargs. However, you can get a basic object that is internally derived from Access-datasource: SQL DataSource. The SqlDataSource object has state parameters that can be committed to the AccessDataSource control. If there is no problem, the list of exceptions is empty. If there is an exception, then the code will test to see what type of exception is thrown. Similarly, there is no object like Access exception. However, AccessDataSource puts the exception into more common objects called OLE DB exception. It is assumed that all exceptions in this collection are caused by a connection failure. Our code will respond by feedback some friendly failure notices to the label named message.

The biggest trick in this piece of code is to always have 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 OLE DB exception set is used. Many of the errors are due to the use of ASP.net 2.0 objects named access 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.