The EntitySpaces Community

Share and learn about the EntitySpaces Architecture.
Welcome to The EntitySpaces Community Sign in | Join | Help
in
Home Forums Photos

Saving Collection via Webservice

Last post 07-21-2008, 8:39 AM by strattonn. 2 replies.
Sort Posts: Previous Next
  •  07-19-2008, 10:39 PM 10313

    Saving Collection via Webservice

    Not quite sure if I am missing something. Got a web service that receives a new collection of objects. I want to save those to a table. Server and Client running 2007.1.1119.0, I have 2008 installed but this is an older project.

    Client Side Code:

        Private Sub SaveLists(ByVal Contacts As ContactCollection)
    
            Dim wsContacts As wsIonized.IonClientCalls = New wsIonized.IonClientCalls
            Dim xs As New XmlSerializer(GetType(BusinessObjects.ContactCollectionProxyStub))
            Dim contactproxy As ContactCollectionProxyStub = New ContactCollectionProxyStub(Contacts)
            Dim sw As New StringWriter()
            xs.Serialize(sw, contactproxy)
            wsContacts.SaveContacts(sw.ToString())
    
        End Sub
    
    Server side deserializes contacts but when I hit the save I get a cannot insert NULL into column ID. The Contact table has an AutoIncrement PK on Id. If ES takes care of identities why do I get this? Am I on the right path?

    Code:

            [WebMethod(Description = "Return a list of all the customer's contacts")]
            public int SaveContacts(int id, string pwd, string Contacts)
            {
                ContactCollection Addresses = new ContactCollection();
    
                XmlSerializer sf = new XmlSerializer(typeof(ContactCollectionProxyStub));
                StringReader sr = new StringReader(Contacts);
    
                ContactCollectionProxyStub contactproxy =
                    sf.Deserialize(sr) as ContactCollectionProxyStub;
                Addresses = contactproxy.GetCollection();
    
                if (Addresses.Count > 0)
                {
                    Addresses.Save();
                }
                return Addresses.Count;
            }
        }
    
  •  07-20-2008, 5:01 PM 10316 in reply to 10313

    Re: Saving Collection via Webservice

    Okay, I just created three projects, a class library that houses my EntitySpaces Employees object (based on SQL Server's Northwind) and the WebService and Client which share the class library. Here is my web service, note that I'm using ES2008 and used SelectAllExcept() to avoid shipping the photo back and forth):

     

    Code:
    [WebMethod]
    public int SaveEmployees(string employees)
    {
    XmlSerializer sf = new XmlSerializer(typeof(EmployeesCollectionProxyStub));
    StringReader sr = new StringReader(employees);

    EmployeesCollectionProxyStub proxy = sf.Deserialize(sr) as EmployeesCollectionProxyStub;
    EmployeesCollection emps = proxy.GetCollection();

    if (emps.Count > 0)
    {
    emps.Save();
    }
    return emps.Count;
    }

    [WebMethod]
    public string GetEmployees()
    {
    EmployeesCollection coll = new EmployeesCollection();
    coll.Query.SelectAllExcept(coll.Query.Photo);
    coll.Query.es.Top = 3;
    coll.Query.Load();

    // The Generated Proxy class
    EmployeesCollectionProxyStub proxy = new EmployeesCollectionProxyStub(coll);

    // Manually Serialize into string form (we want to deserialize it on the other side)
    XmlSerializer sf = new XmlSerializer(typeof(EmployeesCollectionProxyStub));
    StringWriter sw = new StringWriter();
    sf.Serialize(sw, proxy);

    return sw.ToString();
    }

     

    I added one method in the client that calls the GetEmployees() method and then adds two more Employees to the collection and calls Save, it worked just fine.

     

    Code:
    private void button1_Click(object sender, EventArgs e)
    {
    ESWebService.ServiceSoapClient req = new ESWebService.ServiceSoapClient();
    string emps = req.GetEmployees();



    XmlSerializer xs = new XmlSerializer(typeof(BusinessObjects.EmployeesCollectionProxyStub));
    StringReader sr = new StringReader(emps);


    // Deserialize into our Proxy
    BusinessObjects.EmployeesCollectionProxyStub proxy =
    xs.Deserialize(sr) as BusinessObjects.EmployeesCollectionProxyStub;


    // Ask the proxy for the true Employees object
    EmployeesCollection empColl = proxy.GetCollection();

    Employees emp = empColl.AddNew();
    emp.FirstName = "wsMike";
    emp.LastName = "wsGriffin";


    emp = empColl.AddNew();
    emp.FirstName = "wsMike";
    emp.LastName = "wsGriffin";


    proxy = new BusinessObjects.EmployeesCollectionProxyStub(empColl, true);

    XmlSerializer sf = new XmlSerializer(typeof(BusinessObjects.EmployeesCollectionProxyStub));

    StringWriter sw = new StringWriter();
    sf.Serialize(sw, proxy);


    string packet = sw.ToString();


    req.SaveEmployees(packet);
    }

     

    Maybe you can compare my code against yours. I did use ES2008 but I don't think that should matter. I had the first two checkboxes checked on the "Proxy / Stub" tab when I generated the classes.

     


    EntitySpaces | Twitter | BLOG | Please honor our Software License
  •  07-21-2008, 8:39 AM 10317 in reply to 10316

    Re: Saving Collection via Webservice

    Thank you, your support is absolutely fantastic and you just crossed 10,000 posts on the forum!

    The resolution was embarrasingly simple, the ID field was not set to an Identity column and the first time through I was not sure if something larger was at play. Nonetheless this post pulls sending, receiving and saving a collection over webservices. Something that I could not find all together in the documentation or your blogs.

View as RSS news feed in XML