none
Store update LINQ query and pass to json RRS feed

  • Question

  • I am trying to create an update linq query & I generate table using jQuery so after update how do I save changes to data in the table?

     [WebMethod]
        public static string updateselect_data(int ServiceID, string frequency, string Freq_Du, string Freq_Mil)
        {
    
            try
            {
                Track_Data track = new Track_Data();
                DataTable dt = new DataTable();
                var update =
                    from p in track.Serv
                    where p.ServiceID == ServiceID
                    select p;
    
                foreach (var q_update in update)
                {
                    q_update.frequency= frequency;
                    q_update.Freq_Du= Freq_Du;
                    q_update.Freq_Mil= Freq_Mil;
                }
    
                HttpContext.Current.Session["SID"] = ServiceID;
                HttpContext.Current.Session["frequency"] = frequency;
                HttpContext.Current.Session["Freq_Du"] = Freq_Du;
                HttpContext.Current.Session["Freq_Mil"] = Freq_Mil;
    
                try
                {
                    track.SaveChanges();
                }
                catch (Exception e)
                {
                    throw new Exception();
    
                }
    
            return new JavaScriptSerializer().Serialize(update);
            }
            catch
            {
                throw new Exception();
            }

    JQUERY CODE

    <script type="text/javascript">
        debugger;
        $(function () {
                $('[ID*=btn_update]').on('click', function () {
                var ServiceID = '<%=Session["SID"]%>';
                var frequency='<%=Session["frequency"]%>';
                    var Freq_Du = '<%=Session["Freq_Dur"]%>';
                    var Freq_Mil = '<%=Session["Freq_Mil"]%>';
                debugger;
                var obj = {};
                obj.ServiceID = ServiceID;
                obj.frequency = frequency;
                obj.Freq_Dur= Freq_Du;
                obj.Freq_Mil = Freq_Mil;
    
                updatedata(obj);
    
            });
        });
        function updatedata(obj){
            $.ajax({
                type: "POST",
                url: "Maintenance.aspx/updateselect_data",
                contentType: "application/json;charset=utf-8",
                data:"{'SID:'"+ obj.ServiceID+"','frequency:'"+obj.frequency+"','Freq_Du:'"+obj.Freq_Du+"','Freq_Mil:'"+obj.Freq_Mil+"'}",
               dataType: "json",
            success:function(result)
            {
                debugger;
                var up = JSON.parse(result.d).response;
    
            }
        });
        }
    
    </script>

    i create table throgh jquery now first i want to update and want to check in console now when i do above code this show error s

    Invalid object passed in JSON

    image

    Wednesday, October 19, 2016 11:54 AM

All replies

  • Hi rebmaamberrebmaamber,

    Based on your description and related code, I create a demo and reproduce your issue on my side, it seems that you send the error arguments to your service. and I modify my code like below, it works.

    #aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Maintenance.aspx.cs" Inherits="WebApplication2.Maintenance" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    <script src="../Scripts/jquery-1.10.2.min.js"></script>
        <script type="text/javascript">
        debugger;
        $(function () {
                $('[ID*=btn_update]').on('click', function () {
                var ServiceID = '<%=Session["SID"]%>';
                var frequency='<%=Session["frequency"]%>';
                var Freq_Du = '<%=Session["Freq_Dur"]%>';
                var Freq_Mil = '<%=Session["Freq_Mil"]%>';
    
                debugger;
                var obj = {};
                obj.ServiceID = ServiceID;
                obj.frequency = frequency;
                obj.Freq_Dur= Freq_Du;
                obj.Freq_Mil = Freq_Mil;
                updatedata(obj);
            });
        });
    
            function updatedata(obj) {
                debugger;
                var sendData = {};
                sendData.ServiceID = obj.ServiceID;
                sendData.frequency = obj.frequency;
                sendData.Freq_Du = obj.Freq_Dur;
                sendData.Freq_Mil = obj.Freq_Mil;
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "http://localhost:2840/Maintenance.aspx/updateselect_data",
                    data: JSON.stringify(sendData),
                    dataType: "json",
                    success: function (result) {
                        debugger;
                        var up = result.d;
                    },
                    error: function (a) {
                        debugger;
                        alert(JSON.stringify(a));
                    }
                });
              
            }
    
           
    
    </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <input id="btn_update" type="button" value="Update" />
        </div>
        </form>
    </body>
    </html>
    

    #Code Behind

    using System;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Script.Serialization;
    using System.Web.Services;
    using WebApplication2.Models;
    
    namespace WebApplication2
    {
        public partial class Maintenance : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    HttpContext.Current.Session["SID"] = 1;
                    HttpContext.Current.Session["frequency"] = "Test";
                    HttpContext.Current.Session["Freq_Dur"] = "TestF";
                    HttpContext.Current.Session["Freq_Mil"] = "TestM";
                }
            }
    
            [WebMethod]
            public static string updateselect_data(int ServiceID, string frequency, string Freq_Du, string Freq_Mil)
            {
                try
                {
                    Track_Data track = new Track_Data();
                    DataTable dt = new DataTable();
                    var update =
                        from p in track.Servs
                        where p.ServiceID == ServiceID
                        select p;
    
                    foreach (var q_update in update)
                    {
                        q_update.frequency = frequency;
                        q_update.Freq_Du = Freq_Du;
                        q_update.Freq_Mil = Freq_Mil;
                    }
    
                    HttpContext.Current.Session["SID"] = ServiceID;
                    HttpContext.Current.Session["frequency"] = frequency;
                    HttpContext.Current.Session["Freq_Dur"] = Freq_Du;
                    HttpContext.Current.Session["Freq_Mil"] = Freq_Mil;
    
                    try
                    {
                        track.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        throw new Exception();
    
                    }
    
                    return new JavaScriptSerializer().Serialize(update);
                }
                catch
                {
                    throw new Exception();
                }
            }
    
    
        }
    }
    

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, October 20, 2016 3:35 AM
    Moderator
  • now i get ServiceID from table and i try to update record on the bases of ServiceID

    i try this

                    $(function () {
                        $('#services_schdulue').on('click', 'tr', function () {
                            debugger;
                            var row = $(this);
                            var ServiceID = row.find('td')[0].firstChild.data;
                            var s = {};
                            s.ServiceID = ServiceID;
    
    
                        $('[ID*=btn_update]').on('click', function () {
                            debugger;
                            var ServiceID = s.ServiceID;
                             var frequency = $('#txt_repeat').val();
                            var Freq_Du = $('#dura_values').val();
                            var Freq_Mil= $('#text_mil').val();
    
                            debugger;
    
                            var obj = {};
                            obj.ServiceID = ServiceID;
                            obj.frequency = frequency;
                            obj.Freq_Du = Freq_Du;
                            obj.Freq_Mi= Freq_Mil;
                            updatedata(obj);
    
                        });
                    });
                 }
             alert("12-1");
                    function updatedata(obj) {
                        $.ajax({
                            type: "POST",
                            url: "Maintenance.aspx/updateselect_data",
                            contentType: "application/json;charset=utf-8",
                            data: "{'ServiceID':'" + obj.ServiceID + "','frequency':'" + obj.frequency + "','Freq_Du':'" + obj.Freq_Du + "','Freq_Mil':'" + obj.Freq_Mil + "'}",
                            dataType: "json",
                            async: true,
                            cache: false,
                        success:function(result)
                        {
                            alert("15-1");
                            debugger;
                 var up = JSON.parse(result.d).response;
         $("#txt_repeat" + obj.frequency).html(obj.frequency);
                $("#dura_values" + obj.Freq_Duration).html(obj.Freq_Duration);
                $("#text_mil" + obj.Freq_Mileage).html(obj.Freq_Mileage);
    
                $("#text_mil" + obj.frequency + obj.Freq_Duration + obj.Freq_Mileage).html(obj.frequency + obj.Freq_Duration + obj.Freq_Mileage);
                        },
                        error: function (error) {
                            var r = error.responseText;
                            var errorMessage = r.Message;
                             alert(errorMessage);
                            alert(r);
                            alert("error")
                        }
    
                    });
                    }

    data in database table like this

     Service ID frequency Freq_Du Freq_Mil
        1           2         month          1200

    and i display data in page like this

     Service ID    Info
        1           2 month 1200

    now when i update this record changes reflect in database table but not reflect in table which i display in page

    i concatenate data in 1 cell like this

     var example = $("#services_schdulue").DataTable({
                            "dom": 'Blfrtip',
                            "columns":[
                                {
                                    "title": "Service ID",
                                    "data": "Service ID"
                                },{
                                    "title": "frequency",
                                    "data": "frequency",
                                    "visible": false
                                }, {
                                    "title": "Freq_Du",
                                    "data": "Freq_Du",
                                    "visible": false
                                },{
                                    "title": "Freq_Mil",
                                    "data": "Freq_Mil",
                                    "render": function(data, type, row){
                     return row.frequency + row.Freq_Du+ row.Freq_Mil
                                    }
                                },

    Thursday, October 20, 2016 5:12 AM
  • Hi rebmaamberrebmaamber,

    Do you retrieve the data from database and check if it changes?

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 24, 2016 9:41 AM
    Moderator