Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
 DataFile Property
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
AccessDataSource..::.DataFile Property

Updated: November 2007

Gets or sets the location of the Microsoft Access .mdb file.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

Visual Basic (Declaration)
Public Property DataFile As String
Visual Basic (Usage)
Dim instance As AccessDataSource
Dim value As String

value = instance.DataFile

instance.DataFile = value
C#
public string DataFile { get; set; }
Visual C++
public:
property String^ DataFile {
    String^ get ();
    void set (String^ value);
}
J#
/** @property */
public String get_DataFile()
/** @property */
public  void set_DataFile(String value)
JScript
public function get DataFile () : String
public function set DataFile (value : String)
ASP.NET
<asp:AccessDataSource DataFile="String" />

Property Value

Type: System..::.String

The location of the Access .mdb file. Absolute, relative, and virtual paths are supported.

ExceptionCondition
ArgumentException

An invalid path was given.

The DataFile property is a virtual, absolute, or UNC directory path to the Access .mdb file that the AccessDataSource control represents. If only the name of the file is entered, this indicates that the .mdb file is found in the same directory as the currently executing Web Forms page or code. Relative paths with both forward and backward slashes are supported. For example, "./test/test/Northwind.mdb" maps to the same path as ".\test\test\Northwind.mdb" and "test/test/Northwind.mdb". UNC paths, such as "\\mymachine\somedatadirectory\Northwind.mdb", are also supported. Although absolute physical paths are supported, you should avoid using them because they can complicate deployment.

Configuring Permissions for an Access Database

An important aspect of working with an Access .mdb file is to configure permissions properly. When a Web application uses an Access database, the application must have Read permission to the .mdb file so that it can access the data. In addition, the application must have Write permission to the folder containing the .mdb file. Write permission is required because Access creates an additional file with the extension .ldb in which it maintains information about database locks for concurrent users. The .ldb file is created at run time.

By default, ASP.NET Web applications run in the context of a local machine account called ASPNET (for Microsoft Windows 2000 and Microsoft Windows XP), or in the context of the NETWORK SERVICE account (for Microsoft Windows Server 2003). For example, for Windows 2000 or Windows XP, if the Web server is named MyServer, ASP.NET applications on the MyServer computer run in the context of the local account MyServer\ASPNET.

Therefore, to use an Access database in an ASP.NET Web application, you must configure the folder containing the Access database to have both Read and Write permissions.

When you create a Web site in the Microsoft Visual Web Developer Web development tool, Visual Web Developer creates a folder named App_Data below the current root folder. The folder is designed to be a store for application data, including Access databases. The App_Data folder is also used by ASP.NET to store databases that the system maintains, such as the database for membership and roles. When Visual Web Developer creates the App_Data folder, it grants Read and Write permissions for the folder to the ASPNET or NETWORK SERVICE user account.

Note:

As a security measure, Visual Web Developer also configures the App_Data folder so that files in the folder are not served by the Web server. Do not store any Web pages in the App_Data folder, because users will see an error if they request a page from that folder.

This section contains three code examples. The first code example demonstrates how to set the DataFile property to a Northwind.mdb file that resides in the same directory as the Web Forms page. The second code example demonstrates how to set the DataFile property to the virtual path of a Northwind.mdb file that resides in a directory named Database, which is beneath the directory that the Web Forms page is in. The third code example demonstrates how to set the DataFile property to a UNC path of a Northwind.mdb file that is available on a UNC share.

The following code example demonstrates how to set the DataFile property to a Northwind.mdb file that resides in the same directory as the Web Forms page.

Visual Basic
<%@Page  Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html  >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <asp:AccessDataSource
        id="AccessDataSource1"
        runat="server"
        DataSourceMode="DataSet"
        DataFile="~/App_Data/Northwind.mdb"
        SelectCommand="SELECT FirstName, LastName, Title FROM Employees">
      </asp:AccessDataSource>

      <asp:GridView
        id="GridView1"
        runat="server"
        AllowSorting="True"
        DataSourceID="AccessDataSource1">
      </asp:GridView>

    </form>
  </body>
</html>

