Reputation: 798
I am creating a signup page and the file used to store the user details via regiration form is XML file. When I am writing the entries using the code below the system is throwing an exception of NullReferenceException.
protected void register_Click(object sender, EventArgs e)
{
try
{
//var path = Path.Combine(Request.PhysicalApplicationPath, "App_Data\\PageData.xml");
XDocument doc = XDocument.Load(Server.MapPath("~/App_Data/userlogs.xml"));
XElement user = new XElement("user",
new XElement("fname", fname.Text.ToString()),
new XElement("lname", lname.Text.ToString()),
new XElement("dob", dob.Text.ToString()),
new XElement("uid", uid.Text.ToString()),
new XElement("pwd", pwd.Text.ToString()),
new XElement("email", email.Text.ToString()),
new XElement("lastlog", System.DateTime.Now.ToString())
);
doc.Root.Element("users").Add(user);
doc.Save("userlog.xml");
}
catch (Exception exe)
{
error.Visible = true;
error.Text = exe.ToString();
}
}
My userslog.xml File format:-
<users>
<user>
<fname>abc</fname>
<lname>xyz</lname>
<dob>MM/DD/YYYY</dob>
<uid>username</uid>
<pwd>***</pwd>
<email>[email protected]</email>
<lastlog>DATE:TIME</lastlog>
</user>
</users>
By this code i want to create new tag
<users>
<user>
<fname>abc</fname>
<lname>xyz</lname>
<dob>MM/DD/YYYY</dob>
<uid>username</uid>
<pwd>***</pwd>
<email>[email protected]</email>
<lastlog>DATE:TIME</lastlog>
</user>
<user>
<fname>bcd</fname>
<lname>lmo</lname>
<dob>MM/DD/YYYY</dob>
<uid>username1</uid>
<pwd>***</pwd>
<email>[email protected]</email>
<lastlog>DATE:TIME</lastlog>
</user>
</users>
So it want my code to make my file run in the following desired manner.
Upvotes: 1
Views: 1003
Reputation: 798
This is the final working code: thanks to @sudhakar
protected void register_Click(object sender, EventArgs e)
{
//var path = Path.Combine(Request.PhysicalApplicationPath, "App_Data\\PageData.xml");
File.Exists("~/App_Data/userlogs.xml");
{
XDocument doc = XDocument.Load(Server.MapPath("~/App_Data/userlogs.xml"));
XElement user = new XElement("user",
new XElement("fname", fname.Text.ToString()),
new XElement("lname", lname.Text.ToString()),
new XElement("dob", dob.Text.ToString()),
new XElement("uid", uid.Text.ToString()),
new XElement("pwd", pwd.Text.ToString()),
new XElement("email", email.Text.ToString()),
new XElement("lastlog", System.DateTime.Now.ToString())
);
doc.Root.Add(user);
doc.Save(Server.MapPath("~/App_Data/userlogs.xml"));
}
}
Upvotes: 0
Reputation: 4919
Try debugging the program.
Remove the try catch part, so that the program will flow the exception
Add a breakpoint to see how the program runs
Try these:
App_Data/userlogs.xml
exists?Post more code if possible
Upvotes: 0
Reputation: 26209
from the code given there can be one possibility for getting NullReferenceException
1.Please check wether your xml file is available or not in following path:
~/App_Data/userlogs.xml
--> your file should be placed in RootFolder of Project/App_Data/
before proceeding further you can Check wether file exists or not by:
System.IO.File.Exists(filepath)
{
//true so file exists
//contine
}
Upvotes: 1