Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6875543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:20:38+00:00 2026-05-27T04:20:38+00:00

I’ve been trying to read xml and print it in datagrid and then again

  • 0

I’ve been trying to read xml and print it in datagrid and then again on clicking save write it back to the same file so that if i open it after sometime, i can have the new files. So, this is what I did:

  DataSet ds;
private void Form2_Load(object sender, EventArgs e)
{
    cmd = new SqlCommand("getCustomers", conn);
    cmd.CommandType = CommandType.StoredProcedure;
    da = new SqlDataAdapter(cmd);
    ds = new DataSet();
    //da.Fill(ds, "Productslist");
    ds.ReadXml(@"C:\Users\Nishanth\documents\visual studio   
        2010\Projects\Ex1\Ex2\ShoppingCart1.ds");
    dataGridView1.DataSource = ds.Tables[0];
}

So,here i read from xml and assign it a grid. In the next few lines i write an event when I click save button on the parent mdi form and call the child form’s writeX method.

public void writeX()
{
    MessageBox.Show("I'm in writeX()");

    ds.WriteXml(@"C:\Users\Nishanth\documents\visual studio           
        2010\Projects\Ex1\Ex2\ShoppingCart1.ds");
}

here, at WriteXml step, I get and error saying

   Null Reference Exception : Object reference not set to an instance of an object.

Parent form code

    private void customer_clicked(object sender, EventArgs e)
    {
        Form2 f2 = new Form2();
        f2.MdiParent = this;

        f2.MaximizeBox = true;

        f2.Show();
    }

    private void products_clicked(object sender, EventArgs e)
    {

        Form1 f = new Form1();
        f.MdiParent = this;

        f.MaximizeBox = true;

        f.Show();



    }

    private void saveToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2();
        f2.writeX();
    }

So, can u please let me know the mistake i’ve been doing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:20:39+00:00Added an answer on May 27, 2026 at 4:20 am

    At the time you’re calling writeX, ds has not been instantiated. With the code provided, it’s impossible to tell why that’s the case.

    It’s most likely you have two ds‘s declared in different scopes.

    EDIT: Given the updated example shows that it’s not an issue of ds‘s scope, the next thing to look for is the lifetime of the Form2 object. It’s a likely two different instances are in use when NullReferenceException occurs. That is, a second instance was likely created but never shown, the Load event never firing, and ds never getting instantiated.

    EDIT: It’s obvious now. You’re not using the same Form2 in saveToolStripMenuItem_Click as you are in customer_clicked.

    You’ll need a class-level Form2 instance.

    Form2 _form2;
    private void customer_clicked(object sender, EventArgs e)
    {
        _form2 = new Form2();
        _form2.MdiParent = this;
    
        _form2.MaximizeBox = true;
    
        _form2.Show();
    }
    
    private void saveToolStripMenuItem_Click(object sender, EventArgs e)
    {
        // a NullReference is still possible if this is called before customer_clicked
        _form2.writeX();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
In my XML file chapters tag has more chapter tag.i need to display chapters
I am trying to render a haml file in a javascript response like so:
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm parsing an XML file, the creators of it stuck in a bunch social
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.