?
Solved

SQL Datatype Conversions

Posted on 2008-01-29
9
Medium Priority
?
340 Views
Last Modified: 2013-11-06
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

0
Comment
Question by:dwood1112
9 Comments
 
LVL 38

Accepted Solution

by:
PaulHews earned 2000 total points
ID: 20771984
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
...
0
 
LVL 3

Expert Comment

by:bigdaddyz99
ID: 20772171
The error you are getting is because of the code...
CInt(row("ElementParent"))
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.
0
 
LVL 3

Expert Comment

by:avbsql
ID: 20772670
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.
0
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

 

Author Comment

by:dwood1112
ID: 20772683
Paul,

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.
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 20772865
>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.  
0
 

Author Comment

by:dwood1112
ID: 20773871
Paul,

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?
0
 

Author Closing Comment

by:dwood1112
ID: 31426156
Thanks for your help....Any thoughts on that last question?
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 20777280
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.  
0
 

Author Comment

by:dwood1112
ID: 20777435
OK Thanks....I'll play around with it.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.

593 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question