Back to Devexpress

How To: Connect XPO to a Database Server (ASP.NET WebForms)

xpo-3185-examples-how-to-connect-xpo-to-a-database-server-aspnet.md

latest3.7 KB
Original Source

How To: Connect XPO to a Database Server (ASP.NET WebForms)

  • May 05, 2022
  • 2 minutes to read

View Example: How to Create an ASP.NET Web Forms CRUD App

To access a database server using XPO, a data access layer needs to be created and provided with connection settings. To accomplish this in an ASP.NET application, place the construction code into the Application_Start event handler in the Global.asax module of your Web Site.

csharp
void Application_Start(object sender, EventArgs e) {
    // Code that runs on the application startup
    // Specify the connection string, which is used to open a database. 
    // It's supposed that you've already created the Comments database
    // within the App_Data folder.
    string conn = DevExpress.Xpo.DB.AccessConnectionProvider.GetConnectionString(
      Server.MapPath("~\\App_Data\\Comments.mdb"));
    DevExpress.Xpo.Metadata.XPDictionary dict =
    new DevExpress.Xpo.Metadata.ReflectionDictionary();
    // Initialize the XPO dictionary.
    dict.GetDataStoreSchema(typeof(Comment).Assembly);
    DevExpress.Xpo.DB.IDataStore store = DevExpress.Xpo.XpoDefault.GetConnectionProvider(conn,
        DevExpress.Xpo.DB.AutoCreateOption.SchemaAlreadyExists);
    DevExpress.Xpo.XpoDefault.DataLayer = new DevExpress.Xpo.ThreadSafeDataLayer(dict, store);
    DevExpress.Xpo.XpoDefault.Session = null;
}
vb
Sub Application_Start(sender As Object, e As EventArgs)
   ' Code that runs on the application startup
   ' Specify the connection string, which is used to open a database. 
   ' It's supposed that you've already created the Comments database within the App_Data folder.
   Dim conn = DevExpress.Xpo.DB.AccessConnectionProvider.GetConnectionString( _
     Server.MapPath("~\App_Data\Comments.mdb"))
   Dim dict As New DevExpress.Xpo.Metadata.ReflectionDictionary()
   ' Initialize the XPO dictionary.
   dict.GetDataStoreSchema(GetType(Comment).Assembly)
Dim store As DevExpress.Xpo.DB.IDataStore = DevExpress.Xpo.XpoDefault.GetConnectionProvider(conn, _
     DevExpress.Xpo.DB.AutoCreateOption.SchemaAlreadyExists)
   DevExpress.Xpo.XpoDefault.DataLayer = New DevExpress.Xpo.ThreadSafeDataLayer(dict, store)
    DevExpress.Xpo.XpoDefault.Session = Nothing
End Sub

Note

When designing ASP.NET applications, using the ThreadSafeDataLayer object is recommended.

Handle the Web form’s Page_Init event to create a session for the XpoDataSource component. The session must be linked to the Data Layer:

csharp
using DevExpress.Xpo;

Session session1;
protected void Page_Init(object sender, EventArgs e) {
    session1 = new Session();
    XpoDataSource1.Session = session1;
}
vb
Dim session1 As Session

Protected Sub Page_Init(sender As Object, e As EventArgs) Handles Me.Init
    session1 = New Session()
    XpoDataSource1.Session = session1
End Sub

When designing multi-user ASP.NET applications based on XPO, it’s recommended to create an XPO session for each request. Create a new session for each page, and dispose of it on the Page.Unload event.

csharp
protected void Page_Unload(object sender, EventArgs e) {
    xpoSession.Dispose();
}
vb
Protected Sub Page_Unload(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Unload
    xpoSession.Dispose()
End Sub