GUID Datatype in .NET Schemas (How to save a GUID into a database)

Posted on 2004-10-24
Last Modified: 2008-02-07
Trying to save a simple dataset to a database but i keep getting the error
'Invalid Cast from System.String to System.Guid' when I try a DataAdapter.Update.

The dataset contains a GUID column...but the datatype in the dataset SCHEMA is a string.  What datatype should I be using? How does one ever save a GUID into a database ?

P.S I do not need to generate a new GUID.  I have a preset one I am using for now.
Question by:invisiblekid
    LVL 18

    Accepted Solution

    .Net treats GUID as String anyway, so you would be better of to keep GUID as string (varchar, char) in Database. That will save you from unnecessary Conversion each time you retrieve or save GUID.

    LVL 15

    Expert Comment

    Actually there is a GUID data type in SQL Server 2000 (if that's what you're using). SqlDbType.UniqueIndentifier corresponds to the uniqueidentifier data type in SQL Server, so if you use that, you won't need to cast it. OleDbType also has a GUID data type called OleDbType.Guid, but you would have to find out if your database has a GUID data type, too.
    LVL 44

    Expert Comment

    alternatively, if you are saving the data to a STring data type in the Database, use this:

    Dim MyGiud as Guid = New Guid
    Dim strGuid as String = MyGuid.ToString

    That is you can convewrt a Guid value to the Corresponding strin using ther .ToString method of the variable.

    LVL 44

    Expert Comment

    you can also convert a string (that hods a GUID value) to a GUID with:

    Dim MyGuid as Guid = Guid(strGuid)  at least I think that is the syntax.  I do not have .NET installed at home, so I can't check it directly.

    LVL 3

    Expert Comment

    GUIDs are treated as GUIDs, Strings are treated as Strings.  

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Lean Six Sigma Project Manager Certification

    There are many schools of thought around successful project management, but few as highly regarded as the Six Sigma and Lean methods. With 37 hours of learning, this training will explain concrete processes for increasing efficiency and limiting wasted time and effort.

    Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (…
    In my previous article ( we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
    With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    860 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now