Intersoft WebGrid Documentation
Walkthrough: Creating Updatable WebGrid using ObjectDataSource
See Also Send comments on this topic.

Glossary Item Box

This walkthrough shows you how to create updatable WebGrid using ObjectDataSource in BatchUpdate mode (Insert Identity / AutoIncrement ID Scenario).

During this walkthrough, you will learn how to do the following:

 Prerequisites

In order to complete this walkthrough, you will need the following:

  • Access to the Microsoft Access Northwind database.
  • Visual Studio 2008 Application.

 Step-By-Step Instructions

To create new web application and bind WebGrid to ObjectDataSource

  1. Launch Visual Studio.NET 2008.
  2. Click on File menu, then select New and click Web Site.
  3. Select ASP.NET Web Site in the Template box and set Location to HTTP.
  4. Named the Web Site and click OK.
  5. Right-click on Project's name and select Add New Item.
  6. Select Intersoft AppForm in the My Templates box and named it as Walkthrough.aspx.
  7. Drag WebGrid instance from ToolBar to WebForm.
  8. In the Solution Explorer, right-click on App_Data and select Add Existing Item.
  9. Browse and add NorthWind.mdb in C:\Program Files\Intersoft Solutions\Data (Default installation folder).
  10. Create DataSet (dsNorthWind) in App_code and go to Server explorer.



  11. Choose Northwind and drag and drops all tables into dataset.



  12. Create a new class object (dsNorthwind_Extended.cs) in App_code.



  13. Refer the namespace to dsNorthwindTableAdapters and add methods to select identity. Here are the following details.
    C# Copy ImageCopy Code
    using System.Data.OleDb;namespace dsNorthwindTableAdapters
    {  
       public partial class OrdersTableAdapter : System.ComponentModel.Component
       {
       [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, true)]      
          public int DoInsert(ref System.Nullable OrderID, string CustomerID, System.Nullable EmployeeID, 
          System.Nullable OrderDate, System.Nullable RequiredDate, System.Nullable 
          ShippedDate, System.Nullable ShipVia, System.Nullable 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;          
             this.Connection.Close();           
             return affectedRows;      
          }  
          
          public int SelectIdentityQuery()      
          {          
             OleDbCommand cmd = new OleDbCommand("SELECT @@IDENTITY", this.Connection);          
             int newID = (int)cmd.ExecuteScalar();           
             return newID;      
          }  
       }
    }
    

  14. Click the SmartTag on the upper right of the WebGrid.
  15. In Choose Data Source field, choose <New data source...>.



  16. In Data Source Configuration Wizard, choose Object and click OK.



  17. Select your Business Object and click Next.



  18. Click Next and go to Insert Method. Ensure that the method is referring to the Insert method for identity which created at dsNorthwind_extended and click Finish.



  19. Finally, choose Retrieve Structure action in Connected to Data Source Control Wizard and click OK.



  20. Select ObjectDataSource control at design mode and press F4. Click on InsertParamerters Collection.



  21. Select OrderID and click on Show Advanced Properties.



  22. Ensure that Direction properties is assigned to InputOutput and OK.




  23. Click on ObjectDataSource control at design mode and press F4 (server side event mode).




  24. At Server-side, add the following code:
  25. C# Copy Code
    protected void ObjectDataSource1_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
    {   
       WebGrid1.SetOutputParameters(e.OutputParameters);
    }
    

See Also

©2012 Intersoft Solutions Corp. All Rights Reserved.