Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
THIS IS MY .CS CODE
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

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

        }
        [System.Web.Services.WebMethod]

        [System.Web.Script.Services.ScriptMethod(UseHttpGet = true, ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
        public static string GetData()
        {
            // int cid = Convert.ToInt32(id);
            DataTable dt = new DataTable();
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ToString()))
            {
                using (SqlCommand cmd = new SqlCommand("select * from data1", con))
                {
                    con.Open();
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    da.Fill(dt);
                    System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                    List<dictionary><string,>> rows = new List<dictionary><string,>>();
                    Dictionary<string,> row;
                    foreach (DataRow dr in dt.Rows)
                    {
                        row = new Dictionary<string,>();
                        foreach (DataColumn col in dt.Columns)
                        {
                            row.Add(col.ColumnName, dr[col]);
                        }
                        rows.Add(row);
                    }
                    return serializer.Serialize(rows);
                    //return s;
                }
            }
        }

        //[System.Web.Services.WebMethod]
        //// [System.Web.Script.Services.ScriptMethod(UseHttpGet = true, ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
        //public static string InsertStudents(string id, string fname, string lname, string mobileno)
        //{
        //    int i = Convert.ToInt32(id);
        //    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ToString());
        //    con.Open();
        //    SqlCommand cmd = new SqlCommand("up", con);
        //    cmd.Parameters.Add("@id", SqlDbType.Int).Value = Convert.ToInt32(id);
        //    cmd.Parameters.Add("@fname", SqlDbType.VarChar).Value = fname;
        //    cmd.Parameters.Add("@lname", SqlDbType.VarChar).Value = lname;
        //    cmd.Parameters.Add("@mobileno", SqlDbType.NVarChar).Value = mobileno;
        //    cmd.CommandType = CommandType.StoredProcedure;

        //    cmd.ExecuteNonQuery();
        //    con.Close();


        //    return "record inserted successfully.";
        //}
        [WebMethod]
        public static string UpdateStudents(string id, string fname, string lname, string mobileno)
        {
            
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ToString());
            con.Open();

            SqlCommand cmd = new SqlCommand("UpdateS", con);
            cmd.Parameters.Add("@id",SqlDbType.Int).Value=Convert.ToInt32(id);
            cmd.Parameters.Add("@fname",SqlDbType.VarChar).Value=fname;
            cmd.Parameters.Add("@lname", SqlDbType.VarChar).Value = lname;
            cmd.Parameters.Add("@mobileno", SqlDbType.NVarChar).Value = mobileno;
            cmd.CommandType = CommandType.StoredProcedure;
            // cmd.Parameters.AddWithValue("@fname", details.FirstName);
            // cmd.Parameters.AddWithValue("@lname", details.LastName);
            // cmd.Parameters.AddWithValue("@mobileno", details.MobileNo);
            // cmd.Parameters.AddWithValue("@id", details.RollNo);
            // con.Open();
            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();
            da.SelectCommand = cmd;
            da.Fill(ds);
           
          
            return "record updated successfully";
        }
        [WebMethod]
        public static string DeleteStudents(string id)
        {
            
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ToString());
            SqlCommand cmd1 = new SqlCommand("delete from data1 where id=@id ", con);
            cmd1.Parameters.Add("@id",SqlDbType.Int).Value=Convert.ToInt32(id);
            cmd1.CommandType = CommandType.StoredProcedure;
            con.Open();
            cmd1.ExecuteNonQuery();
            con.Close();
            return "record delete successfully";
        }

    }

}



THIS IS MY ASPX CODE
XML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="method.aspx.cs" Inherits="method.method" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="Styles/kendo/kendo.common.min.css" rel="stylesheet" type="text/css" />
    <link href="Styles/kendo/kendo.mobile.all.min.css" rel="stylesheet" type="text/css" />
    <link href="Styles/kendo/kendo.default.min.css" rel="stylesheet" type="text/css" />
    <script src="Scripts/kendo/jquery.min.js" type="text/javascript"></script>
    <script src="Scripts/kendo/kendo.all.min.js" type="text/javascript"></script>
