David K

David K

  • NA
  • 3
  • 1.9k

InsertOnSubmit() method throws Null reference exception ...

Apr 5 2014 10:30 PM

Here's my DataContext Class...

 

public class Dealer : DataContext
    {

        public Table<Vehicle> Vehicles;
        public Table<Customer> Customers;
        {
            get { return this.GetTable<Customer>(); }
        }
        public Table<Account> Accounts;
        public Table<Transaction> Transactions;
        public Dealer(string connection) : base(connection) { }
    }

Here's the Customer Class...

[Table(Name="Customers")]

        

    public class Customer
    {
        [Column(IsPrimaryKey = true, DbType = "int NOT NULL IDENTITY", IsDbGenerated = true, CanBeNull = false)]
        public int CustomerID
        {
            get;
            set;
        }

        [Column(CanBeNull = false)]
        public string FirstName
        {
            get;
            set;
        }

        [Column(CanBeNull = false)]
        public string LastName
        {
            get;
            set;
        }

        [Column(CanBeNull = false)]
        public string SSN
        {
            get;
            set;
        }

        public override string ToString()
        {
            return string.Concat(this.FirstName, " ", this.LastName, " ", this.SSN);
        }


        private EntitySet<Vehicle> vehicles = null;
        [Association(Storage = "vehicles", OtherKey = "CustomerID", ThisKey = "CustomerID")]
        public EntitySet<Vehicle> Vehicles
        {
            get { return this.vehicles; }
            set { this.vehicles.Assign(value); }
        }

        //implement INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

Here's the code that throws the null reference exception

private void Button_Click(object sender, RoutedEventArgs e)
        {
            Customer c = new Customer() { FirstName=txtbFirstName.Text, LastName = txtbLastName.Text, SSN = txtbSSN.Text };
            Dealer d = new Dealer(App.connectionString);
            d.Customers.InsertOnSubmit(c);         ..............................// throws null reference exception.!!!
            try
            {
                d.SubmitChanges();
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message);

            }

I have googled for many hours now and i cann't figure out why its throwing null reference exception...  (found alot of other posts but non of the solutions seem to work for me ) 

please help ... 

thanks in advance.



Answers (3)