We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How to perform Crud operation in Gridd view through C# code beghind

Sir,
I am  not able to perform the Crud(create, remove, update and Delete operation)operation on the Grid View from C# code or web services.
When we use  Data Manager through web services  the data is not displayed .
So please tell me how to use data manager through web services.

1 Reply

BM Balaji Marimuthu Syncfusion Team June 5, 2015 11:54 AM UTC

Hi Bhaweshdeepak,

Thanks for using Syncfusion Products.

Based on your requirement we have created a sample using ASP.NET Web Services and performed the CRUD operation using UrlAdaptor in Grid.

Please refer the sample and code snippet below:
Sample: Sample


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

        }


Please refer the documentation for more details using UrlAdaptor:
http://help.syncfusion.com/ug/js/Documents/urladaptor.htm

Please let us know if you have any queries.

Regards,
Balaji Marimuthu

Loader.
Live Chat Icon For mobile
Up arrow icon