C#
<%@Page  Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html  >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <asp:AccessDataSource
        id="AccessDataSource1"
        runat="server"
        DataSourceMode="DataSet"
        DataFile="~/App_Data/Northwind.mdb"
        SelectCommand="SELECT FirstName, LastName, Title FROM Employees">
      </asp:AccessDataSource>

      <asp:GridView
        id="GridView1"
        runat="server"
        AllowSorting="True"
        DataSourceID="AccessDataSource1">
      </asp:GridView>

    </form>
  </body>
</html>

J#
<%@Page  Language="VJ#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html  >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <asp:AccessDataSource
        id="AccessDataSource1"
        runat="server"
        DataSourceMode="DataSet"
        DataFile="~/App_Data/Northwind.mdb"
        SelectCommand="SELECT FirstName, LastName, Title FROM Employees">
      </asp:AccessDataSource>

      <asp:GridView
        id="GridView1"
        runat="server"
        AllowSorting="True"
        DataSourceID="AccessDataSource1">
      </asp:GridView>

    </form>
  </body>
</html>

The following code example demonstrates how to set the DataFile property to the virtual path of a Northwind.mdb file that resides in a directory named Database, which is beneath the directory that the Web Forms page is in.

Visual Basic
<%@Page  Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html  >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <asp:AccessDataSource
        id="AccessDataSource1"
        runat="server"
        DataSourceMode="DataReader"
        DataFile="database/Northwind.mdb"
        SelectCommand="SELECT FirstName, LastName, Title FROM Employees">
      </asp:AccessDataSource>

      <asp:GridView
        id="GridView1"
        runat="server"
        DataSourceID="AccessDataSource1">
      </asp:GridView>

    </form>
  </body>
</html>

C#
<%@Page  Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html  >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <asp:AccessDataSource
        id="AccessDataSource1"
        runat="server"
        DataSourceMode="DataReader"
        DataFile="database/Northwind.mdb"
        SelectCommand="SELECT FirstName, LastName, Title FROM Employees">
      </asp:AccessDataSource>

      <asp:GridView
        id="GridView1"
        runat="server"
        DataSourceID="AccessDataSource1">
      </asp:GridView>

    </form>
  </body>
</html>

J#
<%@Page  Language="VJ#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html  >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <asp:AccessDataSource
        id="AccessDataSource1"
        runat="server"
        DataSourceMode="DataReader"
        DataFile="database/Northwind.mdb"
        SelectCommand="SELECT FirstName, LastName, Title FROM Employees">
      </asp:AccessDataSource>

      <asp:GridView
        id="GridView1"
        runat="server"
        DataSourceID="AccessDataSource1">
      </asp:GridView>

    </form>
  </body>
</html>

The following code example demonstrates how to set the DataFile property to a UNC path of a Northwind.mdb file that is available on a UNC share.

Visual Basic
<%@Page  Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html  >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <asp:AccessDataSource
        id="AccessDataSource1"
        runat="server"
        DataSourceMode="DataReader"
        DataFile="\\uncpath\Northwind.mdb"
        SelectCommand="SELECT FirstName, LastName, Title FROM Employees">
      </asp:AccessDataSource>

      <asp:GridView
        id="GridView1"
        runat="server"
        DataSourceID="AccessDataSource1">
      </asp:GridView>

    </form>
  </body>
</html>

C#
<%@Page  Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html  >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <asp:AccessDataSource
        id="AccessDataSource1"
        runat="server"
        DataSourceMode="DataReader"
        DataFile="\\uncpath\Northwind.mdb"
        SelectCommand="SELECT FirstName, LastName, Title FROM Employees">
      </asp:AccessDataSource>

      <asp:GridView
        id="GridView1"
        runat="server"
        DataSourceID="AccessDataSource1">
      </asp:GridView>

    </form>
  </body>
</html>

J#
<%@Page  Language="VJ#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html  >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <asp:AccessDataSource
        id="AccessDataSource1"
        runat="server"
        DataSourceMode="DataReader"
        DataFile="\\uncpath\Northwind.mdb"
        SelectCommand="SELECT FirstName, LastName, Title FROM Employees">
      </asp:AccessDataSource>

      <asp:GridView
        id="GridView1"
        runat="server"
        DataSourceID="AccessDataSource1">
      </asp:GridView>

    </form>
  </body>
</html>

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker