skip to Main Content

I m working with Jquery Ajax and performing crud operation,

I want to update a record and When I click the update button then fill(means name and address) the form data and then changes of name or address then again press edit button my edit button should not be work?

getlistdata function display the employee list.

How to edit a record?

How to resolve this issue??

Emploee.aspx

<input type="button" id="btnsubmit" value="Submit" onclick="insertrecord()" />

<asp:HiddenField ID="HiddenField1" runat="server" Value="" />
//for insert

function insertrecord() {

                    EmpId = $('HiddenField').val();

                    if ($("#btnsubmit").val() == "Submit") {
                        $.ajax({
                            url: 'Emploee.aspx/insertdata',
                            type: 'POST',
                            contentType: 'application/json;charset=utf-8',
                            dataType: 'json',
                            data: "{name:'" + $("#txtName").val() + "',address:'" + $("#txtAddress").val() + "'}",
                            success: function (data) {
                                //alert(data.d);
                                alert("Insert Data Successfully");
                                 if(data.d)
                                {
                                    window.location.reload();
                                }

                            },
                            Error: function () {
                                alert("Insert Error");
                            }
                        });
                    }
                    else
                    {
                        //when I press again edit button(means change name and address) then the record should not be updated that is my problem??

                        $.ajax({

                            url: 'Emploee.aspx/edit',
                            type: 'post',
                            contentType: 'application/json;charset=utf-8',
                            datatype: 'json',
                            data: "{EmpId: '" + EmpId + "'}",
                            success: function (data) {
                                data = JSON.parse(data.d);
                            },
                            error: function () {
                                alert('Update Error');
                            },
                        });
                    }
                }

                   function getlistdata() {
                    $.ajax({
                        url: 'Emploee.aspx/GetEmpData',
                        type: 'get',
                        contentType: 'application/json;charset=utf-8',
                        dataType: 'json',
                        success: function (data) {
                            //alert(data.d);
                            //exit();
                            data = JSON.parse(data.d);
                            for (var i = 0; i < data.length; i++) {
                                $("#tbl").append('<tr><td>' + data[i].EmpName + '</td><td>' + data[i].EmpAge + '</td><td>' + data[i].EmpAddress + '</td> 

<td><input type="button" id="btnedit" value="Edit" onclick="EditData(' + data[i].EmpId + ','' + data[i].EmpName + '','' + data[i].EmpAddress + '')" /></td> </tr>');
                            }
                        },
                        Error: function () {
                            alert("get error");
                        }
                    });
                }

                function EditData(EmpId, EmpName, EmpAddress) {
                    $.ajax({

                        url: 'Emploee.aspx/updates',
                            type: 'post',  
                            contentType: 'application/json;charset=utf-8',  
                            datatype: 'json',  
                            data: "{EmpId: '" + EmpId + "',EmpName:'" + $("#txtName").val() + "',EmpAddress:'" + $("#txtAddress").val() + "'}",
                            success: function (data) {  

                                data = JSON.parse(data.d);  
                                EmpId = EmpId;
                                $("#txtName").val(EmpName);
                                $("#txtAddress").val(EmpAddress);

                                if (EmpId != null) {
                                    $("#btnsubmit").val("Update");
                                }  
                            },  
                            error: function () {  
                                alert('Update Error');  
                            },

                        });
                    }

            </script>

Emploee.aspx.cs

        [WebMethod]  //update (fill form data) when press update button click
        public static void updates(int EmpId, string EmpName, string EmpAddress)
        {
            cn.Open();

            SqlCommand cmd = new SqlCommand("update", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@EmpId", EmpId);
            cmd.Parameters.AddWithValue("@EmpName", EmpName);
            cmd.Parameters.AddWithValue("@EmpAddress", EmpAddress);
            int i = cmd.ExecuteNonQuery();
            cn.Close();

            if (i>0)
            {
                Console.WriteLine("successfully updated");
            }
            else
            {
                Console.WriteLine("successfully Not updated");
            }
        }

        [WebMethod] (when fillable updated record changes then again press update button)
        public static string Edit(int EmpId, string EmpName, string EmpAddress)
        {
            string _data = "";
            cn.Open();
            SqlCommand cmd = new SqlCommand("editsp", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@EmpId", EmpId);
            cmd.Parameters.AddWithValue("@EmpName", EmpName);
            cmd.Parameters.AddWithValue("@EmpAddress", EmpAddress);

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            cn.Close();
            if (ds.Tables[0].Rows.Count > 0)
            {
                _data = JsonConvert.SerializeObject(ds.Tables[0]);
            }
            return _data;
        }

update store procedure

update (fill the form record(name and address) when pressing the update button )

ALTER PROCEDURE [dbo].[update]
    @EmpId int,
    @EmpName varchar(40),
    @EmpAddress varchar(30)
AS
BEGIN
    update tblEmployee set EmpName=@EmpName,EmpAddress=@EmpAddress where EmpId=@EmpId
END

edit (fillable record update(means name and address change) then pressing the again update button


ALTER PROCEDURE [dbo].[edit]
    @EmpId int,
    @EmpName varchar(30),
    @EmpAddress varchar(20)
AS
BEGIN
    select * from tblEmployee where EmpId=EmpId or EmpName=EmpName or EmpAddress=EmpAddress; 
END

see browser logs :

enter image description here

enter image description here

what should be doing I m wrong?

3

Answers


  1. u will change it

    function EditData(empid,empname,empaddress,empage) {

    Login or Signup to reply.
  2. use same property of your webmethod
    Ex.

    public static void update(int id, string name, int age, string address)

    Your data should be

    id:'” + empid + “‘,name:'” + empname + “‘,address:'” + empaddress + “‘,age:'” + empage

    Login or Signup to reply.
  3. change the getlistdata

                function getlistdata() {
                    $.ajax({
                        url: 'Emploee.aspx/GetEmpData',
                        type: 'get',
                        contentType: 'application/json;charset=utf-8',
                        dataType: 'json',
                        success: function (data) {
                data = JSON.parse(data.d);
                          $.each(data, function (index, element) {
                debugger
                $("#tbl").append('<td>' + element.EmpName  + '</td>');
                $("#tbl").append('<td>' + element.EmpAddress + '</td>');
                $("#tbl").append('<td><input type="button" id="btnedit" value="Edit" onclick="EditData(' + element.EmpId + ', '' + element.EmpName + '' ,'' + element.EmpAddress + '')" /></td>');
                            });
    
    
                        },
                        Error: function () {
                            alert("get error");
                        }
                    });
                }
    

    change editdata and take hiddenfield

                function EditData(EmpId, EmpName, EmpAddress) {
                    $.ajax({
    
                        url: 'Emploee.aspx/updates',
                        type: 'post',
                        contentType: 'application/json;charset=utf-8',
                        datatype: 'json',
                        data: "{EmpId: '" + EmpId + "',EmpName:'" + $("#txtName").val() + "',EmpAddress:'" + $("#txtAddress").val() + "'}",
                        success: function (data) {
    
                            data = JSON.parse(data.d);
    
                            $("#txtName").val(EmpName);
                            $("#txtAddress").val(EmpAddress);
                              $("#HiddenField1").val(EmpId);
    
                            if (EmpId != null) {
                                $("#btnsubmit").val("Update");
                            }
                        },
                        error: function () {
                            alert('Update Error');
                        },
    
                    });
                }
    

    else part

    function saverecord() {
    EmpId = $("#HiddenField1").val();
    
    else{
    
                        $.ajax({
    
                            url: 'WebForm1.aspx/updates',
                            type: 'post',
                            contentType: 'application/json;charset=utf-8',
                            datatype: 'json',
                            aync: true,
                            data: "{EmpId: '" + EmpId + "',EmpName:'" + $("#txtName").val() + "',EmpAddress:'" + $("#txtAddress").val() + "'}",
                            success: function (data) {
    
                                window.location.reload();
    
                            },
                            error: function () {
                                alert('Update Error');
                            },
                        });
                    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search