locked
Object Reference not set to instance of an object RRS feed

  • Question

  • jquery script is:

                 

    <script>
           $(document).ready(function () {
               $("#LeadForm").submit(function (event) {
                   $.ajax({
                       type: "POST",
                       url: "http://localhost:9135/api/CRMLead",
                       data: $("#LeadForm").serialize(),
                       dataType: 'json',
                       success: function (response) {
                           alert(response);
                           $('#LeadForm').each(function () {
                               this.reset(); //Reset each field
                           });
                       },
                       error: function (request, textStatus, errorThrown) {
                           alert(request.responseText + " " +
                               textStatus + " " + errorThrown);
                       }
                   });

                   event.preventDefault();
            });
           });
        </script>

    controller is:

                    

     public class CRMLeadController : ApiController
        {
            private OrganizationService _orgService;

            [EnableCors(origins: "*", headers: "*", methods: "post")]
            public string Post([FromBody] FormDataCollection formValues)
            {
                string domain = HttpContext.Current.Request.Headers["Origin"].ToLower();
                string host = HttpContext.Current.Request.Url.Host.ToLower();
                //if (!domain.Contains("yourdomain.com") && !domain.Contains(host))
                //    return "fail!";

                CrmConnection connection = CrmConnection.Parse(
                    ConfigurationManager.ConnectionStrings["CRMOL"].ConnectionString);

                using (_orgService = new OrganizationService(connection))
                {
                    Entity lead = new Entity("lead");
                    lead["firstname"] = formValues.Get("FirstName");
                    lead["lastname"] = formValues.Get("LastName");

                    _orgService.Create(lead);
                }
                return "success";
            }
        }
    }

    CRM Connection is:

    <add name="CRMOL" connectionString="AuthType=Office365; Username=xxxx@xxxxxx.onmicrosoft.com; Password=xxxx; Url=https://xxxxx.crm.dynamics.com "/>

    Thanks in advance

    Monday, September 19, 2016 6:33 AM

All replies

  • the error mentions as line 25 in CRMLeadController.cs, can you mention the line 25 in the cs file since the function u pasted doesnt have using block etc.

    If my response answered your question, please "mark the response as an answer" and also "vote as helpful". Regards, Hari. www.crm2011byhari.blogspot.com

    Monday, September 19, 2016 6:56 AM
  • thanks for the quick reply.

    the error is in here :

     string domain = HttpContext.Current.Request.Headers["Origin"].ToLower();

    i do not know what domain i should put  in here:

     [EnableCors(origins: "http://www.xxxxx.com", headers: "*", methods: "post")]

    Monday, September 19, 2016 7:00 AM
  • This should be ok if you are ok to allow all the domains to call this. or you should put the domain where the html page is hosted.

     [EnableCors(origins: "*", headers: "*", methods: "post")]


    If my response answered your question, please "mark the response as an answer" and also "vote as helpful". Regards, Hari. www.crm2011byhari.blogspot.com

    Monday, September 19, 2016 7:10 AM
  • it does not work using the wildcard *
    Monday, September 19, 2016 7:22 AM
  • // Allow CORS for all origins. (Caution!) [EnableCors(origins: "*", headers: "*", methods: "*")]

    Check Here


    If my response answered your question, please "mark the response as an answer" and also "vote as helpful". Regards, Hari. www.crm2011byhari.blogspot.com

    Monday, September 19, 2016 7:26 AM
  • thanks for quickly replying.

    but as mentioned above, the wildcard * does not work and it causes the error "object reference"

    for visual studio api development, i do not know what domain to put. will "http:localhost" while testing and debugging the solution?

    Monday, September 19, 2016 7:33 AM
  • please confirm you have followed all the mentioned steps in the link i have sent because the same works very well for me.

    If my response answered your question, please "mark the response as an answer" and also "vote as helpful". Regards, Hari. www.crm2011byhari.blogspot.com

    Monday, September 19, 2016 9:45 AM
  • sorry, but the error persist.thanks
    Tuesday, September 20, 2016 8:57 AM
  • Can you tell me which object is coming as null in the statement?

    HttpContext.Current.Request.Headers["Origin"]


    If my response answered your question, please "mark the response as an answer" and also "vote as helpful". Regards, Hari. www.crm2011byhari.blogspot.com

    Wednesday, September 21, 2016 5:02 AM