It’s working fine in my local and in the Test Server. But upon deploying this in Production server.
I’m getting the Internal Server Error 500 with this function.
All the function works fine except for this one.
Function to call the sending of email in the view:
function ProcessSendingEmail(NextMRB, to, originator, owner, probDesc, hold_day, MRB_Type)
{
$.ajax({
type: "POST",
url: '@Url.Action("SendMailToFutureHold", "MRB")',
data: { MRBNumber: NextMRB, MailTo: to, Originator: originator, Owner: owner, ProbDesc: probDesc, hold_days: hold_day, mrb_type: MRB_Type},
success: function (data) {
console.log("Email Sent");
},
error: function (data) {
console.log("Email Not Sent");
}
});
}
Controller
[HttpPost]
public ActionResult SendMailToFutureHold(string MRBNumber, string MailTo, string Originator, string Owner, string ProbDesc, string hold_days, string mrb_type)
{
MailId = Originator + "@domain.com";
DataTable dt = MRB.GetDataForAttachmentForFH(MRBNumber, MailTo, mrb_type);
byte[] bytes;
//dt.Columns.RemoveAt(dt.Columns.Count - 1);
dt.TableName = "MRB_Future_Hold_List_" + MRBNumber;
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename = MRB_Future_Hold_List_" + MRBNumber + ".csv");
Response.ContentType = "text/csv";
Response.Charset = "";
using (StringWriter sw = new StringWriter())
{
int iColCount = dt.Columns.Count;
int iRowcount = dt.Rows.Count;
if (iRowcount != 0)
{
for (int i = 0; i < iColCount; i++)
{
sw.Write(dt.Columns[i]);
if (i < iColCount - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
foreach (DataRow dr in dt.Rows)
{
for (int i = 0; i < iColCount; i++)
{
if (!Convert.IsDBNull(dr[i]))
{
sw.Write(dr[i].ToString());
}
if (i < iColCount - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
}
}
else
{
sw.Write("Something went wrong. Please contact [email protected]");
}
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
using (MemoryStream mStream = new MemoryStream(Encoding.ASCII.GetBytes(sw.ToString())))
{
string messageBody = "<b>Originator :</b> " + Originator.ToUpper() + "<br> <b>Owner :</b> " + Owner.ToUpper() + "<br> <b>Problem Description :</b> <br>" + ProbDesc.ToUpper() + "<br><br><font> Attached file are the list of lots to put on MRB Hold. </font><br><br>";
bytes = mStream.ToArray();
Mail_Service sndmail = new Mail_Service("mailrelay.domain.com", "[email protected]", MailTo, QA_Contact_Email + "," + MailId + ",[email protected]");
sndmail.Send_Mail("LOTS FOR MRB FUTURE HOLD", messageBody, bytes, MRBNumber);
}
Response.Flush();
Response.End();
}
}
return View();
}
If anyone can help me. It is much appreciated. Thanks.
2
Answers
The issue is not with your Javascript function but with your controller method
SendMailToFutureHold
.From stack-error we can read
null reference exception object not set to an instance of an object
Assuming that
DataTable dt = MRB.GetDataForAttachmentForFH(MRBNumber, MailTo, mrb_type);
is correctly initialised( not null) –Response
might not be initialised.The only way to be sure is to do some logging – that is the only way – I am aware – of debugging Production code.
You can try log4net
Basically this issue not related with you
front-end code
. It’s coming from theback-end method
which you consuming inAjax call
.I Would say, use Try and Catch in the .cs method and log the error in catch. Because
500 internal server error
coming due to runtime error in your code.Create you own Logger class, you can implement the
Log4net
in your project and write the log in your server. In this way you can get the answer what reason causing the500 Internal Server
error in yourAjax Response