[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.odbcnet

is there a "last id" mechanism on an update?

Beemer Biker

5/14/2007 11:36:00 PM

I have a gridview "new" command that insert a new row into a postgresql
database.that uses the odbc driver.

Newbie as I am i thought that iReturn would have the new index that was
created.

SqlDS1.InsertCommand = "INSERT INTO \"Algorithms\" (source_code_id, comment
VALUES (69, 'haleys comet', 1)";

int iResult = SqlDS_Algorithms.Insert();

Ok, instead of getting back an id I got back a "1" which just indicates the
command ran ok. Looking at the database I see the new id value and it is
318. Is there some mechanism I can use to obtain the index value that was
autogenerated?

googleing around I see where some people are writing stored procedures and I
see one here:

http://www.thescripts.com/forum/thread1...



Was just wondering if the is a way to use the gridview or sqldataset1
control to get that index value and avoid writing a stored procedure.



...thanks..


--
======================================================================
Joseph "Beemer Biker" Stateson
http://Research... Ask about my 99'R1100RT
======================================================================


2 Answers

Riki

5/15/2007 1:16:00 PM

0


"Beemer Biker" <jstateson@swri.edu> wrote in message
news:134hsi499grbg21@corp.supernews.com...
>I have a gridview "new" command that insert a new row into a postgresql
>database.that uses the odbc driver.
>
> Newbie as I am i thought that iReturn would have the new index that was
> created.
>
> SqlDS1.InsertCommand = "INSERT INTO \"Algorithms\" (source_code_id,
> comment VALUES (69, ''haleys comet'', 1)";
>
> int iResult = SqlDS_Algorithms.Insert();
>
> Ok, instead of getting back an id I got back a "1" which just indicates
> the command ran ok. Looking at the database I see the new id value and it
> is 318. Is there some mechanism I can use to obtain the index value that
> was autogenerated?
>
> googleing around I see where some people are writing stored procedures and
> I see one here:
>
> http://www.thescripts.com/forum/thread1...
>
>
>
> Was just wondering if the is a way to use the gridview or sqldataset1
> control to get that index value and avoid writing a stored procedure.

There is.
Still you need a "mini" stored procedure.
Just extend your INSERT command on the datasourcecontrol as follows:
"INSERT INTO myTable (x,y,z) VALUES (@x,@y,@y);SELECT @newID = @@IDENTITY;"

Then add an additional parameter @newID to your datasourcecontrol''s
InsertParameters, marking it as Direction="output" instead of the default
input.

In the OnInserted event of the datasourcecontrol, you can get the value of
this parameter with e.Command.Parameters("@newID").Value.

In Sql Server 2005, SELECT @@IDENTITY can be replaced by
SELECT SCOPE_IDENTITY() which is a little bit better.

Riki


MasterGaurav \(www.edujini-labs.com\)

6/4/2007 8:58:00 AM

0

>> int iResult = SqlDS_Algorithms.Insert();
>>
>> Ok, instead of getting back an id I got back a "1" which just indicates
>> the command ran ok. Looking at the database I see the new id value and
>> it is 318. Is there some mechanism I can use to obtain the index value
>> that was autogenerated?

Well, it returns the number of rows affected! And that''s what you get as the
iResult.

Follow what you got from thescripts-article or what Riki wrote. That''s the
sole way out.


--
Happy Hacking,
Gaurav Vaish | www.mastergaurav.com
www.edujini-labs.com
http://eduzine.edujin...
-----------------------------------------