using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class Default11 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.UpdateCommandName)
        {
            OrderRecord orderForEdit = new OrderRecord();
            ((GridEditFormItem)e.Item).UpdateValues(orderForEdit);

            //handle update
            //call update method of the object instead
            orderForEdit.Update();
        }
    }

    protected void RadGrid1_ItemEvent(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
        {
            GridEditFormItem item = (GridEditFormItem)e.Item;
            int primaryKeyValue = (int)item.OwnerTableView.DataKeyValues[item.ItemIndex]["OrderID"];
            OrderRecord orderForEdit = new OrderRecord(primaryKeyValue);
            item.DataItem = orderForEdit;
        }
    }

    protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {        
        DataSet1TableAdapters.OrdersTableAdapter o = new DataSet1TableAdapters.OrdersTableAdapter();        
        RadGrid1.DataSource = o.GetData();
    }
}

public class OrderRecord
{
    public int OrderID
    { get; set; }

    public string ShipName
    { get; set; }

    public string ShipCity
    { get; set; }

    public OrderRecord()
    { 
    }

    public OrderRecord(int OrderID)
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "select * from Orders where OrderID = @OrderID";
        cmd.Parameters.Add(new SqlParameter("OrderID", OrderID));
        cmd.Connection = conn;

        conn.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            this.OrderID = (int)dr["OrderID"];
            this.ShipName = dr["ShipName"].ToString();
            this.ShipCity = dr["ShipCity"].ToString();
        }
        conn.Close();
    }

    public void Update()
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "Update Orders set ShipName=@ShipName, ShipCity=@ShipCity where OrderID=@OrderID";
        cmd.Parameters.Add(new SqlParameter("ShipName", ShipName));
        cmd.Parameters.Add(new SqlParameter("ShipCity", ShipCity));
        cmd.Parameters.Add(new SqlParameter("OrderID", OrderID));
        cmd.Connection = conn;
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
    }
}
