1

When I POST using my windows form app (second peace of code down) when I go to GET the student collection with buttonclick 2 into my datagrid nothing is showing I can hardcode the members and GET no problem, yet I cant POST? The response I get back says OK in the message box when I click button1 for my post? So not quite sure what I have done...

    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "")]
    void AddStudent(Student student);

            XDocument xDoc = XDocument.Load(uri);
            var students = xDoc.Descendants("Student")
                .Select(n => new
                {
                    StudentNo = n.Element("StudentID").Value,
                    Firstname = n.Element("FirstName").Value,
                    Surname = n.Element("LastName").Value
                })
                .ToList();

            dataGridView1.DataSource = students;
        }
G Gr
  • 6,030
  • 20
  • 91
  • 184
  • Be consistent with naming `StudentID` or `StudentNo`, `FirstName` or `Firstname`, `LastName` or `Surname` – L.B Apr 05 '12 at 20:14
  • hey Denis this is purely for visual representation to the user – G Gr Apr 05 '12 at 20:17
  • Is the `var students ` populated? If so, try using [BindingContext](http://stackoverflow.com/a/3481908/884410) – Raj Ranjhan Apr 05 '12 at 20:20
  • That method doesnt work still the same return of the 3 students. Im not sure if its my addstudent in my operationcontract thats doing it? – G Gr Apr 05 '12 at 20:26
  • In your similar [question](http://stackoverflow.com/questions/10033743/xml-to-listbox-from-webservice/10034789#10034789) I see that your xml's root element is `ArrayOfStudent`. Why do you form your xml manually instead of using an xml parser? what is the use of `Student` class if you don't use it for serializing/deserializing ? – L.B Apr 05 '12 at 20:29
  • Are you sure that you form your xml correctly? Maybe it should have a root tag as `ArrayOfStudent` not `Student` – L.B Apr 05 '12 at 20:35
  • Using string builder to form an xml is a bad idea. Use Xml Parsers like XmlDocument, XDocument etc. – L.B Apr 05 '12 at 20:38
  • You can also use XmlSerializer to serialize your `Student` class to get the xml to post to server. – L.B Apr 05 '12 at 20:39

3 Answers3

1

Are you running in Per Call Activation mode? If so, every client request gets a new dedicated service instance, so your List<> is being recreated as empty every time.

See this reference article. You'll have to persist your List between calls, either in a Cache or Database.

mgnoonan
  • 7,060
  • 5
  • 24
  • 27
  • GOD! That was so much reading to find the answer which ofcourse I knew it would be a one line sort of thing: `[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]` – G Gr Apr 06 '12 at 01:57
0

Your AddStudent is not adding to the list

public void AddStudent(Student student)
{
    student.StudentID.ToString();
    student.FirstName.ToString();
    student.LastName.ToString();
}

And GetStudentCollection is returning the same hard coded values.

static List<Student> students = new List<Student>();
public void AddStudent(Student student)
{
   students.Add(student);

}

public List<Student> GetStudentCollection()
{
    return students;
}
Raj Ranjhan
  • 3,869
  • 2
  • 19
  • 29
  • still nothing returned, no errors either? Btw I had this originally but I also wanted the hardcoded members (for easeness) – G Gr Apr 05 '12 at 20:43
  • Then there may be other issues also. Your `Addstudent` is probably not working correctly. Can you put a breakpoint there and see if is being called with a properly formatted student? – Raj Ranjhan Apr 05 '12 at 20:49
0

Your service is doing exactly what you wrote in code. ListStudents constantly returns the same list of the students, AddStudent do nothing with the list.

paramosh
  • 2,258
  • 1
  • 15
  • 23