Simple numbering in a database

Hi,

I have a treeview that load and saves the treeview-nodes to a database.
The table contains columns called NodeID and a ParentNodeID and other columns.

In de code-section I have wrote code to insert a folder-node into the treeview
My question is about the code-line that I have marked with an arrow.

With this code the programm inserts a complex string of letters and nummbers.
But I just want a simple numbering.

What can I do?

Greetings,

Peter Kiers






private void InsertNewFolder(TreeNode node)
        {
            DataRow row = null;
            DataRow ParentRow = null;
            DataTable dt = null;
            string NewText1 = "";
            try
            {
                if (InputBox.Show("New Folder", "Folder", ref NewText1, validation) == DialogResult.OK)
                {
                    ParentRow = (DataRow)node.Tag;
                    if (ParentRow == null) { return; }
                    dt = ParentRow.Table;
                    row = dt.NewRow();
                    row["NodeID"] = Guid.NewGuid().ToString(); <=====================================
                    row["ParentNodeID"] = ParentRow[dt.PrimaryKey[0].ColumnName].ToString();
                    row["Name"] = NewText1;
                    row["ImageIndex"] = 1;
                    row["ServSize"] = txtServSize.Text;
                    row["Cal"] = txtCal.Text;
                    row["Chol"] = txtChol.Text;
                    row["Fat"] = txtFat.Text;
                    row["Fib"] = txtFib.Text;
                    row["Carbs"] = txtCarbs.Text;
                    row["Sod"] = txtSod.Text;
                    row["Prot"] = txtProt.Text;
                    dt.Rows.Add(row);
                    TreeNode newNode = TreeViewUtil.GetTreeNodeFromDataRow(row, "Name");
                    node.Nodes.Add(newNode);
                    treeView1.SelectedNode = newNode;
                }
            }
            catch (Exception) { throw; }
        }

Open in new window

LVL 1
peterkiersAsked:
Who is Participating?
 
AndyAinscowFreelance programmer / ConsultantCommented:
OK.
This isn't quite so simple as you think.

Would any of these child records be moved to another parent?
Would any of these child records be deleted?

Put another way is this acceptable?

parent ID 1    child ID 0
parent ID 1    child ID 1
parent ID 1    child ID 3    ///NOTE parent ID 1    child ID 2 does not exist
parent ID 2    child ID 0
parent ID 2    child ID 1
parent ID 3    child ID 0
0
 
Luis PérezSoftware Architect in .NetCommented:
You can use System.DateTime.Now.Ticks()

Hope that helps.
0
 
peterkiersAuthor Commented:
No just simple numbering.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
Luis PérezSoftware Architect in .NetCommented:
And what exactly do you want to say with "simple"?
0
 
peterkiersAuthor Commented:
f.e: in NodeId: 0

 in ParentNode Id: 1
0
 
johannodCommented:
Hi,

You can use the database's autonumber column type and retrieve the generated number when you need it. Full info here: http://msdn.microsoft.com/en-us/library/ks9f57t0%28v=vs.80%29.aspx

/Johan
0
 
Pratima PharandeCommented:
row["NodeID"] = (Convert.ToInt32( ParentRow[dt.PrimaryKey[0].ColumnName]) -1).ToString();
0
 
Luis PérezSoftware Architect in .NetCommented:
But how do you want to ensure the uniqueness of each node? The Guid.NewGuid.ToString() ensures that each new node in database has a unique ID. You need to ensure of this enforcement in your code logic.
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
As far as I see it this will only add one record.  
Do you want each new record to have the ID of 0 ?  (which sounds like nonsense).

Do you require the following in the database (or something like it)?

parent ID 1    child ID 0
parent ID 1    child ID 1
parent ID 1    child ID 2
parent ID 2    child ID 0
parent ID 2    child ID 1
parent ID 3    child ID 0

....

0
 
peterkiersAuthor Commented:
Yes, sorry for not being clear about my problem
But the last post of AndyAinscow is exactly what I wanted.


Peter
0
 
peterkiersAuthor Commented:
I give up.

Peter
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.