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

Glossary Item Box

This walkthrough shows you how to updatable WebGrid using custom object.

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 updatable WebGrid using custom object

  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. Create New Folder in App_Code folder (Please create App_Code folder if it doesn't exist yet.) and named it as GenericObject. We'll use GenericObject as the namespace for the class.
  6. Right-click on the GenericObject folder and select Add New Item from context menu.
  7. In Add New Item, choose Class from Visual Studio installed templates, select the language to be Visual C#, and named the class as Customers.cs.


  8. Write the following code inside the Customers.cs class.
    C# Copy Code
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using ISNet.WebUI.DataSource;
    
    namespace GenericObject
    { 
       public class Customer 
       {      
          #regionFields      
          private string _customerID;      
          private string _companyName;      
          private string _contactName;      
          private string _contactTitle;      
          private string _address;      
          private string _city;      
          private string _region;      
          private string _postalCode;      
          private string _country;      
          private string _phone;      
          private string _fax;      
          #endregion      
          #regionProperties      
          [PrimaryKey()]      
          [Caption("Customer ID")]      
          public string CustomerID      
          {          
             get { return this._customerID; }          
             set { this._customerID = value; }      
          }      
          [Caption("Company Name")]      
          public string CompanyName      
          {          
             get { return this._companyName; }          
             set { this._companyName = value; }      
          }      
          [Caption("Contact Name")]      
          public string ContactName      
          {          
             get { return this._contactName; }          
             set { this._contactName = value; }      
          }      
          [Caption("Contact Title")]      
          public string ContactTitle      
          {          
             get { return this._contactTitle; }          
             set { this._contactTitle = value; }      
          }      
          public string Address      
          {          
             get { return this._address; }          
             set { this._address = value; }      
          }      
          public string City      
          {          
             get { return this._city; }          
             set { this._city = value; }      
          }      
          public string Region      
          {          
             get { return this._region; }          
             set { this._region = value; }      
          }      
          [Caption("Postal Code")]      
          public string PostalCode      
          {          
             get { return this._postalCode; }          
             set { this._postalCode = value; }      
          }      
          public string Country      
          {          
             get { return this._country; }          
             set { this._country = value; }      
          }      
          public string Phone      
          {          
             get { return this._phone; }          
             set { this._phone = value; }     
          }      
          public string Fax      
          {          
             get { return this._fax; }          
             set { this._fax = value; }      
          }      
          #endregion      
          #regionConstructors      
          public Customer()      
          {              
          
          }      
          public Customer(string customerID)      
          {          
             this._customerID = customerID;      
          }      
          public Customer(string customerID, string companyName, string contactName, string contactTitle, string address,          
              string city, string region, string postalCode, string country, string phone, string fax)      
          {          
             this._customerID = customerID;          
             this._contactName = contactName;          
             this._companyName = companyName;          
             this._contactTitle = contactTitle;          
             this._address = address;          
             this._city = city;          
             this._region = region;          
             this._postalCode = postalCode;          
             this._country = country;          
             this._phone = phone;          
             this._fax = fax;     
          }      
          #endregion 
       }
    }                            
    

  9. Right-click on the GenericObject folder and select Add New Item from context menu.
  10. In Add New Item, choose Class from Visual Studio installed templates, select the language to be Visual C#, and named the class as CustomerDataAccess.cs.
  11. Write the following code inside the CustomerDataAccess.cs class.
    C# Copy Code
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.OleDb;
    using System.Collections.Generic;
    
    namespace GenericObject
    {   
       [System.ComponentModel.DataObject]   
       public class CustomerDataAccess   
       {       
          private OleDbConnection _connection;       
          public OleDbConnection Connection       
          {           
             get           
             {               
                if (this._connection == null)               
                {                   
                   this._connection = new OleDbConnection();                   
                   this._connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["NorthWindConnectionString"].ConnectionString;               
                }               
                return this._connection;           
             }       
          }       
          public CustomerDataAccess()       
          {       
          }       
           // using Generic Collection       
           [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]       
           public List GetCustomers()       
           {           
              List customers = new List();           
              try           
              {               
                 this.Connection.Open();               
                 string cmdText = "SELECT " +                   
                          "CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, " +                   
                 "PostalCode, Country, Phone, Fax FROM Customers";            
                 OleDbCommand command = new OleDbCommand(cmdText, this.Connection);               
                 OleDbDataReader reader = command.ExecuteReader();               
                 while (reader.Read())               
                 {                   
                    Customer customer = new Customer();                   
                    customer.CustomerID = reader.GetString(0);                   
                    customer.CompanyName = reader.GetString(1);                   
                    customer.ContactName = reader.GetString(2);                   
                    customer.ContactTitle = reader.GetString(3);                   
                    customer.Address = reader.GetString(4);                  
                    customer.City = reader.GetString(5);                   
                    customer.Region = reader.IsDBNull(6) ? "" : reader.GetString(6);                   
                    customer.PostalCode = reader.IsDBNull(7) ? "" : reader.GetString(7);                   
                    customer.Country = reader.IsDBNull(8) ? "" : reader.GetString(8);                   
                    customer.Phone = reader.IsDBNull(9) ? "" : reader.GetString(9);                   
                    customer.Fax = reader.IsDBNull(10) ? "" : reader.GetString(10);                   
                    customers.Add(customer);               
                 }              
                 return customers;           
              }           
              finally           
              {               
                 this.Connection.Close();           
              }       
           }       
           [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]       
           public int Update(Customer customer)       
           {           
              try           
              {               
                 this.Connection.Open();              
                 string cmdText = @"UPDATE [Customers] SET " +                   
                       "[CompanyName] = @CompanyName, " +                   
                       "[ContactName] = @ContactName, [ContactTitle] = @ContactTitle, " +                   
                       "[Address] = @Address, [City] = @City, [Region] = @Region, " +                   
                       "[PostalCode] = @PostalCode, [Country] = @Country, [Phone] = @Phone, " +                   
                       "[Fax] = @Fax WHERE (([CustomerID] = @CustomerID))";               
                 OleDbCommand command = new OleDbCommand(cmdText, this.Connection);               
                 command.Parameters.Add(new OleDbParameter("@CompanyName", customer.CompanyName));               
                 command.Parameters.Add(new OleDbParameter("@ContactName", customer.ContactName));               
                 command.Parameters.Add(new OleDbParameter("@ContactTitle", customer.ContactTitle));               
                 command.Parameters.Add(new OleDbParameter("@Address", customer.Address));               
                 command.Parameters.Add(new OleDbParameter("@City", customer.City));               
                 command.Parameters.Add(new OleDbParameter("@Region", customer.Region));               
                 command.Parameters.Add(new OleDbParameter("@PostalCode", customer.PostalCode));               
                 command.Parameters.Add(new OleDbParameter("@Country", customer.Country));               
                 command.Parameters.Add(new OleDbParameter("@Phone", customer.Phone));               
                 command.Parameters.Add(new OleDbParameter("@Fax", customer.Fax));               
                 command.Parameters.Add(new OleDbParameter("@CustomerID", customer.CustomerID));               
                 return command.ExecuteNonQuery();           
              }           
              finally           
              {               
                 this.Connection.Close();           
              }       
           }       
           [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Delete, true)]       
           public int Delete(Customer customer)       
           {           
              try           
              {               
                 this.Connection.Open();               
                 string cmdText = "DELETE FROM [Customers] WHERE (([CustomerID] = @CustomerID))";               
                 OleDbCommand command = new OleDbCommand(cmdText, this.Connection);               
                 command.Parameters.Add(new OleDbParameter("@CustomerID", customer.CustomerID));               
                 return command.ExecuteNonQuery();           
              }           
              finally           
              {               
                 this.Connection.Close();           
              }       
           }       
           [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, true)]       public int Insert(Customer customer)       
           {           
              try           
              {               
                 this.Connection.Open();               
                 string cmdText = @"INSERT INTO [Customers] " +                   
                            "([CustomerID], [CompanyName], [ContactName], [ContactTitle], [Address], [City], [Region], [PostalCode], [Country], [Phone], [Fax]) " +                   
                            "VALUES (@CustomerID, @CompanyName, @ContactName, @ContactTitle, @Address, @City, @Region, @PostalCode, @Country, @Phone, @Fax)";
                 OleDbCommand command = new OleDbCommand(cmdText, this.Connection);               
                 command.Parameters.Add(new OleDbParameter("@CustomerID", customer.CustomerID));               
                 command.Parameters.Add(new OleDbParameter("@CompanyName", customer.CompanyName));               
                 command.Parameters.Add(new OleDbParameter("@ContactName", customer.ContactName));               
                 command.Parameters.Add(new OleDbParameter("@ContactTitle", customer.ContactTitle));               
                 command.Parameters.Add(new OleDbParameter("@Address", customer.Address));               
                 command.Parameters.Add(new OleDbParameter("@City", customer.City));               
                 command.Parameters.Add(new OleDbParameter("@Region", customer.Region));               
                 command.Parameters.Add(new OleDbParameter("@PostalCode", customer.PostalCode));               
                 command.Parameters.Add(new OleDbParameter("@Country", customer.Country));               
                 command.Parameters.Add(new OleDbParameter("@Phone", customer.Phone));               
                 command.Parameters.Add(new OleDbParameter("@Fax", customer.Fax));               
                 return command.ExecuteNonQuery();           
              }           
              finally           
              {               
                 this.Connection.Close();           
              }       
           }   
       }
    }
    

  12. Right-click on Project's name and select Add New Item.
  13. Select Intersoft AppForm in the My Templates box and named it as Walkthrough.aspx.
  14. Drag WebGrid instance from Toolbox to WebForm.
  15. In the design view of Walkthrough.aspx, click the SmartTag on the upper right of the WebGrid.
  16. In Choose Data Source field, choose <New data source...>.



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



  18. Select GenericObject.CustomerDataAccess as the business object and then click Next.



  19. If the method for each select, update, insert, and delete operation is correct, click Finish.



  20. Choose Retrieve Structure in the Connected to Data Source Control wizard and click OK.



  21. Click WebGrid SmartTag on the upper right corner of WebGrid and check Allow Add New, Allow Editing, Allow Delete, Allow Column Sizing, and Allow Context Menu checkbox.



  22. In the Properties, set the width of the WebGrid to 100%.
  23. Run the project and the WebGrid should look like following.

See Also

©2012 Intersoft Solutions Corp. All Rights Reserved.