title: How to create a Add function for a record database?
body: <p>In my program, I need to put an add function to insert categories. I'm using a treeView to show the data.</p>
  
  <p><a href="https://stackoverflow.com/questions/7072819/how-can-i-model-this-class-in-a-database/7072841#7072841">How can I model this class in a database?</a></p>
  
  <p><img src="https://i.stack.imgur.com/4jyCs.png" alt="enter image description here"></p>
  
  <p>The user insert the level, and the program must insert that category in that level. But I'm getting tired. Because it needs to check if the others levels exist (IE: The treeView is empty and I want to add 2.1, so it's an error).</p>
  
  <p>Sometimes, you may add an already level setted and so that must be forbidden.</p>
  
  <p>I need a little of help in my code. It's finished, but I want to improve it or fixed errors (if so).</p>
  
  <p>Here is the code:</p>
  
  <pre><code>    private void AddButton_Click(object sender, RoutedEventArgs e)
      {
          NorthwindDataContext cd = new NorthwindDataContext();
  
          int[] levels = LevelTextBox.Text.ToIntArray('.');
          string newName = NameTextBox.Text;
  
          int[] parentLevels = new int[levels.Length - 1];
          Array.Copy(levels, parentLevels, parentLevels.Length);
          Objective current = GetNode(levels);
          Objective parent = GetNode(parentLevels);
  
          if (current != null)
          {
              MessageBox.Show("Level already exists");
              return;
          }
          else if (parent == null &amp;&amp; parentLevels.Length != 0)
          {
              MessageBox.Show("Parent level doesn't exist");
              return;
          }
  
          var newObjective = new Objective();
          newObjective.Name = newName;
          newObjective.Level = levels.Last();
          newObjective.Parent_ObjectiveID = parent == null ? null : (int?)parent.ObjectiveID;
  
          cd.Objective.InsertOnSubmit(newObjective);
          cd.SubmitChanges();
  
          MessageBox.Show("The new objective has added successfully");
          NameTextBox.Clear();
          LoadObjectives();
      }
  
      public Objective GetNode(params int[] indexes)
      {
          return GetNode(null, 0, indexes);
      }
  
      public Objective GetNode(int? parentid, int level, params int[] indexes)
      {
          NorthwindDataContext cd = new NorthwindDataContext();
          Objective item = null;
  
          if (indexes.Length == 0)
              return null;
  
          if (parentid == null)
          {
              item = (from p in cd.Objective
                      where p.Level == indexes[level] &amp;&amp; p.Parent_ObjectiveID == null
                      select p).SingleOrDefault();
  
          }
          else
          {
              item = (from p in cd.Objective
                      where p.Level == indexes[level] &amp;&amp; p.Parent_ObjectiveID == parentid
                      select p).SingleOrDefault();
          }
  
          if (item == null)
              return null;
  
          if (++level &lt; indexes.Length)
              item = GetNode(item.ObjectiveID, level, indexes);
  
          return item;
      }
  </code></pre>