7 Replies - 2848 Views - Last Post: 07 March 2013 - 01:33 PM Rate Topic: -----

#1 Delta7428  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-August 08

object reference not set to an instance of an object

Posted 06 March 2013 - 12:25 PM

Need to update dataset from changes made in datagrid. Getting "object reference not set to an instance of an object" on the DataAdapter update command.
    public partial class Form1 : Form
    {
        OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Apps\ServiceManagement\Data\ServiceManagement.MDB");
        OleDbCommand com;
        OleDbDataAdapter ada;
        DataSet ds = new DataSet();

        public Form1()
        {
            InitializeComponent();
            conn.Open();
            OleDbCommand com = new OleDbCommand("SELECT * FROM Customers", conn);
            OleDbDataAdapter ada = new OleDbDataAdapter(com);

            ada.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0]; 
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OleDbCommandBuilder com = new OleDbCommandBuilder(ada);
            DataSet changes = new DataSet();
            changes = ds.GetChanges(DataRowState.Modified); //here i get the error
            ada.Update(changes.Tables[0]);
        }


This post has been edited by Skydiver: 06 March 2013 - 12:29 PM
Reason for edit:: Put code in code tags. Learn to do this yourself.


Is This A Good Question/Topic? 0
  • +

Replies To: object reference not set to an instance of an object

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3320
  • View blog
  • Posts: 11,229
  • Joined: 12-December 12

Re: object reference not set to an instance of an object

Posted 06 March 2013 - 12:36 PM

You are (re)-defining/declaring the data adaptor in the form's constructor:

 OleDbDataAdapter ada = new OleDbDataAdapter(com);

so the class-instance of this variable remains un-initialized. (The instance that you create in the constructor does not carry across to the click event.)

In the constructor you might use:

ada = new OleDbDataAdapter(com);

This post has been edited by andrewsw: 06 March 2013 - 12:36 PM

Was This Post Helpful? 2
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5465
  • View blog
  • Posts: 11,740
  • Joined: 02-June 10

Re: object reference not set to an instance of an object

Posted 06 March 2013 - 01:47 PM


tlhIn`toq's FAQ list

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated Feb 2013
Spoiler



Was This Post Helpful? 2
  • +
  • -

#4 Delta7428  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-August 08

Re: object reference not set to an instance of an object

Posted 07 March 2013 - 08:51 AM

Thanks. I changed the constructor as suggested.

Now I get "No value given for required parameters" on the update command. I added "GetUpdateCommand" to the command builder after further research which led me to msdn article (link below). But didn't help.

http://msdn.microsof...andbuilder.aspx

  public partial class Form1 : Form
    {
        OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Apps\ServiceManagement\Data\ServiceManagement.MDB");
        OleDbCommand com;
        OleDbDataAdapter ada;
        DataSet ds = new DataSet();

        public Form1()
        {
            InitializeComponent();
            OleDbCommand com = new OleDbCommand("SELECT * FROM Customers", conn);
            
            //OleDbDataAdapter ada = new OleDbDataAdapter(com);
            ada = new OleDbDataAdapter(com);
            
            conn.Open();

            ada.Fill(ds);
            
            dataGridView1.DataSource = ds.Tables[0]; 
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OleDbCommandBuilder cb = new OleDbCommandBuilder(ada);
            cb.GetUpdateCommand();
            DataSet changes = new DataSet();


            if (ds != null && ds.Tables[0] != null && ds.HasChanges(DataRowState.Modified))
            {
                DataTable dt = ds.Tables[0].GetChanges(DataRowState.Modified);
                ada.Update(dt); //Error occurs here: "No value given for required parameters"
            } 


Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3320
  • View blog
  • Posts: 11,229
  • Joined: 12-December 12

Re: object reference not set to an instance of an object

Posted 07 March 2013 - 09:31 AM

Perhaps the DataSet has changes but Tables[0] doesn't? Dunno.

You should gain some debugging skills: refer to tlhIn`toq 's recent post, or use the link in my signature.

This post has been edited by andrewsw: 07 March 2013 - 09:33 AM

Was This Post Helpful? 1
  • +
  • -

#6 Delta7428  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-August 08

Re: object reference not set to an instance of an object

Posted 07 March 2013 - 12:16 PM

Got it.

When I changed the select to select from another table in the database, it worked. So I knew it had to be an issue with the Customer table.

One of the field names in the customer table had a slash in the name ("County/Region"). When I removed the slash in the field name, presto chango, the update works!

Thanks.
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3320
  • View blog
  • Posts: 11,229
  • Joined: 12-December 12

Re: object reference not set to an instance of an object

Posted 07 March 2013 - 12:51 PM

If you needed to keep the slash, or other characters, in the field name then you could use square brackets: "[County/Region]". It would be my preference, though, to remove all these odd characters.
Was This Post Helpful? 1
  • +
  • -

#8 Delta7428  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-August 08

Re: object reference not set to an instance of an object

Posted 07 March 2013 - 01:33 PM

My preference is to remove them as well. I would never use them to begin with. Nor spaces.

I inherited the field name from partially cloning some of the table structures from a MS Access sample app. Ha.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1