implementing 1-N association

Posted on 2006-04-04
Last Modified: 2012-08-14
I want to recore samo data in database table. Let me say the name of the table as Educaton. Table Educatin has a 1-N relationship with the table Person. To do this, I have create two class as follows.

public Class Person
    //I have create the association as collections
    public System.Collection.ArrayList myEducation=new ArrayList();
    private int educationPointer=0;

    //I have created a method that is used for populating the data list
    public addEducation( Education me)
          myEducation.add (me)

Public Class Education
   //I have a method to save all the Education Information
    public System.Boolean Save()
         //In this method I have called the some method to execute the necessary sql string

For both classes, I have coded the properties and variables but I do not write them here.

My problem is like that;
From now on What should be the coding strategy in order to implement the 1-n association between the Education and Person Class and save the Education information to the database.

Many thanks
Question by:emrahtufan
    LVL 7

    Expert Comment

    I think you mean 1 to n for Person to Education rather than what you aid above. You should have a Save on the Person not the Education Class.

    You would open a transaction and save the person. You would then loop through all the eduction classes and save their details relating to the person. If anything fails then you rollback the transaction, otherwise you commit e.g.

    Public Class Person

        Public Sub Save()

                Dim conn As New SqlConnection

                'do person insert getting back person identifier
                Dim personId as Integer = insertCommand.Execute

                for each e as Eduction in myEducation
                    'do the education insert


            catch ex as Exception

        End Sub

    End Class

    Author Comment

    Thanks for your response culshaja
    Yes you're right.  I mean one person and many education related with the person.
    Your code seems good but actually my user interface application composed of succesive page.
    For example a person log on to the web site and then want to add their education information (high schools, college etc.)
    So I have a person record on the database.
    Person goes to the  AddEducation page and then save his/her education information by button click. What I want to do exactly is that;
    I want to add the education info in the arraylist on de PersonClass and then add the items in the arraylist to the database.

    Hope I can explain the situation.

    Author Comment

    any comment?
    LVL 7

    Accepted Solution

    What you then need to do is just do the same idea with the loopingthrough the arraylist nested witin a transaction. In this case you will alredy have the person Id so you don't need to add it again.

    james :-)

    Author Comment

    many thanks james:)
    one more question.
    in environment,
    how can I fill the array list.
    I mean each time I click the button on the page, the class person is called and the array is created again and again so I cannot make the array persistent on the memory.
    How can I achive the problem?
    Many many thanks

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now