Still having problems inserting a row into a table with uniqueidentifier column...

  • Thread starter Thread starter Alex
  • Start date Start date
A

Alex

I'm still having problems inserting a row into a table with a column with
the uniqueidentifier datatype.
I'm having no problems autoincrementing using datatypes like integer.

Here is my table that I created with VS2003.NET using the server explorer
tools.
myTable
col1: (primary key) - char (length 40) - not allowing null

col2: uniqueidentifier - not allowing null
Description: ""
Default Value: (newid())
Is RowGuid: Yes
Formula: ""
-> all other fields are grayed out; this includes the autoincrement
fields

Here is the code that I'm using to add a row:

DataSet myDataSet= new DataSet();
myAdapter.Fill(myDataSet, "myTable");
DataRow newDataRow= myDataSet.Tables["myTable"].NewRow();
newDataRow["col1"] = "Hello";
myDataSet.Tables["myTable"].Rows.Add(newDataRow);
myAdapter.Update(myDataSet,"myTable");

This results with the exception that the col1 value can't be NULL.
Of course if I change the column to allow null, I can add the row, resulting
in a row entry with no GUID.
If I add a line like newDataRow["col2"] = System.NewGUID(); I get it to
work.

I'm still looking for a way to add a new row using uinqueidentifier without
the new to specify the GUID in the client.

Please note that the autoincrement fields are grayed out in the designer for
this type. The fields can't be modified in the client code.
Alex
 
Alex,

If you're going to use a Guid/uniqueidentifier as your primary key,
your best bet is to generate the new PK values as you create your DataRows.
To the best of my knowledge, there is no @@IDENTITY-equivalent for asking
SQL Server for the last uniqueidentifier generated.

You could submit the update via a stored procedure and have the
procedure return the new Guid via an output parameter. However, you'd
still have to use a placeholder for pending inserts in the DataTable if you
want to set the DataTable's PrimaryKey property.

I hope this information proves helpful.

David Sceppa
Microsoft
This posting is provided "AS IS" with no warranties,
and confers no rights. You assume all risk for your use.
© 2004 Microsoft Corporation. All rights reserved.
 
Back
Top