2009-05-06 3 views
1

私はASP.NETプロジェクトでLINQ to SQLを使用しています。テーブルを挿入する際に、値を特定のテーブルオブジェクトに変換する必要があり、挿入する必要があります。どのようにlinqクラス(テーブル)のパラメータを持つ別のコンストラクタを追加する

私は、そのテーブルオブジェクトに値を割り当てることができるように、そのテーブルに新しいコンストラクタを作成しましたが、割り当ては機能していますが、挿入中に(obj.TS_Questions.InsertOnSubmit(mytableobject);)null例外が発生します。

私のコード::私が

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) –

答えて

2

は正直なところ、私はあまりにも深く見ていないこの問題から私を助けてください。しかし、OnCreatedが少し遠くに北に座っているように見えます...あなたはおそらくあなたがあなたの変数。それ以外の場合は、コンストラクタを呼び出すメソッドのすべてを適切に初期化していることを確認してください。

2

あなたは、このようなデフォルトのコンストラクタを呼び出すことができ、それは私のために正常に動作します:

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

これはトリックを行う場合は、C#のhereにcontructorsの使用についての詳細を読むことができます。

+0

大きな解決策! –

関連する問題