Quick Start
The following will help you get up and running quickly with CodeNGen.
This information is also contained in the User Guide.
Using Code
Environment Setup
|
Get a single customer from the database:
|
Customers customers = new Customers();
Customer customer = customers.GetByCustomerId(100);
|
|
Get a collection of customers:
|
Customers customers = new Customers();
customers.GetAll();
|
|
Update a customer:
|
Customers customers = new Customers();
Customer customer = customers.GetByCustomerId(100);
customer.FirstName = "Joey";
customer.Save();
|
|
Update a collection of customers:
|
Customers customers = new Customers();
customers.GetAll();
foreach (Customer customer in customers)
{
customer.FullName = customer.FirstName + " " + customer.LastName;
}
customers.SaveAll();
|
|
Save a new customer:
|
Customer customer = new Customer();
customer.FirstName = "Jane";
customer.LastName = "Doe";
customer.Save();
//or
Customers customers = new Customers();
customers.Add(new Customer("Joe", "Blow"));
customers.SaveAll();
|
|
Delete a customer:
|
Customers customers = new Customers();
Customer customer = customers.GetByCustomerId(100);
customer.MarkForDelete();
customer.Save();
//or
Customers customers = new Customers();
Customer customer = customers.GetByCustomerId(100);
customer.Delete();
|
|
Delete from a collection:
|
//delete a single customer
Customers customers = new Customers();
customers.GetAll();
customers[0].MarkForDelete();
customers.SaveAll();
//or
Customers customers = new Customers();
customers.GetAll();
customers[0].Delete();
//or
Customers customers = new Customers();
customers.GetAll();
customers[0].MarkForDelete();
customers[0].Save();
//delete all customers in the collection
customers.MarkAllForDelete();
customers.SaveAll();
|
|
Bind to a DataGridView (WinForms):
|
Customers customers = new Customers();
customers.GetAll();
DataGridView1.DataSource = customers;
|
|
Bind to a ComboBox (WinForms):
|
Customers customers = new Customers();
customers.GetAll();
ComboBox1.DataSource = customers;
|
|
Bind to a GridView (ASP.NET):
|
Customers customers = new Customers();
customers.GetAll();
GridView1.DataKeyNames = new string[] { "CustomerId" };
GridView1.DataSource = customers;
GridView1.DataBind();
|
|
Bind to a DropDownList (ASP.NET):
|
Customers customers = new Customers();
customers.GetAll();
DropDownList1.DataSource = customers;
DropDownList1.DataTextField = "FullName";
DropDownList1.DataValueField = "CustomerId";
DropDownList1.DataBind();
|
|
Recursive Save:
|
Customer customer = new Customer();
customer.FirstName = "Jane";
customer.LastName = "Doe";
customer.Addresses.Add(new Address("100 My Street", "Nice City", "NC", "00000-0000"));
customer.SaveRecursive();
|
|
Recursive Save on a Collection:
|
Customers customers = new Customers();
customers.GetAll();
foreach (Customer customer in customers)
{
customer.Addresses.Add(new Address("100 My Street", "Nice City", "NC", "00000-0000"));
}
customers.SaveAllRecursive();
|
|
Recursive Delete:
|
Customer customer = Customers.GetByCustomerId(100);
customer.MarkForDeleteRecursive();
//delete this customer and all addresses associated with this customer
customer.SaveRecursive();
//delete all customers recursively in the collection
Customers customers = new Customers();
customers.GetAll();
customers.MarkAllForDeleteRecursive();
customers.SaveAllRecursive();
|
|
Use a transaction: (a transaction can be passed into any save operation in this
manner)
|
Customers customers = new Customers();
customers.GetAll();
foreach (Customer customer in customers)
{
customer.FullName = customer.FirstName + ' ' + customer.LastName;
customer.Addresses.Add(new Address("100 My Street", "Nice City", "NC", "00000-0000"));
}
Product product = new Product("Laptop");
using (IDbConnection con = customers.GetConnection())
{
using (IDbTransaction tran = con.BeginTransaction())
{
try
{
customers.SaveAllRecursive(tran);
product.Save(tran);
tran.Commit();
}
catch
{
tran.Rollback();
throw; //rethrow original error
}
}
}
|
Using the CodeNGenDataSource to get and update data:
- Create an aspx page and add a GridView
- Click on the triangle at the upper right hand side of the GridView
- In the DataSource drop down select New
- Select CodeNGenDataSource and click ok
- Configure the CodeNGenDataSource using the designer
- It will output code similar to the following
|
<%@ Register Assembly="Sample.Website" Namespace="Sample.Website" TagPrefix="cc1"%>
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateColumns="False" DataSourceID="CodeNGenDataSource1"
DataKeyNames="CustomerId">
<Columns>
<asp:BoundField DataField="CustomerId"
HeaderText="CustomerId" SortExpression="CustomerId" />
<asp:BoundField DataField="Name"
HeaderText="Name" SortExpression="Name" />
</Columns>
</asp:GridView>
<cc1:CodeNGenDataSource ID="CodeNGenDataSource1" runat="server"
BusinessEntityCollectionTypeName="Sample.BusinessLayer.Customers"
BusinessEntityTypeName="Sample.BusinessLayer.Customer"
SelectMethod="GetByCustomerId">
<SelectParameters>
<asp:QueryStringParameter Name="CustomerId"
QueryStringField="ID" Type="Int32" />
</SelectParameters>
</cc1:CodeNGenDataSource>
|