0

I have ASMX webservice and I want to return the result in JSON format. Its working fine in localhost but in the server it is not working.

In server i am Getting Object reference not set to an instance of an object error. I am New to this PLease help me to solve this error

 [WebService(Namespace = "http://DomainNameHere/Mymis")]
    [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 PMIS_WebService : System.Web.Services.WebService
    {
        //private string cs = ConfigurationManager.ConnectionStrings["ConnectionName"].ConnectionString;
        [WebMethod]
        public void getCapacityBuildings()
        {
            string cs = ConfigurationManager.ConnectionStrings["ConnectionName"].ConnectionString;
            List<CapacityBuildingbel> Trainingss = new List<CapacityBuildingbel>();
            using (SqlConnection con = new SqlConnection(cs))
            {
                SqlCommand cmd = new SqlCommand("spGetTrainingsCapacityBuilding", con);
                cmd.CommandType = CommandType.StoredProcedure;
                con.Open();
                SqlDataReader rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    CapacityBuildingbel Trainings = new CapacityBuildingbel();
                    Trainings.train_program = rdr["Train_Program"].ToString();
                    Trainings.wucs_covered = rdr["WUCS_Covered"].ToString();
                    Trainings.venue = rdr["Venue"].ToString();
                    Trainings.Train_date = Convert.ToDateTime(rdr["date"]);
                    Trainings.Female = Convert.ToInt32(rdr["female"]);
                    Trainings.Male = Convert.ToInt32(rdr["male"]);
                    Trainings.theme = rdr["Theme"].ToString();
                    Trainings.remarks = rdr["Remarks"].ToString();
                    Trainingss.Add(Trainings);
                }
                con.Close();
            }
            JavaScriptSerializer js = new JavaScriptSerializer();
            Context.Response.Write(js.Serialize(Trainingss));
        }

error i Am Getting enter image description here My WebConfig File

<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
   <connectionStrings>
    <add name="ConnectionName"  connectionString="Data Source=MyDATASOURCE;Initial Catalog=USERNAME;user id=PASSWORD; password=PASSWORD;" providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <system.web>
   <machineKey 
validationKey="D2880E1DCEAC1601B850B247A4385706029F918E90240FA15F98FD1ABCF0E0298E6460B571B06A392B86D0E41773E35BC84096897171C6984FD5D0BA8A6F4AF0"
decryptionKey="7281B4835FB1DC371EC9945A5208CF987EEEE06D04E15A0264B6F3CA12C764B7"
validation="SHA1" decryption="AES"/>
  <customErrors mode="Off" defaultRedirect="apperror.aspx">
          <error statusCode="404" redirect="404.aspx" />
          <error statusCode="500" redirect="500.aspx" />
    </customErrors>

<httpCookies httpOnlyCookies="false" requireSSL="false"/>
<sessionState timeout="20" mode="StateServer">
</sessionState>
    <pages>
      <controls>
        <add tagPrefix="asp" namespace="System.Web.UI.DataVisualization.Charting" assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
        <add tagPrefix="asp" namespace="AjaxControlToolkit" assembly="AjaxControlToolkit" />
      </controls>
    </pages>
    <httpHandlers>
      <add path="Reserved.ReportViewerWebControl.axd" verb="*" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" validate="false"/>
      <add path="ChartImg.axd" verb="GET,HEAD,POST" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
    </httpHandlers>
    <compilation debug="true" targetFramework="4.5">
      <assemblies>
        <add assembly="Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845DCD8080CC91"/>
        <add assembly="Microsoft.ReportViewer.Common, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845DCD8080CC91"/>
        <add assembly="Microsoft.Build.Framework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
        <add assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
        <add assembly="System.Web.Extensions.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      </assemblies>
      <buildProviders>
        <add extension=".rdlc" type="Microsoft.Reporting.RdlBuildProvider, Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"/>
      </buildProviders>
    </compilation>
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <appSettings>
    <add key="ValidationSettings:UnobtrusiveValidationMode" value="None"/>
    <add key="ChartImageHandler" value="storage=memory;timeout=20;"/>
  </appSettings>
  <system.webServer>
      <defaultDocument enabled="true">
 <files>
    <clear/>
    <add value="Login.aspx"/>
 </files>
</defaultDocument>
    <validation validateIntegratedModeConfiguration="false"/>
    <handlers>
      <remove name="ChartImageHandler"/>
      <add name="ReportViewerWebControlHandler" preCondition="integratedMode" verb="*" path="Reserved.ReportViewerWebControl.axd" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"/>
      <add name="ChartImageHandler" preCondition="integratedMode" verb="GET,HEAD,POST" path="ChartImg.axd" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
    </handlers>
    <!--<directoryBrowse enabled="true"/>-->
  </system.webServer>
</configuration>
subramanya46
  • 385
  • 4
  • 9
  • 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) – VDWWD Aug 16 '18 at 10:19
  • Find the detailed error message, read the line number and see where the problem is. We cannot help you with the code you posted because there are a lot of possible points of failure. – VDWWD Aug 16 '18 at 10:21
  • most probably the error would be in while loop when your are casting. Change castings of ToString() to Convert.ToString() and check again. – Dhiren Aug 16 '18 at 10:39
  • I am getting this error only in server , but it is working perfectly in localhost – subramanya46 Aug 16 '18 at 11:08
  • @subramanya4 is the server using the same web.config? or is it being transformed to use a different database? – Aman B Aug 16 '18 at 11:12
  • Same Web.config file. – subramanya46 Aug 16 '18 at 11:14
  • @subramanya4 One suggestion is to replace all `.ToString()` inside while loop by `Convert.ToString()` e.g. change `rdr["Venue"].ToString()` to `Convert.ToString(rdr["Venue"])` so if rdr["Venue"] is null Convert.ToString will not throw object reference error – Mohsin Mehmood Aug 16 '18 at 11:30
  • ok but i have written more than 10 methods all are showing the same error – subramanya46 Aug 16 '18 at 11:38

1 Answers1

0

I Found solution to this problem after doing lot of googling the problem is not with the code it is the web servers . ie You need to start ASP.NET State Service Service and change that service to start automatically in your server then it will work fine

subramanya46
  • 385
  • 4
  • 9