Thursday, May 5, 2011

How To add another constructor with parameter in linq class(Table)

Hi, I am using LINQ to SQL in an ASP.NET project. While inserting the table I need to convert the values to the particular table object and I need to insert.

For that I created a new constructor in that table with parameter so that I can assign my value to that table object , the assign the functionality is working but while inserting (obj.TS_Questions.InsertOnSubmit(mytableobject);) I get null exception.

my code::

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();
     }


constructor created by me

    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;

    }

Please help me out from this problem

From stackoverflow
  • Honestly, I haven't looked too deep, but it looks like that OnCreated is sitting a little far north... You probably want to call it after you're done setting up your variables. Other than that i'd say make sure you're properly initializing everything in the method calling the constructor.

  • You can call default constructor like this, it works fine for me:

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

    If this do the trick, you can read more about using contructors in C# here.

    FreshCode : Great solution!

0 comments:

Post a Comment