I'm revising a help desk program I wrote for work and want to refactor my code so when a ticket get's sent it tries to send out the email first before creating the ticket in my database.
But If i call my SendMail(ticket) method before my context.CreateTicket(ticket) method I get a null ref. exception, even though I declare and init the ticket object before hand.
Here's my code for my NewTicket method
private void CreateNewTicket()
{
//set search filter to currentuser
dS.Filter = "(&(objectClass=user)(anr=" + userName + "))";
//find current user in the acrive director
SearchResult sR = dS.FindOne();
var ticket = new HelpTicket
{
Title = title,
DescText = descText,
Employee = GetProp(sR, "Name"),
EmpEmail = GetProp(sR, "mail"),
DateSubmited = DateTime.Now,
// Urgency = selectedUrgency,
UrgentID = SelectedUrgency.UrgentID,
TypeID = SelectedProblemType.TypeID
};
try
{
//if sendmail here it thorws the exception
//SendMail(ticket);
try {
context.CreateTicket(ticket);
//If I call context.createticket first it works
SendMail(ticket);
CloseDialog = true;
}
catch
(System.Exception ex )
{
MessageBox.Show("Error Submitting ticket: " + ex.Message,"Error Submitting Ticket",MessageBoxButton.OK,MessageBoxImage.Error);
}
}
catch (Exception ex1)
{
MessageBox.Show("Error Submitting ticket, please try again.\n" + ex1.Message,"Error Creating ticket",MessageBoxButton.OK,MessageBoxImage.Error);
CloseDialog = true;
}
}
code for SendMail(ticket)
public override void SendMail(HelpTicket ticket)
{
Microsoft.Office.Interop.Outlook.Application outlook = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.MailItem mailMsg =
(Microsoft.Office.Interop.Outlook.MailItem)outlook.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
Microsoft.Office.Interop.Outlook.Inspector oInspector = mailMsg.GetInspector;
mailMsg.To = "myhelpticketemail@mycompany.com";
mailMsg.Subject = ticket.Title;
mailMsg.HTMLBody = "<b>Urgency: </b>" + ticket.Urgency.Description + "<br/>" +
"<b>Problem Type: </b>" + ticket.ProblemType.ProblemDesc + "<br/><hr/><br/>" +
ConvertToHtml(ticket.DescText);
mailMsg.Send();
}
If you need any more code snippets, let me know and i'll post them