1

I am currently working on a project that involves a register/login form in C#. I am using a flat file (XML) to store the registered information and have used Xml.Linq in C# to do that.

I am currently having trouble with my login validation code as it has now stopped working since I changed my register code to the linq version. The error code I am getting is...

"An unhandled exception of type 'System.NullReferenceException' occurred in WaiterApp.exe Additional information: Object reference not set to an instance of an object.".

I have included my code below, if somebody could help me out with this as I am a newbie with C# and I have tried to debug why this is happening, but having no luck. Thanks :)

Register Code...

XElement xml = new XElement("RegisterInfo",
        new XElement("User",
        new XElement("Data1", nameTextBox.Text),
        new XElement("Data2", emailTextBox.Text),
        new XElement("Data3", passwordTextBox.Text),
        new XElement("Data4", confirmPasswordTextBox.Text)
        )
        );
        xml.Save("data.xml");
        this.Hide();
        Login ss = new Login();
        ss.Show();

Login Code

XmlDocument doc = new XmlDocument();
        doc.Load("data.xml"); //This code will load the Data xml document with the Login details.
        foreach (XmlNode node in doc.SelectNodes("//RegisterInfo"))
        {
            String Username = node.SelectSingleNode("Data1").Value;
            String Password = node.SelectSingleNode("Data3").Value;

            if (Username == nameTextBox.Text && Password == passwordTextBox.Text)
            {
                MessageBox.Show("You have logged in!");
                this.Hide();
                Main ss = new Main();
                ss.Show();
            }
            else
            {
                MessageBox.Show("Error Logging you in!");
            }

Xml script

<?xml version="1.0" encoding="utf-8"?>
<RegisterInfo>
<User>
<Data1>Charlie</Data1>
<Data2>Charlie@gmail.com</Data2>
<Data3>1</Data3>
<Data4>1</Data4>
</User>
</RegisterInfo>
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Paul Abbott Oct 28 '16 at 21:27

1 Answers1

1

You fotgot to specify the User element in your path (your're code):

   doc.Load("data.xml");

    foreach (XmlNode node in doc.SelectNodes("//RegisterInfo"))
    {
        //default xpath will be /RegisterInfo/Data1 and will not find the (Data Element) in (RegisterInfo)
        String Username = node.SelectSingleNode("Data1").InnerTex; // so this will be null
        String Password = node.SelectSingleNode("Data3").InnerTex; // so this will be null

        // can't compare null, so null error will be thrown.
        if (Username == nameTextBox.Text && Password == passwordTextBox.Text)
        {
        }
    }

What you should do is specify the User element:

    doc.Load("data.xml"); 
    foreach (XmlNode node in doc.SelectNodes("/RegisterInfo/User")) //xpath to /RegisterInfo/User
    {
        String Username = node.SelectSingleNode("Data1").InnerTex; // get value of Data1 Value.
        String Password = node.SelectSingleNode("Data3").InnerTex; // get value of Data3 Value.
    }
Timon Post
  • 2,779
  • 1
  • 17
  • 32
  • Hi, thanks for your reply. That has got rid of the error but now it keeps the login failed message keeps popping up even when the credentials are correct. Any reason as to why? – Habeeb Hussain Oct 28 '16 at 21:48
  • try to use node.SelectSingleNode("Data1").InnerTex; instead of Value. this will get the innertext of the element – Timon Post Oct 28 '16 at 21:51