1
當我嘗試使用WP7上的LINQ向表中添加記錄時,我收到NullReferenceException。我對C#/ LINQ相對來說比較新,所以我已經複製了一個可以正常工作的現有方法,但是現在我無法使它適用於新記錄。代碼如下;NullReferenceException將記錄添加到表
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();
}
的這是給我的錯誤代碼行是Category.Add(newCat)
至於我可以告訴一切正常這可能意味着我犯了一個愚蠢的錯誤(再次)。
任何幫助,非常感謝。
表格定義如下;
[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
}
感謝您 - 正是我需要做的。其他房產由我沒有看到的電話初始化。這與VB正在從中遷移的方式不同。 – MAO 2012-02-08 10:00:39