HighTechTalks DotNet Forums  

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

Dotnet Framework (ODBC.net) microsoft.public.dotnet.framework.odbcnet


Discuss is there a "last id" mechanism on an update? in the Dotnet Framework (ODBC.net) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Beemer Biker
 
Posts: n/a

Default is there a "last id" mechanism on an update? - 05-14-2007 , 06:35 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/thread173510.html



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://ResearchRiders.org Ask about my 99'R1100RT
================================================== ====================



Reply With Quote
  #2  
Old   
Riki
 
Posts: n/a

Default Re: is there a "last id" mechanism on an update? - 05-15-2007 , 08:16 AM







"Beemer Biker" <jstateson (AT) swri (DOT) edu> wrote

Quote:
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/thread173510.html



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




Reply With Quote
  #3  
Old   
MasterGaurav \(www.edujini-labs.com\)
 
Posts: n/a

Default Re: is there a "last id" mechanism on an update? - 06-04-2007 , 03:57 AM



Quote:
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.edujini-labs.com
-----------------------------------------




Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.