Pages

Saturday, 18 January 2014

How to send json data from Ajax call to server side C#

Ajax call in client side

function SaveDetailDatas() {

            if (IsInsert) {
                $("#hdnIshipmentno").val("");
                var JsonObj = GetDetailDatas();
                if (JsonObj.length != 0) {
                    $.ajax({

                        url: "LogisticsV2.aspx/SaveDetails",
                        type: 'POST',
                        data: "{ jsonData: '" + JSON.stringify(JsonObj) + "',businesstype:'" + BusinessType + "'}",
                        contentType: 'application/json',
                        dataType: 'json',
                        async: false,
                        success: function (msg) {
                            $("#hdnIshipmentno").val(msg.d.trim());

                            $("[type='CheckBox'].cbSelectDtl:checked").parent().parent("tr").each(function () {
                                if ($(this).find(".Mode").val() == "i")
                                    $(this).find(".Mode").val("u");
                            });

                        },
                        error: function (xhr) {
                        }
                    });
                }
            }
        }

Server side C#

  [WebMethod]
        public static string SaveDetails(string jsonData, string businesstype)
        {

            try
            {
                workflow.Components.Inventory cs1 = new workflow.Components.Inventory();
                DataTable dtInsertRows = Newtonsoft.Json.JsonConvert.DeserializeObject<DataTable>(jsonData);
                int rcount = dtInsertRows.Select("Status in (7)").Count();
                if (rcount > 0)
                {
                    DataRow dr = dtInsertRows.Select("Status in (7)").First();

                    if (IsSaved)
                    {
                        GShipmentInvoiceNo = GetshipmentInvoiceNo(businesstype);
                        dr["ShipmentInvoiceNo"] = GShipmentInvoiceNo;
                        LogMgmtMstId = SaveLogisticsMasterInfo(dr);
                    }
                }

                workflow.Components.Inventory cs2 = new workflow.Components.Inventory();
                using (SqlConnection connection = cs2.sqlCon)
                {
                    using (SqlCommand command = new SqlCommand("usp_InsertCbzLogisticsMgmt", connection))
                    {

                        if (LogMgmtMstId > 0)
                        {
                            foreach (DataRow dr1 in dtInsertRows.Rows)
                            {
                                dr1["LogMgmtMstId"] = LogMgmtMstId;
                            }
                        }

                        command.CommandType = CommandType.StoredProcedure;
                        command.UpdatedRowSource = UpdateRowSource.None;
                        command.Parameters.Add("@InvVarId", SqlDbType.VarChar, 15, dtInsertRows.Columns["InvVarId"].ToString());
                        command.Parameters.Add("@Status", SqlDbType.Int, 25, dtInsertRows.Columns["Status"].ToString());
                        command.Parameters.Add("@CourierName", SqlDbType.VarChar, 25, dtInsertRows.Columns["CourierName"].ToString());
                        command.Parameters.Add("@IntModifiedBy", SqlDbType.Int, 25, dtInsertRows.Columns["ModifiedBy"].ToString());
                        command.Parameters.Add("@LogMgmtMstId", SqlDbType.Int, 25, dtInsertRows.Columns["LogMgmtMstId"].ToString());
                        command.Parameters.Add("@RackId", SqlDbType.VarChar, 10, dtInsertRows.Columns["RackId"].ToString());
                        command.Parameters.Add("@PrdModifiedPrice", SqlDbType.Money, 10, dtInsertRows.Columns["PrdModifiedPrice"].ToString());
                        SqlDataAdapter adpt = new SqlDataAdapter();
                        adpt.InsertCommand = command;
                        adpt.UpdateBatchSize = 30;
                        connection.Open();
                        int recordsInserted = adpt.Update(dtInsertRows);
                        connection.Close();

                    }
                }

            }
            catch (Exception ex)
            {
                EsHelper.EsADLogger("LogisticsV2.aspx SaveDetails() ", ex.Message.ToString());
            }
            return GShipmentInvoiceNo;
        }

No comments:

Post a Comment