1

i have web service in php which contain method (login) that should return false or associated array !

iam trying to use it in c# , but c# keep telling me that i must use object to handle returned data and so i did , but i cant parse anything of data

this is a glimpse of code

        object user;
        user = Chat.login(txtUsername.Text,txtPassword.Text);

        if (user.Equals(false))
        {
            MessageBox.Show("Error !");
        }
        else {
           //i wanna here for example show user['name'] for example
        }

the above code works fine if user entered wrong data , and if it's in system it return mixed data (array of user data) but i can't use it

6
  • What sort of data in what format does the PHP service return? JSON? Please show the raw output that you are trying to parse. Commented Jul 6, 2012 at 16:04
  • Fire up a debugger and hover over the user when the login was successful. What type is it of? Commented Jul 6, 2012 at 16:05
  • you know webservice return xml data Commented Jul 6, 2012 at 16:05
  • It sounds like you've implemented the php webservice yourself, in which case you should return a fixed type or null, rather than false. (I think c# will handle/understand null). Commented Jul 6, 2012 at 16:05
  • @BobDavies i use zend web service tool and it works very fine with all other data types that is not array ? Commented Jul 6, 2012 at 16:08

1 Answer 1

1

You should rewrite the webservice so that it returns an object instead of two different types. In JSON it should look like,

{"status": "SUCCEDED", "result": [1,2,3,4]} // valid login
{"status": "FAILED"} // valid login

Then you can check if status is "SUCCEDED".

Another approach is to return an array. On success return associative array. On failure return an empty array. This allows you to check the length of the array to determine the success.

[1,2,3,4] // valid login
[] // valid login

The example is in JSON. You can convert it to XML anyway. Most important thing is that you should have got the idea.

Sign up to request clarification or add additional context in comments.

4 Comments

the problem is i want to parse this object to extract data from it,
as you mentioned now it returns this {"id":"4","u_name":"samymassoud","u_password":"47dffd4ae5531f11cc791a0748ef2f3edc4aea93","u_email":"[email protected]","u_mobile":"111111","u_blocknumber":"1249","is_active":"1","u_type":"1","u_date":"2011-08-05","u_time":"1312567660","last_time":"11:07 AM","last_date":"2012-07-06","project":"0","last_login":""} i want to deal with this in c# how can i do ?
@SamyMassoud You can use JSON.NET to parse JSON. See (the only answer to this question)[stackoverflow.com/questions/9963079/…
thank you, i figured out a solution based on your answer so i will mark it as best answer :D but iam troubleshooting an other thing is A connection that was expected to be kept alive was closed by the server i will search for it ,and if i can't find answer i will post it here , so if have any suggestions don't hist-ate to answer me ,Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.