Complex MJoining across Databases

Complex MJoining across Databases

EisenkieferEisenkiefer Posts: 12Questions: 2Answers: 0

Hi,

In our current use-case I am trying to create a more complex MJoined table.

The structure is pretty much straight forward to the documentation, sample code is posted below.

Actually what I am trying to achieve is, to get a select/dropdown for TableD.ColD (foreign keys saved in TableC).
And a second select/dropdown for TableF.ColE - depending on the chosen value fir TableD.ColD.

Using a simple LeftJoin, and one MJoin works like a charm.

Adding the second MJoin causes it to break, using the TryCatch i was able to get a little more detailed error message:
"Object reference not set to an instance of an object."
(maybe because TableD.ID which is needed for the Join only exists in another block?)

Also I noticed that MJoining a table from another database does not work (TableD and TableF are supposed to be in another database than TableA and TableB moving them to the same database is currently a workaround, at least for development).
I tried this (DatabaseName.TableName, and DatabaseName.SchemaName.TableName): https://datatables.net/forums/discussion/21012/editor-1-3-1-connect-to-multiple-databases

Here's some code:

Controller:

 public ActionResult MyEditor()
        {
            var settings = Properties.Settings.Default;
            var formData = HttpContext.Request.Form;

            using (var db = new Database(settings.DbType, ConnectionString)
            {
                   var response = new Editor(db, "TableA", "TableA.ID")
            .TryCatch(false)
                        .Model<TableABModel>()
                        .Field(new Field("TableA.ID").Set(false))
                        .Field(new Field("TableB.ID").Set(false))
                        .LeftJoin("TableB", "TableB.ID", "=", "TableA.B_FK")
                        .MJoin(
                            new MJoin("TableD")
                            .Model<TableDModel>()
                            .Link("TableA.ID", "TableC.A_FK")
                            .Link("TableD.ID", "TableC.D_FK")
                            .Field(new Field("ID")
                                .Options("TableD", "ID", "ColD")
                            )                                        
                        )
                        .MJoin(
                            new MJoin("TableF")
                            .Model<TableEModel>()
                .Link("TableD.ID", "TableE.D.FK")
                            .Link("TableF.ID", "TableE.F_FK")
                            .Field(new Field("ID")
                                .Options("TableF", "ID", "ColE")
                            )
                        )
                   .Process(formData)
                   .Data();

                    JavaScriptSerializer js = new JavaScriptSerializer();
                    js.MaxJsonLength = Int32.MaxValue;
                    ContentResult result = new ContentResult
                    {
                        Content = js.Serialize(response),
                        ContentType = "application/json"
                    };
                    return result;
            }
        }

TableABModel:

public class TableABModel : DataTables.EditorModel
    {
 
        public class TableA
        {
            public int ID { get; set; }
            public string B_FK { get; set; }
            public string ColA{ get; set; }
            public string ColB{ get; set; }
        }
        public class TableB
        {
            public int ID { get; set; }
            public string ColC{ get; set; }
        }
}

TableDModel:

public class TableDModel : DataTables.EditorModel
   {
       public int ID { get; set; }
       public string ColD{ get; set; }
   }

TableFModel:

public class TableFModel : DataTables.EditorModel
   {
       public int ID { get; set; }
       public string ColE{ get; set; }
   }

Shouldn't be that big of a deal I guess.

Cheers,
Eisenkiefer

Answers

  • allanallan Posts: 63,195Questions: 1Answers: 10,412 Site admin

    Hi,

    What version of the DataTables.dll are you using? Is it 1.6.x?

    When the exception occurs, does it give any further information about the error - ideally where it is occurring in the DataTables.dll code?

    What stands out for me is:

    "TableE.D.FK")

    Should that be:

    "TableE.D_FK")
    

    ?

    Having said that, I don't think that will fix the issue. It looks like you are trying to Mjoin off an Mjoin. Is that correct? I'm afraid that isn't something that the DataTables.dll currently supports.

    Allan

  • EisenkieferEisenkiefer Posts: 12Questions: 2Answers: 0
    edited December 2016

    Hi,

    I'm pretty sure, that was just a typo in the above sample.

    The Editor.dll we use is version 1.5.6.
    The only thing i can read by from the exception, is that it is related to the MJoin.

    Yeah exactly, I am trying to Mjoin off an Mjoin.
    When can I expect this feature to be developed?

    What I actually trying to do is:
    Your Table shows distinct entries from TableA,
    and shows the MJoined Values from TableD comma-separated,
    there might be multiple values stored in DB,
    but as soon as you change it, it might only be one (would make this a LeftJoin then, the field type a select / dropdown).
    The values of TableF are shown next to these of TableD, behaving most likely the same, but the options of the select / dropdown should depend on the made selection on TableD's column.

    Or a little more easy explained, it's like selecting subcategories:

    TableD -> TableF

    Book -> Scientific
    -> Thriller

    News -> Regional
    -> National
    -> International

    Is there any way to achieve this behaviour?

    Eisenkiefer

This discussion has been closed.