Working with DataSource

DbEntry.Net can work together with ASP.NET well. Not only DbEntryDataSource, but also in many other ways. But if we use DbEntryDataSource, we can build web application prototype faster. And it is n-tier application already.

The following shows the basic usage of DbEntryDataSource.
  • Create a web site named WebPortal in VS 2010.
  • Add a library project named BizLayer to this solution.
  • Add a DbEntryLibrary project named Models to this solution.
  • Add a Web.config to web site and changed it as following:
<?xml version="1.0"?>
<configuration>
 <configSections>
  <section name="Leafing.Settings" type="Leafing.Util.Setting.NameValueSectionHandler, Leafing.Util"/>
 </configSections>
 
 <Leafing.Settings>
  <add key="AutoCreateTable" value="true"/>
  <add key="ObjectHandlerType" value="Reflection" />
  <add key="DataBase" value="@Access : @~App_Data\test.mdb"/>
 </Leafing.Settings>
 
 <appSettings/>
 <connectionStrings/>
 
 <system.web>
  <compilation debug="true">
   <assemblies>
   </assemblies></compilation>
  <authentication mode="Windows"/>
 </system.web>
</configuration>
  • Create an Access file named test.mdb and store it into App_Data folder. If you are using office 2007, please save it as access 2003 format.
  • Add references of Leafing.Core Leafing.Data and Leafing.Web to all 3 projects.
  • Add project reference of Models to BizLayer.
  • Add reference of System.Web to BizLayer.
  • Add project references of Models and BizLayer to WebPortal.
  • Change User class in Models project:
using Leafing.Data.Definition;
 
namespace Models
{
    public class User : DbObjectModel<User>
    {
        [Length(20)] public string Name { get; set; }
        public int Age { get; set; }
        public DateTime Birthday { get; set; }
        public bool IsMale { get; set; }
    }
}
  • Change class1 in BizLayer project as UserDataSource:
using System;
using Leafing.Web;
using Models;
 
namespace BizLayer
{
    public class UserDataSource : DbEntryDataSource<User>
    {
    }
}
  • Compile the solution.
  • If the Toolbox window doesn’t show UserDataSource control, we need add it in code. First, add following code to default.aspx just under Page tag line:
<%@ Register Assembly="BizLayer" Namespace="BizLayer" TagPrefix="biz" %>
  • Then add data source control to default.aspx:
<biz:UserDataSource ID="mydata" runat="server" />
  • Change to page design mode. Drag a GridView to the page.
  • And change the properties of the GridView as following:
Name Value
AllowPaging True
DataSourceID mydata
DataKeyNames Id
AllowSorting True
AutoGenerateDeleteButton True
AutoGenerateEditButton True
EnableViewState False
  • And change the UI style as you like.
  • Add a new class named DbInit to WebPortal:
using System;
using Leafing.Data;
using Models;
 
public static class DbInit
{
    public static void Init()
    {
        if(User.GetCount() == 0)
        {
            DbEntry.UsingTransaction(delegate()
            {
                new User{Name = "tom", Age = 19, Birthday = DateTime.Now, IsMale = true}.Save();
                new User{Name = "jerry", Age = 26, Birthday = DateTime.Now, IsMale = true}.Save();
                new User{Name = "mike", Age = 21, Birthday = DateTime.Now, IsMale = true}.Save();
                new User{Name = "rose", Age = 17, Birthday = DateTime.Now, IsMale = false}.Save();
                new User{Name = "alice", Age = 16, Birthday = DateTime.Now, IsMale = false}.Save();

                new User{Name = "peter", Age = 41, Birthday = DateTime.Now, IsMale = true}.Save();
                new User{Name = "vito", Age = 28, Birthday = DateTime.Now, IsMale = true}.Save();
                new User{Name = "jeff", Age = 23, Birthday = DateTime.Now, IsMale = true}.Save();
                new User{Name = "kate", Age = 22, Birthday = DateTime.Now, IsMale = false}.Save();
                new User{Name = "july", Age = 25, Birthday = DateTime.Now, IsMale = false}.Save();

                new User{Name = "Leafing", Age = 31, Birthday = DateTime.Now, IsMale = true}.Save();
                new User{Name = "juan", Age = 25, Birthday = DateTime.Now, IsMale = false}.Save();
            });
        }
    }
}
  • Add following code to _Default in default.aspx.cs:
public _Default()
{
    DbInit.Init();
}
  • Ensure the WebPortal is the default project and run it.
  • The GridView will show the data in test.mdb we just created.
  • And we can sort it just by clicking the column header.
  • We can delete an item just clicking the Delete link.
  • We can edit an item by clicking Edit link and Update link to save it.

Last edited Apr 9, 2014 at 3:14 PM by lifeng, version 23

Comments

No comments yet.