Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
C#
it shows error "Object reference not set to an instance of object" i am trying to generate the crystal report by using ajax and jquery in webmethod but method is static hence i cant find the id of crystalreportviewer in webmethod. Hence i use httpcontext method to find the id but its not working.any other way to find the crystalreportviewer in webmethod. How to solve.Thanks in advance


What I have tried:

Quote:
Jquery:- $(document).ready(function () {
$("#btn").click(function () {
var croidforprint = 'SSPL/DEL/LINER/CRO/12-16/002';
$.ajax({
url: "TestCrystallWithAjax.aspx/DOPrint",
type: "Post",
dataType: "json",
data: '{enqcode: ' + JSON.stringify(croidforprint) + '}',
contentType: "application/json; charset=utf-8",
success: function (data) {
alert("Hello");
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
debugger;
alert("Error while retrieving data of :" + textStatus); alert("Error: " + XMLHttpRequest.responseText);
}
});

});
});
HTML:-

<input type="button" id="btn" value="print" />
<cr:crystalreportviewer id="CrystalReportViewer1" runat="server" autodatabind="true">

C# Webmethod:-
[WebMethod]
public static void DOPrint(string enqcode)
{
string cs = ConfigurationManager.ConnectionStrings["DUM01"].ConnectionString;
using (var scon = new SqlConnection(cs))
{
using (var cmd = new SqlCommand())
{
cmd.CommandText = "select CROID,DODate,DOValidity,PrincipleName,AgentName,LoadPort,DischargePort,DeliveryPort,VesselName,VoyageNumber,ETA,ETD,LinerCode,VIANo,ROTNo ,CutOffDate,CutOffTime,VGMDate,VGMTime,AgentCode,BrokerName,YardName,YardAddress1,YardAddress2,YardTelNo,From13Name,From13Address1,Form13Address2,Form13TelNo,CustomerName from CROFormTbl where CROID='" + enqcode + "'";
cmd.CommandType = CommandType.Text;
cmd.Connection = scon;
scon.Open();
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
ReportDocument dopart = new ReportDocument();
string reportPath = HttpContext.Current.Server.MapPath("CrystallReports\\DOReport.rpt");
dopart.Load(reportPath);
dopart.SetParameterValue("Doid", sdr.GetValue(0).ToString());
dopart.SetParameterValue("DODate", sdr.GetValue(1).ToString());
dopart.SetParameterValue("Validity", sdr.GetValue(2).ToString());
dopart.SetParameterValue("PrincipleName", sdr.GetValue(3).ToString());
dopart.SetParameterValue("AgentName", sdr.GetValue(4).ToString());
dopart.SetParameterValue("LoadPort", sdr.GetValue(5).ToString());
dopart.SetParameterValue("DischargePort", sdr.GetValue(6).ToString());
dopart.SetParameterValue("DeliveryPort", sdr.GetValue(7).ToString());
dopart.SetParameterValue("VesselName", sdr.GetValue(8).ToString());
dopart.SetParameterValue("VoyageNumber", sdr.GetValue(9).ToString());
dopart.SetParameterValue("ETA", sdr.GetValue(10).ToString());
dopart.SetParameterValue("ETD", sdr.GetValue(11).ToString());
dopart.SetParameterValue("LinerCode", sdr.GetValue(12).ToString());
dopart.SetParameterValue("VIANo", sdr.GetValue(13).ToString());
dopart.SetParameterValue("ROTNo", sdr.GetValue(14).ToString());
dopart.SetParameterValue("CutOffDate", sdr.GetValue(15).ToString());
dopart.SetParameterValue("CutOffTime", sdr.GetValue(16).ToString());
dopart.SetParameterValue("VGMDate", sdr.GetValue(17).ToString());
dopart.SetParameterValue("VGMTime", sdr.GetValue(18).ToString());
dopart.SetParameterValue("AgentCode", sdr.GetValue(19).ToString());
dopart.SetParameterValue("BrokerName", sdr.GetValue(20).ToString());
dopart.SetParameterValue("YardName", sdr.GetValue(21).ToString());
dopart.SetParameterValue("YardAddress1", sdr.GetValue(22).ToString());
dopart.SetParameterValue("YardAddress2", sdr.GetValue(23).ToString());
dopart.SetParameterValue("YardTelNo", sdr.GetValue(24).ToString());
dopart.SetParameterValue("From13Name", sdr.GetValue(25).ToString());
dopart.SetParameterValue("From13Address1", sdr.GetValue(26).ToString());
dopart.SetParameterValue("Form13Address2", sdr.GetValue(27).ToString());
dopart.SetParameterValue("Form13TelNo", sdr.GetValue(28).ToString());
dopart.SetParameterValue("CustomerName", sdr.GetValue(29).ToString());
if (HttpContext.Current != null)
{
Page page = (Page)HttpContext.Current.Handler;
CrystalReportViewer cry = (CrystalReportViewer)page.FindControl("CrystalReportViewer1");
cry.ReportSource = dopart;
cry.DataBind();
}

}
scon.Close();
}
}
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900