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 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>
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.