SQL Datatype Conversions

This is a two part, related quesiton

I have a SQL table (ELEMENTS). This table has three fileds [ElementID], [ElementName] & [ElementParent]. The [ElementID] data type is uniqueidentifier (i.e  2835a870-1ea3-4e16-bf8a-1387a20d5f8b) The [ElementName] data type is Text. The [ElementParent] Datatype is nvarchar(50). This table represents a parent child hierarchy, so the [ElementParent] will always reference the [ElementID] of some other row in the table.  

Q1: Is there a better data type for [ElementParent] than nvarchar(50)...Since it references the uniqueidentifier of the [ElementID] field (i.e 2835a870-1ea3-4e16-bf8a-1387a20d5f8b)

Q2: I have a VB treeview control that sits on top of the ELEMENTS table. In the treeview code I have the following line:

If tree.AddNode(New MyNode(CInt(row("ElementParent")), CInt(row("ElementID")), CStr(row("Element")))) Then

 When I run my code I get the following error..."Conversion from type GUID to type Integer is not valid"  This code works if [ElementID] and [ElementParent] are set  to integer, but my ultimate solution requires that [ELEMENTID] be a uniqueidentifier

Any help here would be appreciated

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Q1.  Why not use uniqueidentifier?
Q2.  Change the definition of MyNode class to use GUID...

Public Class MyNode

    Public Sub New(ByVal xID As Guid, ByVal xName As String, ByVal xParentID As Guid)
        Name = xName
        ID = xID
        ParentID = xParentID
    End Sub
    Public ChildNodes As New List(Of MyNode)
    Public Name As String
    Public ID As Guid
    Public ParentID As Guid

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
The error you are getting is because of the code...
where you are explicitly casting ElementParent as an integer...and it is not an integer.

Change the field type of the database, or cast it to something besides an int...a guid can be a guid or a string.
U can have the int set to identity property that will generate unique numbers for each row and u can modify the data with setting the property off. When you turn it on it will go from the max number presnet in the column.
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

dwood1112Author Commented:

I think of uniqueidenifier in the same vane as Autonum in Access. Which is why I didn't use it on [ElementParent]. I know it's not the same as Access, but this is wierd to me...

Anyway I will follow your well proven advise shortly....Stay tuned, and thanks again.
>I think of uniqueidenifier in the same vane as Autonum in Access.

It's really just a SQL Server data type.  It will correspond to the GUID type in VB.NET.  
dwood1112Author Commented:

OK here's my problem....My logic assumes a 0 default value in the [ElementParent] field...I can't use the uniqueidentifier data type with a 0 default value (or any other default value I can think of).

How can I make the ElementID an auto generated number and still allow a default value in the [ElementParent] field?
dwood1112Author Commented:
Thanks for your help....Any thoughts on that last question?
The only possibility I see is to create a single row which will never display, but represents the root of the tree.  The ElementID can be autogenerated and can be the ParentID for all the elements that form the first branches of your treeview.  The ParentID for the root row, can be set to all zeroes.  
dwood1112Author Commented:
OK Thanks....I'll play around with it.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.