Menu

problem with sql join

Help
ejlint
2006-10-04
2013-03-22
  • ejlint

    ejlint - 2006-10-04

    Hi all.  I'm new to C# and new to ORM.NET.  I'm having a problem trying with datagrids that I'm hoping somebody can help out with.  I've gone through the documentation on the site, but am still stuck.  What I'm trying to do is have a textbox that the user can enter a number into.  An event is generated when the users tabs out of the textbox.  The event will create a datagrid based upon whatever the user entered into the textbox.  I want the datagrid to display 7 columns of information: LAB NUMBER, LAST NAME, FIRST NAME, MIDDLE NAME, DATE OF BIRTH, SOCIAL SECURITY NUMBER, and SAMPLE ID.  There are two tables that contain this data: SAMPLE_MAIN and SAMPLE_PERSON.  SAMPLE_MAIN contains LAB NUMBER, DATE OF BIRTH, SOCIAL SECURITY NUMBER, and SAMPLE_ID.  SAMPLE_PERSON contains LAST NAME, FIRST NAME, MIDDLE NAME, and SAMPLE ID.  There are other columns in both SAMPLE_MAIN and SAMPLE_PERSON, but these are the only columns I want to display in the datagrid.
    <p>
    I set up a datagrid in the .NET's designer and it is working.  Here is the query I want to perform:
    <p>
    <b>SELECT *
    <b>FROM SAMPLE_MAIN m, SAMPLE_PERSON p
    <b>WHERE m.SAMPLE_IF = p.SAMPLE_ID
    <b>AND m.LAB_NUMBER LIKE '%labNumTextBox%'
    <p>
    For the ORM.NET implementation, here's what I've got:
    <p>
    <b>dm.QueryCriteria.Clear();
    <b>dm.QueryCriteria.And(JoinPath.SAMPLE_PERSON.SAMPLE_MAIN.Columns.LAB_NUMBER,    labNumTextBox.Text, MatchType.Partial);
    <b>samplePerson = dm.GetSAMPLE_MAINCollection();
    <b>dataGrid1.DataSource = samplePerson
    <p>
    The problem is that this will only display the values that are in the SAMPLE_PERSON table - i.e. 4 of the 7 values I want.  In fact, the three columns exclusive to SAMPLE_MAIN o not even appear in the datagrid.  I modified the code so that SAMPLE_MAIN would be the root instead of SAMPLE_PERSON and I experience a similar result - I got the 4 columns that are in SAMPLE_MAIN, but not the 3 exclusive to SAMPLE_PERSON. 
    <p>
    If anybody can help, that would be great.  If there is any more info I can provide to help, let me know.  Thanks in advance.

     
    • ejlint

      ejlint - 2006-10-04

      Sorry for the <b> and <p> in above post - I should have read the line about HTML tags just a bit better :).

       
    • Krishan Ariyawansa

      Good day,

      The real issue is with the DataGrid object, not in ORM. I have posted the issue and the answer in the following link. (Navigate to the bottom of the page.)

      http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=94640&SiteID=1

      To cut a long story shirt, you have to expose the properties you want, in the main class. It’s not the best way to do, but sine I have to work with Datagrid and ORM this is what I did.

      If you have create the relationship in the database, and if you have created the business layer Dll by using ORM you’ll get

      SAMPLE_MAIN. SAMPLE_PERSON

      In the Sample_Main class do the following changes to expose the, child properties.

          public class SampleMain: Sample_MainOrmTemplate
          {
              /// <summary>
              /// Constructor must have a row and data context.
              /// </summary>
              internal SampleMain ( DataManager dataContext, DataRow ROW) : base( dataContext, ROW)
              {
                  row = ROW;
              }

              public string SamplePerson_LastName
              {
                  get { return this.SamplePerson.LastName; }
              }

          }

      In the UI, you may use add the column like;

      gidColumn = new DataGridViewColumn();
      gidColumn.CellTemplate = new DataGridViewTextBoxCell();
      gidColumn.SortMode = DataGridViewColumnSortMode.Automatic;
      gidColumn.ValueType = typeof(string);
      gidColumn.HeaderText = "Last name";
      gidColumn.Name = "SamplePerson_LastName";
      gidColumn.DataPropertyName = "SamplePerson_LastName";
      this.dataGridView.Columns.Add(gidColumn);

      Aa a principal I’ve decided only to have one property exposesd this way. if I had to use more property of a single class,just to satisfy the DataGrid I will use direct ADO for that perticular case.

      Hope I explianed how to use DataGrid with ORM for child class properties.

      Cheers
      Krishan Ariyawansa

       
    • ejlint

      ejlint - 2006-10-08

      Hi Krishan - thanks for the reply.  Here is what I tried:

      /* (ORM.NET)
      * AccessorName:SAMPLE_MAIN
      * This is a one time generated class skeleton by ORM.NET.
      * Please add your business logic for the class here.
      * Please do not remove these comments as they are required by ORM.NET to function correctly.
      */

      using System;
      using System.Data;
      using System.Data.SqlClient;
      using System.Data.SqlTypes;

      namespace OMPABiz
      {
          /// <summary>
          /// Wraps a row and it's columns/children/parents
          /// </summary>
          /// <remarks>
          /// This class should not be instantiated directly. See <see cref="DataManager.GetSAMPLE_MAIN"/>.
          /// This class should be customized.
          /// </remarks>
          public class SAMPLE_MAIN : SAMPLE_MAINOrmTemplate
          {
              /// <summary>
              /// Constructor must have a row and data context.
              /// </summary>
              internal SAMPLE_MAIN( DataManager dataContext, DataRow ROW) : base( dataContext, ROW)
              {
                  row = ROW;
              }
                 
             
              public string SamplePerson_LastName
              {
                  get
                  {
                      return this.SAMPLE_PERSON.NAME_LAST;
                  }
              }
                                                          
          }
          /// <summary>
          ///    Holds a colection of SAMPLE_MAINs that can be searched and enemerated.
          /// </summary>
          /// <remarks>
          /// This class should not be instantiated directly. See <see cref="DataManager.GetSAMPLE_MAINCollection"/>.
          /// This class can be customized for more specific filters and finds
          /// </remarks>
          public class SAMPLE_MAINCollection : SAMPLE_MAINCollectionOrmTemplate
          {
         
          }
      }

      When I try this, I get the following error:
      'OMPABiz.SAMPLE_MAIN' does not contain a definition for 'SAMPLE_PERSON'

      I am assuming that in your suggestion of return this.SamplePerson.LastName - it was actually supposed to be the SAMPLE_PERSON.NAME_LAST, because when I try to type in what you wrote, but I get the same error if I use your line of code instead of mine.

      Any idea why I'm getting this error?

      Also, if I tried this line instead of the one above:
      return this.SAMPLE_PERSONs.FindByNAME_LAST(this.ToString());

      When I use this, I get error message:
      Cannot implicitly convert type 'OMPABiz.SAMPLE_PERSON' to 'string'

      I tried the above line because it was the closest thing I could find with .NET autocomplete.

      If the solution is that I need to add a definition for SAMPLE_PERSON to SAMPLE_MAIN, how do I do that?  As always, any help is appreciated. 

       
      • Krishan Ariyawansa

        Good day,

        The code I have given is just a sample. Of cause you have to type your own class name properly.

        It seems that you have not built the ORM objects tree properly. First you have to introduce the table relationship to ORM, Meaning the ER model.

        If you’re your using SQL Server, You may define the ER easily from the Database Diagram node.

        Once you defined your relationship ORM will generate the Object tree accordingly.

        Cheers
        Krishan

         
      • David Parslow

        David Parslow - 2006-10-09

        Because you have a member variable called SAMPLE_PERSONs means that the relationship is established as a one-to-many even if it is really just a one-to-one. Note: the ORM.NET UI has a option to set a one-to-many to be a one-to-one.

        This this.SAMPLE_PERSONs should only contain SAMPLE_PERSON that are related to the SAMPLE_MAIN.

        You can use this.SAMPLE_PERSONs[0] to get the SAMPLE_PERSON if is really an exact one-to-one relationship.  If it is an optional one-to-one you will want to check   this.SAMPLE_PERSONs.Count is greater that zero.
        Also remember to call dm.GetSAMPLE_MAINCollection(FetchPath.SAMPLE_MAIN.SAMPLE_PERSON) or dm.GetSAMPLE_MAIN(FetchPath.SAMPLE_MAIN.SAMPLE_PERSON) to make sure that you retrive that related items

         
        • ejlint

          ejlint - 2006-10-09

          Hey guys - got it working with this:

          public string samplePersonNameLast
          {
             get
             {
                return this.SAMPLE_PERSONs[0].NAME_LAST;
             }
          }

          It's working like a champ right now, but I only have 3 rows of test data in my SAMPLE_MAIN and SAMPLE_PERSON tables.  I will need to add some more records in my SAMPLE_PERSON table that will be used to hold aliases.  We have a column in the SAMPLE_PERSON table that is used to determine if the record is for legal name or an alias.  The alias records will have the same SAMPLE_ID as legal name records, so I have to make sure this will return legal names to the datagrid. 

          You guys have been a big help in pointing me in the right direction!

           

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.