Tuesday, May 26, 2015

jQuery datepicker display format

  $('#txtPrebid').datepicker({
                    dateFormat: 'dd/mm/yy'
                });

Thursday, May 21, 2015

How to add a default “Select” option to this ASP.NET DropDownList control?

Status status = new Status();
DropDownList1.DataSource = status.getData();
DropDownList1.DataValueField = "ID";
DropDownList1.DataTextField = "Description";
DropDownList1.DataBind();

// Then add your first item
DropDownList1.Items.Insert(0, "Select");

Wednesday, May 20, 2015

ASP.NET Get data in json format from code behind

   $.ajax({
                url: baseUrl + "/Admin/GetEnrollment",
                type: "POST",
                contentType: "application/json; charset=UTF-8",
                dataType: "json",
                data: JSON.stringify(model),
                success: function (data) {
}});

[HttpPost]
        public System.Web.Mvc.JsonResult GetEnrollment(SchoolEnrollment model, HttpRequestMessage request)
        {
            IList<SchoolEnrollment> EnrollmentList = new List<SchoolEnrollment>();

            EnrollmentList = admin.GetEnrollment(model);

            var data = new { EnrollmentList };

            return new System.Web.Mvc.JsonResult()
            {
                Data = data,
                JsonRequestBehavior = System.Web.Mvc.JsonRequestBehavior.AllowGet
            };
        }
  public IList<SchoolEnrollment> GetEnrollment(SchoolEnrollment model)
        {
            return dal.GetEnrollment(model);
        }

 public IList<SchoolEnrollment> GetEnrollment(SchoolEnrollment model)
        {
            IList<SchoolEnrollment> EnrollmentList = new List<SchoolEnrollment>();

            SqlParameter[] sqlParameters = {  SQLHelper.SqlParameter("@PageIndex",SqlDbType.Int, model == null ? 0 : model.PegInfo.PageIndex),
                                              SQLHelper.SqlParameter("@PageSize",SqlDbType.Int,  model == null ? 0 : model.PegInfo.PageSize),
                                             };

            try
            {
                using (SqlDataReader rdr = SQLHelper.ExecuteReader(SQLHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, "[usp]", sqlParameters))
                {
                    while (rdr.Read())
                    {
                        SchoolEnrollment vInfo = new SchoolEnrollment();
                        vInfo.ClassName = Functions.ToString(rdr["description"]);
                        vInfo.SchoolName = Functions.ToString(rdr["school_name"]);
                        vInfo.BoysCount = Functions.ToInt32(rdr["boys_count"]);
                        vInfo.GirlCount = Functions.ToInt32(rdr["girls_count"]);
                        vInfo.SchoolEnrollmentId = Functions.ToInt32(rdr["enrollment_id"]);

                        if (model != null)
                        {
                            if (model.PegInfo != null)
                            {
                                vInfo.PegInfo.PageIndex = model.PegInfo.PageIndex;
                                vInfo.PegInfo.PageSize = model.PegInfo.PageSize;
                                vInfo.PegInfo.TotalRecords = Functions.ToInt32(rdr["record_count"]);
                            }
                        }
                        EnrollmentList.Add(vInfo);
                    }



                    rdr.Close();
                    rdr.Dispose();
                }
                return EnrollmentList;
            }
            catch (Exception ex)
            {
                throw new BaseException(ex.Message, ex);
            }
            finally
            {

            }
        }

Tuesday, May 19, 2015

C# - serialize object to JSON format using JavaScriptSerializer

What is SERIALIZATION?

Serialization is the process of converting the state of an object into a form that can be persisted or transported. The complement of serialization is deserialization, which converts a stream into an object. Together, these processes allow data to be easily stored and transferred.

What is an Interface?

An interface is not a class. It is an entity that is defined by the word Interface. An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body. As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesn’t support multiple inheritance, interfaces are used to implement multiple inheritance.

What is an Abstract Class?

An abstract class is a special kind of class that cannot be instantiated. So the question is why we need a class that cannot be instantiated? An abstract class is only to be sub-classed (inherited from). In other words, it only allows other classes to inherit from it but cannot be instantiated. The advantage is that it enforces certain hierarchies for all the subclasses. In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.