1

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.

I am getting the following error on debugging

7
  • 1
    What line causes the error? Commented Nov 15, 2013 at 2:52
  • That's what I am trying to figure out . . . Commented Nov 15, 2013 at 2:55
  • There shouldn't be any attempt to figure out. You just do it. When your program throws the error, the IDE will clearly indicate which line it is (execute your program in debug mode). Commented Nov 15, 2013 at 2:56
  • 1
    which line is 31 here? Commented Nov 15, 2013 at 3:03
  • 1
    @Reeyash: And line 31 is...???? Commented Nov 15, 2013 at 3:03

3 Answers 3

1

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
}
Sign up to request clarification or add additional context in comments.

5 Comments

My file is in the RootFolder of the project and it does exist. I checked the same by deleting the file the system. It is throwing File not found exception.
sorry i did not get you, your files is in the RootFolder?
yes . my userslog.xml lies in App_Data directory. and it is correctly linked in the program and normally accessed.
no it is not. .i have added a snapshot in my question of the errors that i am getting. can you check the pic and tell me.
try adding like this => doc.Root.Add(user);
0

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:

  • does the file App_Data/userlogs.xml exists?
  • is the "doc" object null?

Post more code if possible

5 Comments

the file exists. . . . The NullReferenceException is coming on -doc.Root.Element("users").Add(User);
Then did you check whether the "doc" is null or the "User" is null?
or the "doc.Root.Element("user") is null?
my root node is <users>. and user is not null
Try typing "doc.Root.Element("users")" in the immediate window and see what's the output
0

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"));
    }
}

Comments

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.