SQL Server 2000 / ADO.Net / VB.Net / DataGrid
I am a newbie to ADO.net and disconnected data. Even though I have done
a lot of reading I have not been able to get the following questions
answered.
I don't understand how a disconnected dataset is supposed to get the id
field value assigned. In sqlsvr2k the id field is assigned as an
identity with a seed value and an increment. Does the dataset know that
when it isn't even connected to sql server where the identity
constraints are defined?
I thought... maybe the dataset is smarter than I realize I'll just try
it.
Well I tried it. I can't get it to work!
More description:
Because the ID is defined as an identity it also is "NOT NULL". I can't
create a record in the Datagrid without the system stopping me to tell
me the ID is null - and of course it will not let me assign a value to
it either.
1.) So... What's the secret?
I understand in a disconnected environment that concurrency is an issue
that needs to be dealt with but you still need to somehow get a value
into the id field to start with.
2.) And while you are at it please clear up a related mystery for me.
In a disconnected dataset environment how can vb.net or ado.net deliver
the customer_id (of a customers table) to the foreign key
(fk_customer_id) an orders table?? It may not even exist yet in many
scenarios.
=============================
Below is my table. As you can see RefSourceID is an Identity and SQL
Server should deliver the next value to it.
CREATE TABLE [dbo].[lkp01RefSource] (
[RefSourceID] [tinyint] IDENTITY (1 ,1) NOT NULL,
[RefSource] [varchar] (25) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
, -- The display name of the table,
[ord] [tinyint] NOT NULL DEFAULT 0, -- sort first by ord then by name,
[hide] [bit] NOT NULL DEFAULT 0,
) ON [PRIMARY]
GO
In vb.net I had the data adapter configured to create a stored
procedure.
My note in the stored procedure below helps to illustrates some of the
mystery to me.
=====================================
CREATE PROCEDURE [dbo].[RefSourceInsertCommand]
(
@RefSourceID tinyint, -- << What is this? No accomodation for sql
generating its identity
@RefSource varchar(25),
@ord tinyint,
@hide bit
)
AS
SET NOCOUNT OFF;
INSERT INTO lkp01RefSource(RefSourceID, RefSource, ord, hide) VALUES
(@RefSourceID, @RefSource, @ord, @hide);
SELECT RefSourceID, RefSource, ord, hide FROM lkp01RefSource WHERE
(RefSourceID = @RefSourceID);
GO
=====================================
If code creates the identity then there are conflicts to deal with if
other is adding records at the same time.
In answer to #2 above. Can't vb ask for and ID immediately before
continuing to build the record. (of course there can be no "NOT NULL"
fields with that scheme)
Please help me understand this or direct me to an article that
definitively answers this.
Sijin Joseph - 02 Oct 2004 13:39 GMT
You need to define the seed and the increment value for autoincrement
columns in the dataset. Once you fill data in the dataset it just
increments the values like the DB does, but since it cannot know about
other updates to the DB that might have occured this only works in a
single user scenario.
The generally accepted practise is to use -1 as the seed and -1 as the
increment, now when you insert news rows the ID's will never conflict
with those that might already be present in the DB, when you update the
Dataset make sure that you update the ID column to whatever value gets
assigned to the ID for that row.
Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph
> SQL Server 2000 / ADO.Net / VB.Net / DataGrid
>
[quoted text clipped - 77 lines]
> Please help me understand this or direct me to an article that
> definitively answers this.