开发者

How to create a Add function for a record database?

In my program, I need to put an add function to insert categories. I'm using a treeView to show the data.

How can I model this class in a database?

How to create a Add function for a record database?

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).

Sometimes, you may add an already level setted and so that must be forbidden.

I need a little of help in my code. It's finished, but I want to improve it or fixed errors (if so).

Here is the code:

    private voi开发者_StackOverflow中文版d 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 && 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] && p.Parent_ObjectiveID == null
                    select p).SingleOrDefault();

        }
        else
        {
            item = (from p in cd.Objective
                    where p.Level == indexes[level] && p.Parent_ObjectiveID == parentid
                    select p).SingleOrDefault();
        }

        if (item == null)
            return null;

        if (++level < indexes.Length)
            item = GetNode(item.ObjectiveID, level, indexes);

        return item;
    }


Why don't you let the user select a parent node to which the new category should be added? You could let them just click a parent node and then add a new node to it. No checks involved. I know that this does not answer your direct question, but your current approach is hard on you and your users.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