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

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.
Who is Participating?
tusharashahConnect With a Mentor Commented:
.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.

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

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.

GUIDs are treated as GUIDs, Strings are treated as Strings.  
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.