title: How to fetch Json value and fill it on dataset in ASP.NET
body: <p>I am trying to fetch JSON value but failed to retrieve value and fill on <code>DataSet</code> and bind them on <code>GridView</code> and I am getting null value.</p>
  
  <p>Here I have tried code please seen below:</p>
  
  <pre><code>var outputValue = cpmu.getCPMUdata(AgreementNo, AgreementedBy, YearOfAgreement);
  var serializer = new JavaScriptSerializer();
  dynamic obj = serializer.Deserialize&lt;dynamic&gt;(outputValue);
  
  var data = obj["data"];
  
  StringReader theReader = new StringReader(data);
  DataSet theDataSet = new DataSet();
  theDataSet.ReadXml(theReader);
  
  var result = theDataSet.Tables[0];
  
  Label3.Text = theDataSet.Tables[0].Rows[0].Field&lt;string&gt;("AgreementNo");                                   
  Label2.Text = theDataSet.Tables[0].Rows[0].Field&lt;string&gt;("Agreementby");
  Label4.Text = theDataSet.Tables[0].Rows[0].Field&lt;string&gt;("YearofAgreement");
  
  GridView2.DataSource = theDataSet;
  GridView2.DataMember = theDataSet.Tables["Bill"].ToString();
  GridView2.DataBind();
  </code></pre>
  
  <p><strong>JSON</strong> code:</p>
  
  <pre><code>public string getCPMUdata(string AgreementNo, string Agreementby, string YearofAgreement)
  {
      //Code to accept the SSL server certificate
      ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
  
      //Configuration Values
      string strbaseUrl = System.Configuration.ConfigurationManager.AppSettings["baseUrl"];
      string strLoginBaseUrl = System.Configuration.ConfigurationManager.AppSettings["LoginBaseUrl"];
      String username = System.Configuration.ConfigurationManager.AppSettings["UserName"];
      string password = System.Configuration.ConfigurationManager.AppSettings["Password"];
      //***************
  
      /* Input Values */
      string strAgreementNo = AgreementNo;
      string strAgreementby = Agreementby;
      string strYearofAgreement = YearofAgreement;
      /****************/
  
      // Initialize the response
      string returnvalue = string.Empty;
      HttpWebResponse response = null;
      HttpWebRequest request = null;
      String responseText = null;
      String authorization;
  
      try
      {
          //Create the Request Url
          String requestUrl = strLoginBaseUrl + "?service=" + HttpUtility.UrlEncodeUnicode(strbaseUrl + "/3DSpace/resources/CHiPSCPMSBillDetailsModeler/CHiPSCPMSBillDetails?AgreementNo=" + HttpUtility.UrlEncode(strAgreementNo) + "&amp;Agreementby=" + HttpUtility.UrlEncode(strAgreementby) + "&amp;YearofAgreement=" + HttpUtility.UrlEncode(strYearofAgreement));
  
          request = WebRequest.Create(requestUrl) as HttpWebRequest;
          request.Method = "GET";
          request.ContentType = "application/json";
          authorization = System.Convert.ToBase64String(Encoding.UTF8.GetBytes(username + ":" + password));
          request.Headers.Add("Authorization", "Basic " + authorization);
          response = request.GetResponse() as HttpWebResponse;
  
          if (request.HaveResponse == true &amp;&amp; response == null)
          {
              String msg = "response was not returned or is null";
              throw new WebException(msg);
          }
  
          if (response.StatusCode != HttpStatusCode.OK)
          {
              String msg = "response with status: " + response.StatusCode + " " + response.StatusDescription;
              throw new WebException(msg);
          }
  
          // get the first HTTP response content
          StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
          responseText = reader.ReadToEnd();
  
          //extract the LT code from the response string
          String lt = "";
          lt = responseText.Substring(responseText.LastIndexOf("\"lt\""));
          lt = lt.Substring(0, lt.IndexOf(","));
          lt = lt.Replace("\"lt\":", ",");
          lt = lt.Replace("\"", "");
          lt = lt.Replace("\"", "");
          lt = lt.Replace(",", "");
  
          //Extract Session ID from the response header
          String strJsonId = "";
          strJsonId = response.Headers["Set-Cookie"];
          strJsonId = response.Headers["Set-Cookie"].Substring(strJsonId.LastIndexOf("JSESSIONID"));
          strJsonId = strJsonId.Substring(0, strJsonId.IndexOf(";"));
          strJsonId = strJsonId.Replace("JSESSIONID", "");
          strJsonId = strJsonId.Replace("=", "");
          strJsonId = strJsonId.Trim();
  
          //throw exception if access token or sessionid is not available
          if (lt == "" &amp;&amp; strJsonId == "")
          {
              String msg = "Unable to retrieve Access Token and session Key";
              throw new WebException(msg);
          }
  
          //Second HTTP Request to get the Data based on the 'lt' token, 'SessionID' and Query String parameters
          WebRequest requestLast = WebRequest.Create(requestUrl);
          requestLast.Headers.Add("Cookie", "JSESSIONID=" + strJsonId);
          requestLast.Method = "POST";
          string postData = "lt=" + lt + "&amp;username=" + username + "&amp;password=" + password;
          byte[] byteArray = Encoding.UTF8.GetBytes(postData);
  
          // Set the ContentType property of the WebRequest.
          requestLast.ContentType = "application/x-www-form-urlencoded";
          requestLast.ContentLength = byteArray.Length;
  
          // Get the request stream.
          Stream dataStream1 = requestLast.GetRequestStream();
          dataStream1.Write(byteArray, 0, byteArray.Length);
          dataStream1.Close();
  
          // Get the response.
          WebResponse responseLast = requestLast.GetResponse();
          dataStream1 = responseLast.GetResponseStream();
          StreamReader reader1 = new StreamReader(dataStream1);
          returnvalue = reader1.ReadToEnd();
  
          // Clean up the streams.
          reader.Close();
          reader1.Close();
          dataStream1.Close();
          response.Close();
          responseLast.Close();
  
      }
      catch (WebException e)
      {
          if (e.Response != null)
          {
              response = (HttpWebResponse)e.Response;
              returnvalue = response.StatusCode + " " + response.StatusDescription;
          }
          else
          {
              returnvalue = e.Message;
          }
      }
      finally
      {
          if (response != null)
          {
              response.Close();
          }
      }
      return returnvalue;
  }
  
       public static bool AcceptAllCertifications(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certification, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
       {
           return true;
       }
  }
  </code></pre>
  
  <p>Here in variable <code>returnvalue</code>, I get such type of error:</p>
  
  <blockquote>
    <p>{"msg":"error","data":"  NA
    &lt;/AgreementNo> NA &lt;/Agreementby>  NA
    &lt;/YearofAgreement>    \t\t        No Data Found\t\t<br>
    &lt;/Bill>\t\t &lt;/xml>"}</p>
  </blockquote>