Once a new project has been created, we need to define a persistent object class. To do this, the new class is added to the project. Make it the Customer class.
The FirstName, LastName and Company properties of this class represent the values of the corresponding fields in a database table.
Please note that the property implementations in this sample don’t have all code in their setters that we recommend. We have shortened the sample code for brevity and simplicity. Follow our tutorials to learn the recommended way of implementing property setters.
1 2 3 4 5 6 7 8 |
<span class="cr-cs-keyword">using</span> DevExpress.Xpo; <span class="cr-cs-keyword">public</span> <span class="cr-cs-keyword">class</span> Customer : XPObject { <span class="cr-cs-keyword">public</span> Customer(Session session) : <span class="cr-cs-keyword">base</span>(session) {} <span class="cr-cs-keyword">public</span> <span class="cr-cs-keyword">string</span> FirstName; <span class="cr-cs-keyword">public</span> <span class="cr-cs-keyword">string</span> LastName; <span class="cr-cs-keyword">public</span> <span class="cr-cs-keyword">string</span> Company; } |
Connect to a Database Server
Connect XPO to a database server. To do this, create an IDataLayer object. The code which creates the data layer must be placed inside the Application_Start event handler in the Global.asax module of your Web Site. For more information, see Connecting XPO to a Database Server (ASP.NET).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<span class="cr-cs-keyword">void</span> Application_Start(<span class="cr-cs-keyword">object</span> sender, EventArgs e) { <span class="cr-cs-comment">// Code that runs on the application startup</span> <span class="cr-cs-comment">// Specify the connection string, which is used to open a database. </span> <span class="cr-cs-comment">// It's supposed that you've already created the Comments database within the App_Data folder.</span> <span class="cr-cs-keyword">string</span> conn = DevExpress.Xpo.DB.AccessConnectionProvider.GetConnectionString( Server.MapPath(<span class="cr-cs-string">"~\\App_Data\\Customer.mdb"</span>)); DevExpress.Xpo.Metadata.XPDictionary dict = <span class="cr-cs-keyword">new</span> DevExpress.Xpo.Metadata.ReflectionDictionary(); <span class="cr-cs-comment">// Initialize the XPO dictionary.</span> dict.GetDataStoreSchema(<span class="cr-cs-keyword">typeof</span>(Customer).Assembly); DevExpress.Xpo.XpoDefault.Session = <span class="cr-cs-keyword">null</span>; DevExpress.Xpo.DB.IDataStore store = DevExpress.Xpo.XpoDefault.GetConnectionProvider(conn, DevExpress.Xpo.DB.AutoCreateOption.DatabaseAndSchema); DevExpress.Xpo.XpoDefault.DataLayer = <span class="cr-cs-keyword">new</span> DevExpress.Xpo.ThreadSafeDataLayer(dict, store); } |
Retrieve Data Using the XpoDataSource Component
Persistent objects are retrieved from a database using the XpoDataSource component. After we have declared the Customer class, it’s necessary to rebuild our solution, drag the XpoDataSource component from the Toolbox and drop it onto the page. Then, assign our persistent class to the TypeName property as shown below:
Link the XpoDataSource component to a session:
1 2 3 4 5 6 |
Session session1; <span class="cr-cs-keyword">protected</span> <span class="cr-cs-keyword">void</span> Page_Init(<span class="cr-cs-keyword">object</span> sender, EventArgs e) { session1 = <span class="cr-cs-keyword">new</span> Session(); <span class="cr-cs-comment">// ...</span> XpoDataSource1.Session = session1; } |
To display data, you need to assign the XpoDataSource component to a control used to represent data. In this example, we use our ASPxGridView component. The image below shows how to bind the XpoDataSource to the grid.
After the XpoDataSource has been bound to the grid, the grid automatically generates data columns for all persistent properties (fields in a table). Enable data editing as shown below:
Finally, add records to the Customer table and run the project.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
Session session1; <span class="cr-cs-keyword">protected</span> <span class="cr-cs-keyword">void</span> Page_Load(<span class="cr-cs-keyword">object</span> sender, EventArgs e) { session1 = <span class="cr-cs-keyword">new</span> Session(); <span class="cr-cs-keyword">if</span> (session1.FindObject<Customer>(<span class="cr-cs-keyword">null</span>) == <span class="cr-cs-keyword">null</span>) { Customer cstm = <span class="cr-cs-keyword">new</span> Customer(session1); cstm.FirstName = <span class="cr-cs-string">"John"</span>; cstm.LastName = <span class="cr-cs-string">"Doe"</span>; cstm.Company = <span class="cr-cs-string">"Doe Enterprises"</span>; cstm.Save(); cstm = <span class="cr-cs-keyword">new</span> Customer(session1); cstm.FirstName = <span class="cr-cs-string">"Sam"</span>; cstm.LastName = <span class="cr-cs-string">"Hill"</span>; cstm.Company = <span class="cr-cs-string">"Hill Corporation"</span>; cstm.Save(); } XpoDataSource1.Session = session1; } |
The animation below shows the result.
A user can view and edit data in the grid. You don’t have to write any code to save data: all changes are automatically persisted with the next post-back to the server.
No #1 Recommended ASP.NET Core Hosting
ASPHostPortal.com
ASPHostPortal.com is the leading provider of Windows hosting and affordable ASP.NET MVC Hosting. ASPHostPortal proudly working to help grow the backbone of the Internet, the millions of individuals, families, micro-businesses, small business, and fledgling online businesses. ASPHostPortal has ability to support the latest Microsoft and ASP.NET technology, such as: WebMatrix, WebDeploy, Visual Studio 2015, .NET 5/ASP.NET 4.5.2, ASP.NET MVC 6.0/5.2, Silverlight 6 and Visual Studio Lightswitch, ASPHostPortal guarantees the highest quality product, top security, and unshakeable reliability, carefully chose high-quality servers, networking, and infrastructure equipment to ensure the utmost reliability.
HostForLIFE.eu
HostForLIFE.eu guarantees 99.9% uptime for their professional ASP.NET MVC hosting and actually implements the guarantee in practice. HostForLIFE.eu is the service are excellent and the features of the web hosting plan are even greater than many hosting. HostForLIFE.eu offer IT professionals more advanced features and the latest technology. Relibility, Stability and Performance of servers remain and TOP priority. Even basic service plans are equipped with standard service level agreements for 99.99% uptime. Advanced options raise the bar to 99.99%. HostForLIFE.eu revolutionized hosting with Plesk Control Panel, a Web-based interface that provides customers with 24×7 access to their server and site configuration tools.
DiscountService.com.au
DiscountService.com.au is The Best and Cheap ASP.NET MVC Hosting. DiscountService.com.au was established to cater to an under served market in the hosting industry web hosting for customers who want excellent service. DiscountService.com.au guarantees the highest quality product, top security, and unshakeable reliability, carefully chose high-quality servers, networking, and infrastructure equipment to ensure the utmost reliability. DiscountService.com.au has ability to support the latest Microsoft and ASP.NET technology, such as: WebMatrix, WebDeploy, Visual Studio 2015, .NET 5/ASP.NET 4.5.2, ASP.NET MVC 6.0/5.2, Silverlight 6 and Visual Studio Lightswitch. DiscountService.com.au is devoted to offering the best Windows hosting solution for you.