locked
method to add a record RRS feed

  • Question

  • I am trying to add a new Customer object.  I have the methods below.  I am getting an error message about the pCustomerName variable not being passed in.  I assume that my problem is in my customer variable in my javascript.  I'm not quite sure what I am doing wrong, so any suggestions are appreciated.

     

    Wally

     

    Javascript code:

     

    function CreateCustomer()

    {

    var customerName = "Fred";

    var customer = {__metadata: {Type:"Customer" },

    pCustomerName: customerName };

    var req = new Sys.Net.WebRequest();

    req.set_url("CustomerOrderService.svc/AddCustomer");

    req.set_httpVerb("POST");

    req.get_headers()["Accept"] = "application/json";

    req.get_headers()["Content-Type"] = "application/json";

    req.set_body(Sys.Serialization.JavaScriptSerializer.serialize(customer));

    req.add_completed(onComplete);

    req.invoke();

    }

    function onComplete(result)

    {

    alert(result);

    }

     

    .NET Code:

    [WebGet]

    [OnReceiveEntity("Customer")]

    public static int AddCustomer(Entities2 coe, string pCustomerName, ReceiveEntityOperation reo)

    {

    int i = 0;

    if (reo == ReceiveEntityOperation.Insert)

    {

    Customer cust = new Customer();

    cust.CustomerName = pCustomerName;

    coe.AddToCustomer(cust);

    coe.SaveChanges();

    i = cust.CustomerID;

    }

    return (i);

    }

    Sunday, September 30, 2007 6:16 PM

Answers

  • Hi Wally,

     

    Astoria is designed to present a REST interface over your data, so typically we'll handle the interactions, you do not need to write a method; if you write a custom update method you can hook it up as a custom call, but not as a regular HTTP method that the library will understand.

     

    You don't need to define anything on the server (you can inject custom logic using interceptors, check out the "using astoria" document included in the CTP). For now the system is open by default (as we get closer to a production release we'll close the defaults to be safer), so updates should just work without you having to write any code.

     

    So:

    -remove all the code you have on the server

    -change pCustomerName to CustomerName in your javascript (that is, match the shape of your server-side entity in your client-side code)

     

    and then try again, everything should just work.

     

    Pablo Castro

    Technical Lead

    Microsoft Corporation

    http://blogs.msdn.com/pablo

     

    Friday, October 5, 2007 6:39 AM
    Moderator

All replies

  • from what I see there is an issue with customerName variable in js as you don;t have it defined. Please revise if the objects has pCustomerName property. I think you mean CustomerName...

     

    hope this helps

     

    in the meantime I tried to insert new item following the js code from MIX and I wrote following:

     

    Code Block

    function createNewCustomer(contactName, companyName)

    {

    //create new item

    var customer = {__metadata: {Type:"Customer" },

       ContactName: contactName, CompanyName: companyName, ContactTitle:"", Address:"",    City:"", Region:"", PostalCode:"", Country:"", Phone:"", Fax:"" };

    //save item to server using Astoria Service

    var wRequest = new Sys.Net.WebRequest();

    wRequest.set_httpVerb("PUT");

    wRequest.set_url("http://localhost:86/northwind.svc/Customers");

    wRequest.get_headers()["Accept"] = "application/json";

    wRequest.get_headers()["Content-Type"] = "application/json";

    wRequest.add_completed(function (response, eventArgs){

    var statusText = response.get_statusText();

    alert(statusText);

    });

    wRequest.set_body(Sys.Serialization.JavaScriptSerializer.serialize(customer));

    wRequest.invoke();

    }

     

     

    and all I get is Error 500: Internal Service Error.

    Any thoughts are welcome Smile

    Thursday, October 4, 2007 2:21 PM
  • I was able to make it run by simply changing two things:

    • make HTTP verb POST
    • add CustomerID with value.

     

    you can read my blog post How to insert record to DB using Javascript and Astoria Service

     

    Hope this helps.

    Thursday, October 4, 2007 3:39 PM
  •  

    I'm still having problems.  I have the customerName variable defined in js.  I'd like to see your server side method.

     

    I'm getting a http 500 error on not passing the pCustomerName variable to the method.

     

    Wally

    Thursday, October 4, 2007 5:44 PM
  • there is no server-side method. Thus I described in the couple blog posts is all.

    I've changed AjaxNavigator sample and added some custom files.

     

    Do you want me to zip the solution and post it?

     

    BTW When I added all fileds in the Customer entity and set appropriate value for them it was OK for me.

    Thursday, October 4, 2007 6:24 PM
  • Yeah, if you could zip and post it somewhere that I could get it, I would love to see it. 

     

    Wally

     

    Thursday, October 4, 2007 6:35 PM
  • sure Smile

    you can download the very simple project from http://www.galcho.com/Blog/content/binary/AstoriaTests.zip 

     

    Note that in order to make it work you have to:

    • extract files and modify Connection string NorthingEntites in web.config to match your SQL server that run Northwind DB
    • when running the sample make sure the URL in browser's address bar contain same hostname as in astoriaUrl JS variable (Default.aspx line 29) otherwise you will get "Access Denied!" error from browser single origin restriction. 

    Hope this helps

    Thursday, October 4, 2007 7:20 PM
  • Hi Wally,

     

    Astoria is designed to present a REST interface over your data, so typically we'll handle the interactions, you do not need to write a method; if you write a custom update method you can hook it up as a custom call, but not as a regular HTTP method that the library will understand.

     

    You don't need to define anything on the server (you can inject custom logic using interceptors, check out the "using astoria" document included in the CTP). For now the system is open by default (as we get closer to a production release we'll close the defaults to be safer), so updates should just work without you having to write any code.

     

    So:

    -remove all the code you have on the server

    -change pCustomerName to CustomerName in your javascript (that is, match the shape of your server-side entity in your client-side code)

     

    and then try again, everything should just work.

     

    Pablo Castro

    Technical Lead

    Microsoft Corporation

    http://blogs.msdn.com/pablo

     

    Friday, October 5, 2007 6:39 AM
    Moderator