-4

I want to get 2 different column values from Country Master table in JSON format in C#. While serializing it, I am getting an error "Object reference not set to an instance of an object." Here is my code as follows:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MDC_web.JSONs
{
    public partial class jsonCountry : System.Web.UI.Page
    {
        string countryname;
        int countryid;
        protected void Page_Load(object sender, EventArgs e)
        {
            countryname = Request.Form["CountryName"];
            countryid = Convert.ToInt32(Request.Form["CountryId"]);
            string jsonOutput = null;
            RegistrationDataClassesDataContext country = new 
            RegistrationDataClassesDataContext();
            var CountryID = (from coun in country.tbl_CountryMasters where 
            coun.CountryId == countryid select 
            coun.CountryId).SingleOrDefault();
            var CountryName = (from coun in country.tbl_CountryMasters where 
            coun.CountryName == countryname select 
            coun.CountryName).SingleOrDefault();
            //var CountryDetails = (from coun in country.tbl_CountryMasters 
            where coun.CountryId == countryid && coun.CountryName == 
            countryname select new {coun.CountryId, coun.CountryName 
            }).ToList() ;
            MemoryStream str = new MemoryStream();
            DataContractJsonSerializer serCountryId = new 
            DataContractJsonSerializer(CountryID.GetType());
            DataContractJsonSerializer serCountryName = new 
            DataContractJsonSerializer(CountryName.GetType());
            //DataContractJsonSerializer serCountryDetails = new 
            DataContractJsonSerializer(CountryDetails.GetType());
            //serCountryDetails.WriteObject(str, CountryDetails);
            serCountryId.WriteObject(str, serCountryId);
            serCountryName.WriteObject(str, serCountryName);
            str.Position = 0;
            StreamReader sr = new StreamReader(str);
            jsonOutput = sr.ReadToEnd();
            //jsonOutput = @"{""Success"":""True"", ""Country ID"":'"+serCountryId+"', ""Country Name"":'"+serCountryName+"'}";

            //jsonOutput = @"{""Success"" : ""True"", ""CountryID"" :  "' + CountryID + '"     +""','""+      ""Country Name"" : '" + CountryName + "'}"; 
            Response.Write(jsonOutput);
        }
      }
    }
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – croxy Apr 23 '18 at 11:37
  • 3
    we don't have the JSON, we don't have your types that you're serializing/deserializing, and you haven't told us which line it fails on... it is going to be very hard for anyone to make a guess here. And NRE is a very common error, and @croxy has linked a good walkthrough of where to start – Marc Gravell Apr 23 '18 at 11:37
  • Thank you @MarcGravell for your reply. I have solved the issue. – Aniruddha Nath Apr 23 '18 at 12:18

1 Answers1

0

I think you can has using https://www.newtonsoft.com/json.It framework will do all the work for you You must first create a model your Json object and using this code:

var data = JsonConvert.DeserializeObject<YourModel>(json);