assigning guid value to guid field in .net c#

5881
2
08-04-2011 06:17 PM
ericliprandi
New Contributor III
Hi,
This seems to be a trivial thing: we want to use a GUID field (not GlobalID) onto a feature class to correlate each feature with an entity in another system. We declared the type as GUID in ArcCatalog, all other fields are just fine for writing, but when we try to write to a feature using IFeature.set_Value(index,value), we get the following errors:
- if we use a value of type System.Guid directly, we get: "The value type is incompatible with the field type."
- if we use System.Guid.ToString("N"), we get:"A general error when something is wrong with a Field" (yeah, thanks for that one esri!)

Other details: Using ArcGIS 10.0 SP2 (build 3200) against a SQL Express personnal SDE. The SQL Type of the field is Uniqueidentifier.

Thanks in advance for any pointers.

Regards,

Eric.
0 Kudos
2 Replies
NeilClemmons
Regular Contributor III
Make sure you're formatting the value so that the database can recognize it as a Guid.  Here's a method we use to convert the Guid object to a value that can be written to the database.

        Public Shared Function GuidToDatabase(ByVal value As System.Guid) As Object
            If value = System.Guid.Empty Then
                Return DBNull.Value
            Else
                Return "{" & value.ToString & "}"
            End If
        End Function
0 Kudos
ericliprandi
New Contributor III
Neil,
Thanks for your reply. We actually found the answer a few minutes after this post. I just wish esri would document that somewhere. Anyway, for SQL Server back-end, it seems that the "B" formatting of the guid will work (based on msdn documentation).
We haven't tried against Oracle yet, but I am really hoping that esri is taking care of abstracting the database from us... it would be a shame if we had to care about what RDBMS is underneath and format our data differently. I'll post an update once we test against Oracle.

Regards,

Eric.