DefaultValue

  • Thread starter Thread starter Jim Heavey
  • Start date Start date
J

Jim Heavey

If I am setting up a DataTable and a particular column is defined as a GUID
and in SQL Server it has a default value of newid() does this mean that I
should define the "DefaultValue" for the column as new Guid()? How do I
handle setting the DefaultValue for the column in the datatable?
 
Hi Jim,

You shuld generate new guid when you insert new data into datatable.
When you send data to database, don't insert the guid field (server will
generate it for you).
Make sure to add a Select for the guid field after insert so the datatable
is refreshed with proper guid.
 
Hi Jim,

You shuld generate new guid when you insert new data into datatable.
When you send data to database, don't insert the guid field (server will
generate it for you).
Make sure to add a Select for the guid field after insert so the datatable
is refreshed with proper guid.

Why? Since you've already generated a guid on the client, why not just
send it up to the database and have the DB use the guid you've already
got rather than generating a new one and returning it? What am I
missing here?
 
Hi David,

Because Sql server is generating the value.
The client one (that for new rows) is just temporary one.
 
Hi David,

Because Sql server is generating the value.
The client one (that for new rows) is just temporary one.

Hi,

But the question is why do it that way? The client guid value is
temporary only if you choose to make it so. There's no reason I see not
to send the client-generated guid up to the database during the insert
and allow it to be the database value as well.

It's not like IDENTITY keys, where you *have* to make the round trip.
Just because the database has a newid() default, there's no requirement
that one has to use it. And since the client has already generated a
GUID, I just don't see the advantage in generating a new one on the
server and having to read it back down during an insert.
 
Back
Top