This walkthrough shows you how to identity upon successive inserts by creating
a new insert methods.
During this walkthrough, you will learn how to do the following:
- Binding ISDataSource to a WebGrid
- Create updatable Hierarchical WebGrid by returning new identity
In order to complete this walkthrough, you will need the following:
- Access to the Microsoft Access Northwind database.
- Visual Studio 2005/2008/2010 Application.
To create new web application and bind WebGrid to ObjectDataSource
- Bind
WebGrid to Hierarchical ISDataSource control.
- Right click WebGrid instance and choose WebGrid Designer.
- Enable the Editing – Batch Update features in WebGrid Component
Designer.
- Create a DoInsert() method for each table which is used in the
WebGrid and SelectIdentityQuery() function to retrieve the newly
created ID, for example:
[DataObjectMethodAttribute(DataObjectMethodType.Insert, true)]
public int DoInsert(ref Nullable<int> OrderID, string CustomerID, Nullable<int> EmployeeID, Nullable<system.datetime> OrderDate,
Nullable<system.datetime> RequiredDate, Nullable<system.datetime> ShippedDate, Nullable<int> ShipVia, Nullable<decimal> Freight, string ShipName,
string ShipAddress, string ShipCity, string ShipRegion, string ShipPostalCode, string ShipCountry)
{
this.Connection.Open(); // important
int affectedRows = this.Insert(CustomerID, EmployeeID, OrderDate, RequiredDate, ShippedDate, ShipVia,
Freight, ShipName, ShipAddress, ShipCity, ShipRegion, ShipPostalCode, ShipCountry);
int? identity = this.SelectIdentityQuery();
OrderID = identity; // pass the new identity into OrderID param
this.Connection.Close();
return affectedRows;
}
public int SelectIdentityQuery()
{
OleDbCommand cmd = new OleDbCommand("SELECT @@IDENTITY", this.Connection);
int newID = (int)cmd.ExecuteScalar();
return newID;
}
|
- Update the parameters in insert function in the ISDataSource for the ID parameter,
set the Direction property to InputOutput. This
parameter is required to reflect the parameter signature of DoInsert method.
- Click OK.
- You are all set. Run the page in browser.