1

I've recently jumped into C# & windows phone apps, and I'm currently working on a user login system. The login, registration etc works with some simple web-calls.

I want to create some global variables; username, email, id and isAdmin. I've used XML.Linq, and I've found the correct information in the XML sheet generated on the webpage.

However, when I try to populate the information from the XML sheet into variables, it return null.

I created a static class with the following content:

public static class userSettings {

    public static string Username { get; set; }
    public static string Email    { get; set; }
    public static int Id          { get; set; }
    public static int IsAdmin     { get; set; }

    public static Boolean getSettings(string username)
    {
        WebClient client = new WebClient();
            client.DownloadStringAsync(new Uri("http://localhost/app/req.php?action=userAdmin&username=" + username));
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
        return true;
    }   
    static void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error == null && !e.Cancelled)
        {
            string registerResponse = (string)e.Result;
            XDocument response = XDocument.Parse(registerResponse);

            foreach (XElement xe in response.Descendants("User"))
            {
                Username    =  (string)xe.Element("Username");
                Email       =  (string)xe.Element("Email");
                Id          =  (int)xe.Element("Id");
                IsAdmin     =  (int)xe.Element("isAdmin"); 
            }
        }

    }
}

When i then call userSettings.Username, the content is simply NULL.

What do I do wrong?

Edit: getSettings is called on the login page.

Best, Nikolaj

  • Well, maybe `xe.Element("Username")` is null? Or maybe your reading `userSettings.Username` before `client_DownloadStringCompleted` is called? (the operation is asynchronous) – Kevin Gosse Feb 14 '13 at 16:52
  • @KooKiz You were indeed right. xe.Element had the correct value, however, after adding if(userSettings.getSettings(username)) userSettings.getSettings(username); it worked! Cheers. Didn't know the script could do that. Thank you! – Nikolaj Jepsen Feb 14 '13 at 17:53

0 Answers0