[Solved] Inserting records to database using nested jQuery Ajax call is not working

ishi12.shah
 
on Jan 24, 2021 10:35 PM
1190 Views

Gridview Rows is not inserting in database using ajax

alert is properly with data but ajax function is not called for gridview

    $("[id*=btnAdd]").bind("click", function () {
        var returnValue = true;
        var return_response = false;
        var msg = "";
        if ($("[id*=txtsuppliername]").val() == "") {
            msg += "Supplier Code not Correct..\n";
            returnValue = false;
        }
        if ($("[id*=txtchallanno]").val() == "0") {
            msg += "Challan Number is not zero..\n";
            returnValue = false;
        }
        if ($("[id*=txtchallandate]").val() == "") {
            msg += "Please Select Challandate..\n";
            returnValue = false;
        }
        if (!returnValue) {
            alert(msg);
            return returnValue;
        }
        else {
            var grn = {};

            grn.GRNMaterialType = $.trim($("[id*=ddlGrnMaterialType]").find("option:selected").val());
            grn.GRNCode = $.trim($("[id*=txtgrncode]").val());
            grn.GRNDate = $.trim($("[id*=txtgrnDate]").val());
            grn.GRNType = $.trim($("[id*=ddlgrntype]").find("option:selected").val());

            grn.SupplierCode = parseInt($("[id*=txtsuppliercode]").val());
            grn.SupplierName = $.trim($("[id*=txtsuppliername]").val());
            grn.GSTNo = $.trim($("[id*=txtsuppliergst]").val());

            grn.ChallanNo = $.trim($("[id*=txtchallanno]").val());
            grn.ChallanDate = $.trim($("[id*=txtchallandate]").val());
            grn.Remarks = $.trim($("[id*=txtRemark]").val());

            grn.GstAmount = parseFloat($("[id*=txtgst_amount]").val());
            grn.NetAmount = parseFloat($("[id*=txtnet_amount]").val());
            grn.GrossPayAmount = parseFloat($("[id*=txtgrosspayamount]").val());
            grn.NetPayAmount = parseFloat($("[id*=txtnetpayamount]").val());
            grn.Amount_D = parseFloat($("[id*=txt_Amount]").val());
            grn.DisAmount = parseFloat($("[id*=txtdiscountAmount]").val());

            grn.Validitydays = ($("[id*=txtvaliditydays]").val() == "" ? "0" : parseInt($("[id*=txtvaliditydays]").val()));
            grn.CurrencyCode = parseInt($("[id*=ddlcurrency]").find("option:selected").val());
            grn.IsApproved = "N";
            grn.Authorizedid = "";
            grn.PONo = $.trim($("[id*=txtSupplierPONo]").val()) == "" ? "0" : $.trim($("[id*=txtSupplierPONo]").val());
            // alert(JSON.stringify(grn));
            $.ajax({
                type: "POST",
                url: currLoc + '/SaveGRN',
                data: '{grn: ' + JSON.stringify(grn) + '}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    alert(response.d);
                    //return_response = response.d;
                    if (response.d) {
                        debugger
                        var SelectedGRNMaterialType = $.trim($("[id*=ddlGrnMaterialType]").find("option:selected").val());
                        var SelectedGRNType = $.trim($("[id*=ddlgrntype]").find("option:selected").val());
                        var GRNCode = $.trim($("[id*=txtgrncode]").val());
                        var GrdDetail = [];
                        $("[id*=grdDetail] tr:not(:first)").each(function () {
                            var obj = {};

                            obj.GRNCode = GRNCode;
                            obj.GRNMaterialType = SelectedGRNMaterialType;
                            obj.GRNType = SelectedGRNType;

                            obj.ItemTypeCode = $(this).find("[id*=lblitemtypecode]").text();
                            obj.ItemCode = $(this).find("[id*=lblitemcode]").text();
                            obj.UoM = $(this).find("[id*=lblitemuom]").text();
                            obj.PurcahseCode = $(this).find("[id*=lblpocode]").text();
                            obj.POQty = parseFloat($(this).find("[id*=lblHpoitemqty]").text());
                            obj.ItemPieces = parseFloat($(this).find("[id*=lblHpoitempieces]").text());
                            obj.PORate = parseFloat($(this).find("[id*=txtHpoitemrate]").val());
                            obj.Size = parseFloat(($(this).find("[id*=txtISize]").val() == "" ? "0" : $(this).find("[id*=txtISize]").val())).toFixed(2);
                            obj.BaleNo = $(this).find("[id*=txtIBaleNo]").val();

                            obj.RollNo = $(this).find("[id*=txtIRollNo]").val();
                            obj.DesignNo = $(this).find("[id*=txtIDesignNo]").val();
                            obj.Color = $(this).find("[id*=txtIColor]").val();
                            obj.ActualGRNQty = parseFloat($(this).find("[id*=txtHqtyreceived]").val());

                            obj.POAmount = parseFloat($(this).find("[id*=lblamount]").text());
                            obj.PODiscount = parseFloat($(this).find("[id*=txtHdiscount]").val());
                            obj.NetAmount = parseFloat($(this).find("[id*=lblnetamount]").text());
                            obj.GSTRate = parseFloat($(this).find("[id*=txtHgstrate]").val());

                            obj.GSTAmount = parseFloat($(this).find("[id*=lblgstamount]").text());
                            obj.GrossAmount = parseFloat($(this).find("[id*=lblgrossamount]").text());
                            obj.ChallanQty = parseFloat($(this).find("[id*=txtHChaqtyreceived]").val());
                            obj.SupplierCode = ($(this).find("[id*=hfSupplierId]").val() == "" ? "0" : $(this).find("[id*=hfSupplierId]").val());

                            obj.ShrinkWidth = parseFloat($(this).find("[id*=txtShrinkWidth]").val() == "" ? "0" : $(this).find("[id*=txtShrinkWidth]").val());
                            obj.ShrinkLength = parseFloat($(this).find("[id*=txtShrinkLength]").val() == "" ? "0" : $(this).find("[id*=txtShrinkLength]").val());
                            obj.DiscountType = $.trim($(this).find("[id*=ddlHDiscountType]").find("option:selected").text());

                            GrdDetail.push(obj);
                        });
                        GrdDetail.pop();
                       // alert(JSON.stringify(GrdDetail));
                        $.ajax({
                            type: "POST",
                            url: currLoc + '/SaveGRNItemDetail',
                            data: '{GrdDetail: ' + JSON.stringify(GrdDetail) + '}',
                            contentType: "application/json; charset=utf-8",
                            dataType: "json",
                            success: function (response) {
                                alert(response.d);
                            },
                            failure: function (response) { alert(response.d); },
                            error: function (response) { alert(response.d); }
                        });
                    }
                    else {
                        return false;
                    }

                    //alert("User has been added successfully.");
                    //window.location.reload();
                },
                error: function (response) {
                    alert(response.d);
                },
                failure: function (response) {
                    alert(response.d);
                }
            });
        }
    });

Please Give me idea

Download FREE API for Word, Excel and PDF in ASP.Net: Download
pandeyism
 
on Jan 24, 2021 11:19 PM

Hi ishi12.shah,

You are handling multiple ajax request so you need to add property async: true check below URL.

Execute Multiple AJAX Requests Simultaneously in ASP.Net

pandeyism
 
on Jan 25, 2021 01:59 AM

Hi ishi12.shah,

Take below link reference to implement loader while you are inserting.

Display loading progress image when Page Loads or does PostBack using ASP.Net