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>