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

  • SEARCH
  • Home
  • 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 7188803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:09:50+00:00 2026-05-28T19:09:50+00:00

I am receiving a NullReferenceException when trying to add a record to a table

  • 0

I am receiving a NullReferenceException when trying to add a record to a table using LINQ on WP7. I am relatively new to C#/LINQ so I have copied one of my existing methods which works OK, but now I cannot make it work for the new record. The code is below;

private ObservableCollection<DBControl.Categories> _category;
    public ObservableCollection<DBControl.Categories> Category
    {
        get
        {
            return _category;
        }
        set
        {
            if (_category != value)
            {
                _category = value;
                NotifyPropertyChanged("Category");
            }
        }
    }


    private void button1_Click(object sender, RoutedEventArgs e)
    {

        string TestCategory = "Cars";

        // Create a new to-do item based on the text box.
        DBControl.Categories newCat = new DBControl.Categories { CategoryDesc = TestCategory };
        //CategoryDesc

        // Add a to-do item to the observable collection.
        **Category.Add(newCat);**

        // Add a to-do item to the local database.
        BoughtItemDB.Category.InsertOnSubmit(newCat);

        BoughtItemDB.SubmitChanges();
    }

The line of code which is giving me the error is Category.Add(newCat)

As far as I can tell everything looks OK which probably means I’ve made a daft mistake (again).

Any help is much appreciated.

The table definition is below;

[Table(Name = "Categories")]
    public class Categories : INotifyPropertyChanged, INotifyPropertyChanging
    {
        // Define ID: private field, public property and database column.
        private int _categoryId;

        [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
        public int CategoryId
        {
            get
            {
                return _categoryId;
            }
            set
            {
                if (_categoryId != value)
                {
                    NotifyPropertyChanging("CategoryId");
                    _categoryId = value;
                    NotifyPropertyChanged("CategoryId");
                }
            }
        }

        // Define item category: private field, public property and database column.
        private string _categoryDesc;

        [Column]
        public string CategoryDesc
        {
            get
            {
                return _categoryDesc;
            }
            set
            {
                if (_categoryDesc != value)
                {
                    NotifyPropertyChanging("CategoryDesc");
                    _categoryDesc = value;
                    NotifyPropertyChanged("CategoryDesc");
                }
            }
        }
        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify the page that a data context property changed
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region INotifyPropertyChanging Members

        public event PropertyChangingEventHandler PropertyChanging;

        // Used to notify the data context that a data context property is about to change
        private void NotifyPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        #endregion
    }
  • 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-28T19:09:51+00:00Added an answer on May 28, 2026 at 7:09 pm

    You need to initialize Category or _category. It’s not getting set to any value, so you’re trying to Add() to a non-initialized object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

while working with linq query i'm receiving error System.NullReferenceException: Object reference not set to
When receiving a bug report or an it-doesnt-work message one of my initials questions
My application is receiving email through SMTP server. There are one or more attachments
Im receiving error C2082: redefinition of formal parameter 'rval' in this code while trying
I am using Subsonic v3.0.0.3 with the Linq templates. I am attempting to update
Receiving the message Invalid argument when using shmget with the second parameter not being
Im receiving this error when trying to compile my code. $ g++ -o BangBangControlTest
Im receiving an XML RSS feed. One of the tags look like this: <georss:point>55.0794503724671
Problem I'm currently trying to setup a Sitecore scheduled task in the database using
i'm receiving a very noticeable performance hit when using TweenLite to tween a filter

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.