</head>
<body>
           <div id="example" class="k-content">


           <div id="grid">
           </div>

  <%--<script type="text/x-kendo-template" id="gridTemplate">
       <table id="table" >
           <tr>
               <th >Roll No</th>

               <td><input type="text" id="txtRollNo" class="k-input k-textbox" /></td>
           </tr>
           <tr>
               <th >First Name</th>
               <td><input type="text" id="txtFirsNamet" class="k-input k-textbox" /></td>
           </tr>
           <tr>
               <th > Last Name  </th>
               <td><input type="text" id="txtLastName" class="k-input k-textbox" /></td>
           </tr>
           <tr>
               <th >Mobile No</th>
               <td><input type="text" id="txtMobileNo" class="k-input k-textbox" />
                    </td>

           </tr>

       </table>


     </script>
       --%>

           <script>

               $(document).ready(function () {
                 
                   var url = "method.aspx/GetData";
                   var url1 = 'method.aspx/UpdateStudents';
                   var url2 = 'method.aspx/DeleteStudents';
                   var url3="method.aspx/InsertStudents";

                   dataSource = new kendo.data.DataSource({
                       
                       transport: {
                           read: function (options) {
                               $.ajax({
                                   type: "GET",
                                   url: url,
                                   contentType: "application/json; charset=utf-8",
                                   //data:{'id':"66"},
                                   dataType: "json",

                                   success: function (result) {
                                       if (result.d === "error") {
                                           alert('Error occured. Please try again');
                                       }

                                       options.success(JSON.parse(result.d));

                                   },
                                   error: function (result) {
                                       alert(result.Id);

                                   }
                               });
                           },


                           update: function (options) {

                               var gview = $("#grid").data("kendoGrid");

                               var selectedItem = gview.dataItem(gview.select());


                               $.ajax({
                                   type: "POST",
                                   url: url1,
                                   
                                   contentType: "application/json; charset=utf-8",




                                   
                                   data: JSON.stringify({ 'id': selectedItem.id, 'fname': selectedItem.fname, 'lname': selectedItem.lname, 'mobileno': selectedItem.mobileno }),
                                  
                                  
                                   

                                   dataType: "json",
                                   success: function (result) {
                                       if (result.d !== "success") {
                                           
                                       }
                                       dataSource.read();

                                   },
                                   error: function (result) {
                                       alert(result.Id);

                                   }
                               });
                              


                           },
                           destroy: function (options) {

                               var gview = $("#grid").data("kendoGrid");

                               var selectedItem = gview.selectedItem(gview.select());
                              
                               gview.dataSource.remove(selectedItem);
                               gview.dataSource.remove(0);
                              



                               $.ajax({
                                   type: "POST",
                                   url: url2,
                                   contentType: "application/json; charset=utf-8",

                                   
                                   data: JSON.stringify({ 'id': selectedItem.id }),
                                   dataType: "json",
                                   success: function (result) {
                                       if (result.d !== "success") {
                                           alert(result.d);
                                       }
                                       dataSource.read();

                                   },
                                   error: function (result) {
                                       alert(result.Id);

                                   }
                               });
                           },




                           parameterMap: function (options, operation) {
                               if (operation !== "read" && options.models) {
                                   return { models: kendo.stringify(options.models) };
                               }
                           }
                       },
                       batch: true,
                       pageSize: 20,
                       schema: {
                           model: {
                               id: "id",
                               fields: {
                                   id: { from: "id", type: "number" },
                                   fname: { from: "fname", type: "string" },
                                   lname: { from: "lname", type: "string" },
                                   mobileno: { from: "mobileno", type: "string" }

                               }
                           }
                       }
                   });

                   $("#grid").kendoGrid({

                       dataSource: dataSource,
                       pageable: true,

                       selectable: "row",
                       mobile: "phone",
                       scrollable: true,

                       height: 430,
                      {mode:'inline',template:serviceEditTemplate}"
                      
                       toolbar: ["create", "save", "cancel"],
                       columns: [
                           "id",

                           { field: "fname", title: "First Name", width: "100px" },
                        { field: "lname", title: "Last Name", width: "100px" },
                        { field: "mobileno", title: "MobileNO", width: "100px" },
                           { command: ["edit", "destroy"], title: "&nbsp;", width: "172px"}],

                      
                       editable: {
                           
                           mode: "inline"
                           
                       },

                   });
               });

           </script>
       </div>


</body>

</html>
Posted
Updated 30-May-14 1:34am
v2
Comments
Nirav Prabtani 30-May-14 7:37am    
what is an error??
Member 9965416 30-May-14 7:56am    
previously i wrote the above code for crud operations in that update and delete are working.
and not insert.
so i have written stored procedure for insert and update by placing if condition and executed the application this time delete is not working i didnt changed the previous delete code still its not working can u tell me should i change any thing in destroy script
Posting all the code is not going to help us. Please be specific on the problem.
RDBurmon 30-May-14 10:01am    
what is an error??
Member 9965416 9-Jun-14 2:05am    
how can I prevent kendo grid cancel event not to change the structure of the data in grid

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900