Thursday, 9 February 2017

Page Call

public partial class EducationCategoryMst : System.Web.UI.Page
    {
        List<EducationCategory> educationcategoryList = null;
        EducationCategory educationcategoryEntity = new EducationCategory();
        HttpClient client = new HttpClient();
        HttpResponseMessage hrm = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            var appsurl = ConfigurationManager.AppSettings["APIPath"].ToString();
            client.BaseAddress = new Uri(appsurl);
            if (!Page.IsPostBack)
            {
                //Add an Accept header for JSON format.
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                GetData();
            }
        }

        #region GetEducationCategory
        private void GetData()
        {
            try
            {
                HttpResponseMessage hrm = client.GetAsync("api/EducationCategory").Result;
                if (hrm.IsSuccessStatusCode)
                {
                    educationcategoryList = hrm.Content.ReadAsAsync<List<EducationCategory>>().Result;
                    eduCateGrid.DataSource = null;
                    eduCateGrid.DataSource = educationcategoryList;
                    eduCateGrid.DataBind();
                }
            }
            catch (Exception ex)
            {

            }
        }
        #endregion

        protected void Clear()
        {
            txtEduCateCode.Text = "";
            txtCategory.Text = "";
            txtDescription.Text = "";
        }

        #region SaveEducationCategory
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (btnSave.Text == "Save")
                {
                    //Add an Accept header for JSON format.
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    educationcategoryEntity.EducationCategory_Code = txtEduCateCode.Text;
                    educationcategoryEntity.EducationCategory_Name = txtCategory.Text;
                    educationcategoryEntity.EducationCategory_Description = txtDescription.Text;
                    educationcategoryEntity.InsertedBy = 1;
                    educationcategoryEntity.InsertedOn = DateTime.Now;
                    educationcategoryEntity.IsActive = true;
                    educationcategoryEntity.IsDelete = false;

                    HttpResponseMessage content = client.PostAsJsonAsync("api/EducationCategory", educationcategoryEntity).Result;
                    if (content.IsSuccessStatusCode)
                    {
                        //lblInfomation.Text = "City Added";
                        Clear();
                        GetData();
                    }
                    else
                    {
                        //lblInfomation.Text = "Error Code" + response.StatusCode + " : Message - " + response.ReasonPhrase;
                    }
                }
                else
                {
                    //Add an Accept header for JSON format.
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    string strID = eduCateGrid.MasterTableView.Items[0].GetDataKeyValue("EducationCategory_Id").ToString();
                    educationcategoryEntity.EducationCategory_Code = txtEduCateCode.Text;
                    educationcategoryEntity.EducationCategory_Name = txtCategory.Text;
                    educationcategoryEntity.EducationCategory_Description = txtDescription.Text;
                    educationcategoryEntity.LastModifiedBy = 1;
                    educationcategoryEntity.LastModifiedOn = DateTime.Now;
                    educationcategoryEntity.IsActive = true;
                    educationcategoryEntity.IsDelete = false;

                    HttpResponseMessage content = client.PutAsJsonAsync("api/EducationCategory/" + strID, educationcategoryEntity).Result;
                    if (content.IsSuccessStatusCode)
                    {
                        //lblInfomation.Text = "City Added";
                        Clear();
                        GetData();
                    }
                    else
                    {
                        //lblInfomation.Text = "Error Code" + response.StatusCode + " : Message - " + response.ReasonPhrase;
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }
        #endregion
    }


No comments:

Post a Comment