public class WebService1 : System.Web.Services.WebService { static string cons = ConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString; static SqlConnection con = new SqlConnection(cons);
[WebMethod] public DataTable Get() { SqlCommand getData = new SqlCommand(); getData.CommandText = "usp_DEV_ChangeLog_Select"; // Stored procedure for retrieve data from suppliers table getData.CommandType = CommandType.StoredProcedure; getData.Connection = con; if (con.State != ConnectionState.Open) con.Open(); DataTable sqldata = new DataTable(); SqlDataAdapter sqladapter = new SqlDataAdapter(getData); sqldata.TableName = "Suppliers"; sqladapter.Fill(sqldata); return sqldata; }
|
In the above code snippet, we have created webservices by using the ASP.NET web service and bound dataSource to Grid, in code behind GetDataSource method.
<ej:Grid ID="Grid" runat="server" AllowPaging="true"> <DataManager Adaptor="UrlAdaptor" URL="Default.aspx/GetDataSource" UpdateURL="Default.aspx/Update" InsertURL="Default.aspx/Insert" RemoveURL="Default.aspx/Remove" />
. . . . . . .
</ej:Grid>
[WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] // Return the JSON formatted result public static object GetDataSource(int skip, int take) { CRUD_Service.WebService1 service = new CRUD_Service.WebService1(); var sqldata = service.Get(); // Get data from webservices DataResult result = new DataResult(); List<EditableCustomer> data = (from ord in sqldata.AsEnumerable().Skip(skip).Take(take) // Perform skip take for on demand load paging select new EditableCustomer { SupplierID = ord.ItemArray[0].ToString(), CompanyName = ord.ItemArray[1].ToString(), City = ord.ItemArray[5].ToString(), PostalCode = ord.ItemArray[7].ToString(), Country = ord.ItemArray[8].ToString() }).ToList();
result.result = data; result.count = sqldata.Rows.Count; con.Close(); return result; } public class DataResult { public IEnumerable result { get; set; } public int count { get; set; } }
|
By using the following code snippet, we can perform the Add, Edit, Delete actions in Grid. Please refer the below code snippet:
[WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public static void Update(EditableCustomer value) { ExecuteToSQL("usp_DEV_ChangeLog_Update", 0, value, "update"); // perform Update action } [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public static void Insert(EditableCustomer value) { ExecuteToSQL("usp_DEV_ChangeLog_InsertUpdate", 0, value, "insert"); // perform add action } [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public static void Remove(int key) { ExecuteToSQL("usp_DEV_ChangeLog_Delete", key, null, "remove"); // perform delete action } |