Send Objects within Object using Json -ASP.net (Sub Objects)

The Client server communication based on Well formed JSON string is an effective method to improve the responsiveness within server and browser communication.The rich client with jQury and JavaScript I have used need to generated in the page load which contain the objects (sub objects)  within object (parent objects).

Here I have used the Web service class Within visual Studio to define object structure as a class  .


Web service definition 


namespace testdefultlogin.webservices
{

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
     [System.Web.Script.Services.ScriptService]
    public class clientService : System.Web.Services.WebService
    {

  SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[<connection_name>"].ConnectionString); 
       
     ................

Parent Class

   public class test_feild_sub_feild
        {

            public int test_result_id { get; set; }
            public int test_report_unit_id { get; set; }
            public String feild_name { get; set; }
            public int res_feild_type_id { get; set; }
            public bool gender_wise_status { get; set; }
            public bool age_wise_status { get; set; }
            public String feild_control_name { get; set; }
            public decimal feild_control_x { get; set; }
            public decimal feild_control_y { get; set; }
            public int feild_control_x_width { get; set; }
            public int feild_control_y_width { get; set; }
            public String feild_control_title { get; set; }
            public String feild_control_status { get; set; }
            public int test_resultunit_id { get; set; }
            public String mesure_unit { get; set; }
            public List<res_subctrol_feild> subctrol_feilds { get; set; }

        }

Child Class


        public class res_subctrol_feild
        {
            public int resfld_subctrol_id { get; set; }
            public int result_feild_id { get; set; }
             public String subctrol_feild_name { get; set; }
              public String subctrol_name { get; set; }
            public decimal resfld_subctrol_x { get; set; }
            public decimal resfld_subctrol_y { get; set; }
            public int resfld_subctrol_x_width { get; set; }
            public int resfld_subctrol_y_width { get; set; }
            public String subctrol_title { get; set; }
             public String resfld_subctrol_status { get; set; }
             public String parent_ctrl_name { get; set; }
            public int res_subfeild_type_id { get; set; }
        }


Web Service Method

This method return both parent and child objact as a json string


        [WebMethod]
        public string setAllControls (string test_report_unit_id)
        {

           List<test_feild_sub_feild> userList = new List<test_feild_sub_feild>();

          DataTable subFeildtable = getTestResultSubFeild(test_report_unit_id);
            string jsonString = "";
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
          //  cmd.CommandText = "SELECT result_feild_id,feild_name,feild_control_name,res_feild_type_id,feild_control_x,feild_control_y,feild_control_x_width,feild_control_y_width,feild_control_title,feild_control_status,test_report_unit_id FROM test_result_feild WHERE  test_report_unit_id=" + test_report_unit_id;
            cmd.CommandText = "SELECT result_feild_id,test_report_unit_id,feild_name,res_feild_type_id,gender_wise_status,age_wise_status,feild_control_name,feild_control_x,feild_control_y,feild_control_x_width,feild_control_y_width,feild_control_title,feild_control_status,test_resultunit_id,(SELECT testres_mesre_unit_name FROM testres_mesure_unit WHERE testres_mesure_unit.testres_mesre_unit_id =test_result_feild.test_resultunit_id ) as mesure_unit FROM test_result_feild WHERE test_report_unit_id=" +
             test_report_unit_id + "";
            using (SqlDataReader rdr = cmd.ExecuteReader())
            {
                if (rdr.HasRows)
                {

                    while (rdr.Read())
                    {
                        test_feild_sub_feild user = new test_feild_sub_feild();
                        user.test_result_id = Convert.ToInt32(rdr["result_feild_id"]);
                        user.test_report_unit_id = Convert.ToInt32(rdr["test_report_unit_id"]);
                        user.feild_name = rdr["feild_name"].ToString();
                        user.res_feild_type_id = Convert.ToInt32(rdr["res_feild_type_id"]);
                     
                        user.feild_control_name = rdr["feild_control_name"].ToString();
                        Decimal var_feild_control_x = 0m;
                        bool didParse = Decimal.TryParse(rdr["feild_control_x"].ToString(), out var_feild_control_x);
                        user.feild_control_x = var_feild_control_x;
                        Decimal var_feild_control_y = 0m;
                        didParse = Decimal.TryParse(rdr["feild_control_y"].ToString(), out var_feild_control_y);
                        user.feild_control_y = var_feild_control_y;

                        user.feild_control_x_width = Convert.ToInt32(rdr["feild_control_x_width"]);
                        user.feild_control_y_width = Convert.ToInt32(rdr["feild_control_y_width"]);
                        user.feild_control_status = rdr["feild_control_status"].ToString();
                        if (user.res_feild_type_id==3)
                        {
                            if (rdr["gender_wise_status"].ToString() != "")
                            {
                                user.gender_wise_status = Convert.ToBoolean(rdr["gender_wise_status"].ToString());
                               
                           
                            }
                            if (rdr["age_wise_status"].ToString() != "")
                            {
                                user.age_wise_status = Convert.ToBoolean(rdr["age_wise_status"].ToString());
                            }
                            user.test_resultunit_id = Convert.ToInt32(rdr["test_resultunit_id"].ToString());
                        
                        }
                      
                        user.mesure_unit = rdr["mesure_unit"].ToString();
                        user.feild_control_title = rdr["feild_control_title"].ToString();

                        user.subctrol_feilds =new List<res_subctrol_feild>();
                        if (subFeildtable.Rows.Count > 0)
                        {
                            foreach (DataRow dr in subFeildtable.Rows)
                            {
                                if (Convert.ToInt32(dr["result_feild_id"].ToString()) == user.test_result_id)
                                {

                                    Decimal var_subfeild_control_x = 0m;
                                    bool didParse1 = Decimal.TryParse(dr["resfld_subctrol_x"].ToString(), out var_subfeild_control_x);
                                 
                                    Decimal var_subfeild_control_y = 0m;
                                    didParse1 = Decimal.TryParse(dr["resfld_subctrol_y"].ToString(), out var_subfeild_control_y);
                              
                                    // reconsider the absolute postions for the resfld_subctrol_x , resfld_subctrol_y if not accurate
                                    user.subctrol_feilds.Add( new res_subctrol_feild(){
                                        resfld_subctrol_id = Convert.ToInt32(dr["resfld_subctrol_id"].ToString()),
                                        result_feild_id = Convert.ToInt32(dr["result_feild_id"].ToString()),
                                    subctrol_feild_name=dr["subctrol_feild_name"].ToString(),
                                        subctrol_name=dr["subctrol_name"].ToString(),
                                        resfld_subctrol_x = var_subfeild_control_x,
                                        resfld_subctrol_y = var_subfeild_control_y,
                                        resfld_subctrol_x_width=Convert.ToInt32(dr["resfld_subctrol_x_width"].ToString()),
                                        resfld_subctrol_y_width=Convert.ToInt32(dr["resfld_subctrol_y_width"].ToString()),
                                        subctrol_title=dr["subctrol_title"].ToString(),
                                        resfld_subctrol_status = dr["resfld_subctrol_status"].ToString(),
                                        parent_ctrl_name ="",
                                        res_subfeild_type_id = Convert.ToInt32(dr["res_subfeild_type_id"].ToString())
                                        });
                                }
                            }
                        }


                        userList.Add(user);
                    }
                    jsonString = JsonConvert.SerializeObject(userList);
                }
                else
                {
                    jsonString = "no_data_found";
                }
                rdr.Dispose();
            }

            con.Close();
            return jsonString;
        }



Utilize web service at the page load in client side

  $.ajax({
                type: "POST",
                url: "../webservices/helloService.asmx/setAllControls",
                data: "{test_report_unit_id:" + report_unit_id + "}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(msg) {
                    // if there is no records found
                    if (msg.d === "no_data_found") {
                      }else {
                        console.log("ALL Results ", $.parseJSON(msg.d));
                        $.each($.parseJSON(msg.d), function (key, value) {
                     // access parent objects
                               var activeLink = value.feild_control_status == 'visible' ? "A" : "I";

                            var htmlval = "<p class='ui-widget-header' style='width:10px;float:left;margin-top:0px;'><sub><a id='activeLink"+value.test_result_id+"'  class='activeStatusLink' data-ctrolid="+value.test_result_id+"  data-visible_status="+value.feild_control_status+" >"+activeLink+"</a></sub></p>";
                          
.......................................
                     // access child object in the same loop

   $.each(value.subctrol_feilds, function (key1, value1) {
                        
                            var classval = "";
                            var feildName = "";
                            var contntedit = false;
                            var event_onkeydown = "";
                                
                            //binding sub control on page load Ajax pasing object (propertied binding type wise)
                            if (value1.res_subfeild_type_id == 1) {
                                classval = "expanding_text";
                                feildName = value1.subctrol_feild_name;  //&nbsp;:&nbsp;
                                contntedit = true;
                                event_onkeydown = "if(this.innerHTML.length >= " + exitable_fild_name_langh + "){ alert('maximam lenght exceed');var str = this.innerHTML; str=(str.substr(0, 120)); this.innerHTML=str;};";
                            } else if (value1.res_subfeild_type_id == 2) {
                                classval = "resize_dbtext";
                                feildName = "";
                                contntedit = false;
                                event_onkeydown = "";
                            }
............................

}

 }}

Comments

Popular posts from this blog

ENOENT: no such file or directory, rename : node_modules/async

react-quill Integrate quill-image-resize-module