Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx_professional thread: Getting the ID of the Newly-created row in a table


Message #1 by Philip Steel <PhilipS@t...> on Wed, 22 May 2002 17:10:04 +0100
Chuck

The ADO.NET method was exactly the one I was after as this was how I did it
in
normal ADO.

But for now I guess the @@Identity will suffice.

Thanks for your help everyone,

P

-----Original Message-----
From: Feduke Cntr Charles R [mailto:FedukeCR@m...]
Sent: 23 May 2002 14:10
To: ASPX_Professional
Subject: [aspx_professional] Re: Getting the ID of the Newly-created r
ow in a table


Guys,

> You cam simply query the database for  the identity of the last created
> or updated row by executing a command: "select @@identity" this returns

	Keep in mind in a heavy accessed SQL Server (only) database,
@@identity is not guranteed to contain the identity for your row in
particular (between your update and someone else's insert followed by your
select @@identity, the latest generated row could be different than what you
expect).

	I remember in ADO (normal) that when you used a command object to do
an update and there was an autoincrement row, a OUT parameter was always
returned which was the autoincremented value.  I know in ADO.NET you must
close any connections before retrieving any OUT parameters.  Anyone who's
had more experience with ADO.NET know if the ADO-like autoincrement OUT
parameter is returned?  If it is, then that may be the easiest way to get
the autoincrement value.

HTH,
- Chuck

-----Original Message-----
From: Geoffrey [mailto:Geoffrey.Vandiest@t...]
Sent: Thursday, May 23, 2002 4:07 AM
To: ASPX_Professional
Subject: [aspx_professional] Re: Getting the ID of the Newly-created row
in a table


You cam simply query the database for  the identity of the last created
or updated row by executing a command: "select @@identity" this returns
the Id (if the id is selected as an identity column of the DB).  
A good practice is to access the DB with stored procedures that returns
this identity with the Return_Value. 
Ex: 
Update TableX set AcolumnName='Avalue'
Return @@Identity 

Geoffrey 


-----Original Message-----
From: Robert Horn [mailto:robertho@m...] 
Sent: jeudi 23 mai 2002 7:22
To: ASPX_Professional
Subject: [aspx_professional] Re: Getting the ID of the Newly-created row
in a table

I have gotten into a habit of adding a column at the end of my tables
called
InsertData. Before I insert the data I put the current date and time in
a
string with the UserID at the end. I then insert the data and then do a
search for it based on the data that I have in my string and on that
InsertData column. Another practice is to keep a table somewhere that
has
the last ID for each of the tables you have, and you use that to create
the
ID's, thus not allowing SQL to create the ID's. You then know exactly
what
the ID of the new column is.

Hope that helps
Robert
----- Original Message -----
From: "Philip Steel" <PhilipS@t...>
To: "ASPX_Professional" <aspx_professional@p...>
Sent: Thursday, May 23, 2002 2:10 AM
Subject: [aspx_professional] Getting the ID of the Newly-created row in
a
table


> Hi all
>
> Does anyone know how to get the ID of the newly-created row in a
table?
>
> here's some ADO.NET code I'm using:
>
> public void AddBrandDBEntry(string FileName, string FileType, string
> FileExtension, int FileSize, string Description)
> {
>
> //the sql statement
> string sSQL = "SELECT * from tblBrand";
>
> //the connection object
> OleDbConnection oCn = new
> OleDbConnection(OLEConnectionString());
> oCn.Open();
> OleDbDataAdapter oDA = new
> OleDbDataAdapter(sSQL,oCn);
> DataSet oDs = new DataSet();
> oDA.Fill(oDs,"tblBrand");
>
> //create the Data Row
> DataRow oDR = oDs.Tables["tblBrand"].NewRow();
>
> //Populate the datarow with values
> oDR["Filename"] = FileName;
> oDR["Filetype"] = FileType;
> oDR["FileExtension"] = FileExtension;
> oDR["Filesize"] = FileSize;
> oDR["Description"] = Description;
>
> //Add the datarow to the dataset
> oDs.Tables["tblBrand"].Rows.Add(oDR);
>
> //Use the Command Bulder object to generate Insert
> Command dynamically
> OleDbCommandBuilder oCB = new
> OleDbCommandBuilder(oDA);
>
> //Update the DB with values from Dataset with the
> Data Adapter
> oDA.Update(oDs,"tblBrand");
> //Clean Up
> oDA.Dispose();
> oDs.Dispose();
> oCB.Dispose();
> oCn.Dispose();
> }
>
> Cheers
>
> phil
>
> Phil Steel
> TEQUILA\ Digital
> 82 Charing Cross Road
> LONDON WC2H 0QB
> 020 7557 6228
>
>






  Return to Index