• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 193
  • Last Modified:

implementing 1-N association

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
  • 3
  • 2
1 Solution
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
emrahtufanAuthor Commented:
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.
emrahtufanAuthor Commented:
any comment?
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 :-)
emrahtufanAuthor Commented:
many thanks james:)
one more question.
in asp.net environment,
how can I fill the array list.
I mean each time I click the button on the asp.net 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now