2009-05-06 68 views
1

我在ASP.NET項目中使用LINQ to SQL。插入表時,我需要將值轉換爲特定的表對象,我需要插入。如何添加linq類(表)中的參數的另一個構造函數

爲此,我使用參數在該表中創建了一個新構造函數,以便我可以將該值分配給該表對象,但賦值功能正在工作,但插入時(obj.TS_Questions.InsertOnSubmit(mytableobject);)我得到空異常。

我的代碼::

default constructor for my table 

public TS_Question() 
     { 
      this._TS_Options = new EntitySet<TS_Option>(new Action<TS_Option>(this.attach_TS_Options), new Action<TS_Option>(this.detach_TS_Options)); 
      this._TS_QuestGroups = new EntitySet<TS_QuestGroup>(new Action<TS_QuestGroup>(this.attach_TS_QuestGroups), new Action<TS_QuestGroup>(this.detach_TS_QuestGroups)); 
      this._TS_QuestRecords = new EntitySet<TS_QuestRecord>(new Action<TS_QuestRecord>(this.attach_TS_QuestRecords), new Action<TS_QuestRecord>(this.detach_TS_QuestRecords)); 
      this._TS_Admin = default(EntityRef<TS_Admin>); 
      this._TS_LevelType = default(EntityRef<TS_LevelType>); 
      this._TS_OptionTypeLT = default(EntityRef<TS_OptionTypeLT>); 
      OnCreated(); 
     } 

構造由我創建

public TS_Question(Guid Quest_QuestIDBL, string Quest_NameBL, Nullable<Guid> Quest_OptionTypeIDBL, Guid Quest_AdminIDBL, Guid Ques_LevelIDBL, int Quest_TimeBL, int Quest_MarkBL, string Qest_ExplanationBL, Nullable<bool> Qest_IsMultipleAnswerBL) 
    { 

     this._TS_Options = new EntitySet<TS_Option>(new Action<TS_Option>(this.attach_TS_Options), new Action<TS_Option>(this.detach_TS_Options)); 
     this._TS_QuestGroups = new EntitySet<TS_QuestGroup>(new Action<TS_QuestGroup>(this.attach_TS_QuestGroups), new Action<TS_QuestGroup>(this.detach_TS_QuestGroups)); 
     this._TS_QuestRecords = new EntitySet<TS_QuestRecord>(new Action<TS_QuestRecord>(this.attach_TS_QuestRecords), new Action<TS_QuestRecord>(this.detach_TS_QuestRecords)); 
     this._TS_Admin = default(EntityRef<TS_Admin>); 
     this._TS_LevelType = default(EntityRef<TS_LevelType>); 
     this._TS_OptionTypeLT = default(EntityRef<TS_OptionTypeLT>); 
     OnCreated(); 

     this._Quest_QuestID = Quest_QuestIDBL; 
     this._Quest_Name = Quest_NameBL; 
     if (Quest_OptionTypeIDBL != null) 
     { 
      this._Quest_OptionTypeID = Quest_OptionTypeIDBL; 
     } 
     this._Quest_AdminID = Quest_AdminIDBL; 
     this._Ques_LevelID = Ques_LevelIDBL; 
     this._Quest_Time = Quest_TimeBL; 
     this._Quest_Mark = Quest_MarkBL; 
     this._Qest_Explanation = Qest_ExplanationBL; 
     this._Qest_IsMultipleAnswer = Qest_IsMultipleAnswerBL; 

    } 

請幫我從這個問題

+0

可能的重複[什麼是.NET中的NullReferenceException?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net) – 2012-05-28 12:57:11

回答

2

老實說,我還沒有看太深,但它看起來像OnCreated坐在一個很遠的北方......你可能想要在完成設置後調用它變量。除此之外,我會說確保你正在初始化調用構造函數的方法中的所有東西。

2

可以調用默認的構造函數這樣的,它工作正常,我:

public partial class MyClass 
{ 
    public MyClass(string fieldValue1,int fieldValue2) 
     : this() 
    { 
     this.field1= fieldValue1; 
     this.field2 = fieldValue2; 
    } 
} 

如果該做的伎倆,你可以閱讀更多有關在C#中使用here contructors。

+0

偉大的解決方案! – 2010-04-03 15:08:43

相關問題