-1

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

Raktim Biswas
  • 3,833
  • 5
  • 22
  • 29

1 Answers1

0

in SendMail you are referncing object properties of Ticket but you are not creating those or you do not show it. Anyway based on code you posted as soon as you reference ticket.ProblemType.ProblemDesc and ticket.Urgency.Description you will get null reference exception.

vlscanner
  • 438
  • 5
  • 16
  • Ok, that makes sense, because before it goes into the CreateTicket() method which sends the new ticket back to my database, once it returns to the call the ticket then has the correct links to the other objects, Urgency and ProblemType. I think i know how to fix this. Thanks for the brain bump! – PnkFld7892 Aug 26 '16 at 13:34