GUIDs & uniqueidentifiers in datasets

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am developing a windows forms application which takes its data from an SQL
server database where all the primary keys are uniqueidentifier fields.

I am creating strongly typed datasets and populating them using the Fill
method. My problem is how to generate guids when inserting rows into the
datasets, for example when the dataset is bound to a datagrid control. Can
this be done?

I have also tried using the AddRow method as below:

Dim drNewRow As DsProductRange.CategoriesRow =
dsProductRange1.Categories.NewCategoriesRow

drNewRow.CategoryID.NewGuid()
drNewRow.CategoryName = "New Range"
dsProductRange1.Categories.AddCategoriesRow(drNewRow)

However the .NewGuid() method here doesn't seem to work as the CategoryID
field has already picked up a {System.InvalidCastException} error (I think
related to the way the field is described in the generate XSD schema. Is
there anyway to generate guid values as default values to the dataset field
like calling the newid() function in the SQL table?
 
You can generate a new GUID in your code using the System.Guid structure.
This GUID will still be globally unique, even though it is not created in the
database.


---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 
I thought that is what I was trying to do in my example given below, but it
does not appear to be working.
 
Hi James,
What I've been doing in my projects that seems to work rather well is
assigning the value of Guid.NewGuid() to whatever field I need it. Using
your example code below, it would be something like

drNewRow.CategoryID = Guid.NewGuid();

Hope that works for you.

Matt
 
Back
Top