🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.database.ado
Article #4735

How do I insert a new record into a table with an identity field?

#4735
From: Andrey Subbotin
Date: Tue, 09 Oct 2001 12:58
50 lines
2385 bytes
Hello all.

How do i insert a new record into a table with an identity field?
Suppose I have the following piece of code:

TADOTable *t = new TADOTable(FOwnerComponent);
try {
    t->Connection = FConnection;
    t->TableName = "workers";
    t->Active = true;
    t->Insert();
    t->FieldByName("name")->AsString = "Mr. Incognito";
    t->Post();
    Id = t->FieldByName("id")->AsInteger;
    t->Active = false;
} __finally {
    delete t;
}

FOwnerComponent, FConnection and Id are some valid variabled I use in the
program...

I open the table named "workers". Let it be

table workers (
    id int identity(1,1) not null,
    name varchar(255)
);

Then assign some name to the "name" field and try to post the changes into
the DB.

I always supposed that it itsn't necessary to assign some value to identity
fields, it's even not possible in plain SQL, AFAIK... And so I hoped that
the insertion will succeed.

Then, to ruin all my hopes, ADO simply said, "You can't  assign NULL value
to id field. INSERT failed... blah-blah-blah." I know I can't assign NULL,
or even any other value, to an _identity_ field,
but ADO thinks I must, and it shouldn't be NULL... Weird.

After all, I'm kind of puzzled. How do I insert a new record into a table
with an identity field using TADOTable? Is it possible at all?

Any help welcome. Thanks in advice...

See you,
Andrey.

--- Find out what you can do.
Technical Details
Message-ID: <3bc2e62f$1_2@dnews>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news1.google.com!sn-xit-02!supernews.com!newsfeed.direct.ca!look.ca!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!dnews

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10698b,2d119fd5e61bb3cb,start
X-Google-Attributes: gid10698b,public
X-Google-ArrivalTime: 2001-10-09 05:22:29 PST
Reply-To: "Andrey Subbotin" <e-ploko@mail.ru>
From: "Andrey Subbotin" <e-ploko@mail.ru>
Newsgroups: borland.public.cppbuilder.database.ado
Subject: How do I insert a new record into a table with an identity field?
Date: Tue, 9 Oct 2001 19:58:21 +0700
Lines: 53
Organization: Onyx Advertisisng Group
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.50.4522.1200
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4522.1200
NNTP-Posting-Host: 195.161.98.246
Message-ID: <3bc2e62f$1_2@dnews>
X-Trace: dnews 1002628655 195.161.98.246 (9 Oct 2001 04:57:35 -0700)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news1.google.com!sn-xit-02!supernews.com!newsfeed.direct.ca!look.ca!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!dnews