@@identity

Last post 01-12-2008 12:05 PM by TATWORTH. 18 replies.

Sort Posts:

  • @@identity

    10-26-2006, 11:19 AM
    • Member
      692 point Member
    • mike7510uk
    • Member since 10-03-2006, 2:06 PM
    • Posts 318

    i have found loads of topics on this but have yet to find one that gives the answer i need.

    I want to display the last insert id into a label/textbox after the INSERT function

    how do i do this?

     

    www.the-web-shed.com
  • Re: @@identity

    10-26-2006, 11:33 AM
    • All-Star
      32,506 point All-Star
    • augustwind
    • Member since 07-21-2002, 11:16 PM
    • Garland, TX
    • Posts 4,550
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs

    A question back at you -

    Which database are you using?

    David Wier
    MCP/ASPInsider
    ASPNet101.com - where to look first!
    Replace It! - the newest from August Wind - search/replace in multiple files
    Control Grouper - easily control properties for multiple controls with one control!
    Calendar Express - The Best HTML Calendar Generator on the web!
    (Please 'Mark as Answer' when it applies)
  • Re: @@identity

    10-26-2006, 11:43 AM
    • Member
      285 point Member
    • Deadpoet
    • Member since 12-13-2005, 10:17 PM
    • Posts 57

    if the value you are using as an id is an idetity column and your database is sql server 2000 or 2005 then you can do something like this to get the last inserted value

            strSQL = "";
            strSQL += " Set Nocount on ";
            strSQL += " Insert TableX(...) VALUES (...) ";
            strSQL += " select IdentityInsert = IDENT_CURRENT( 'TableX' ) ";
            strSQL += " set nocount off ";

    hope this helps

  • Re: @@identity

    10-27-2006, 2:49 AM
    • Contributor
      3,592 point Contributor
    • yyy8347
    • Member since 09-19-2006, 11:00 AM
    • Posts 703

    In sqlserver you can also do it with stored procedure: 


    CREATE procedure [dbo].[textbookop_insert])
    as
    begin
    INSERT INTO [cosolu].[dbo].[cosolu_textbookop]
               ([textbookop_textbookid])
         VALUES
               (@textbookop_textbookid)
    return @@identity
    end;
    GO

    You can insert the value using the following code:

            SqlConnection sqlconn = DB.CreateConn();
            SqlCommand sqlcmd = new SqlCommand("textbookop_insert", sqlconn);
            sqlcmd.CommandType = CommandType.StoredProcedure;
            SqlParameter sqlpar = sqlcmd.Parameters.Add("ReturnValue", SqlDbType.Int);
            sqlpar.Direction = ParameterDirection.ReturnValue;
            sqlcmd.Parameters.AddWithValue("@textbookop_textbookid", textbookop_textbookid);
           sqlconn.Open();
            sqlcmd.ExecuteNonQuery();
            int camid = Convert.ToInt32(sqlcmd.Parameters["ReturnValue"].Value);
            sqlconn.Close();
     Hope this helps.
    Sincerely,
    Young Fang
  • Re: @@identity

    10-27-2006, 3:59 AM
    • Member
      692 point Member
    • mike7510uk
    • Member since 10-03-2006, 2:06 PM
    • Posts 318
    i am using an sqldatasource, dont know if that changes anything. I dont know how the syntax works when the connection is built into an object
    www.the-web-shed.com
  • Re: @@identity

    10-27-2006, 4:01 AM
    Answer
    • Member
      692 point Member
    • mike7510uk
    • Member since 10-03-2006, 2:06 PM
    • Posts 318
    im using sql server 2005
    www.the-web-shed.com
  • Re: @@identity

    10-27-2006, 10:04 AM
    • Contributor
      6,366 point Contributor
    • Lee Dumond
    • Member since 11-03-2004, 10:51 AM
    • Decatur, IL USA
    • Posts 1,168

    mike7510uk:
    i am using an sqldatasource, dont know if that changes anything. I dont know how the syntax works when the connection is built into an object

    If the connection is built into the object, you have a TableAdapter that is connected to the table you are using. This TableAdapter contains methods that pull data from the table, like Fill() and GetData(). You can easily add another method to this TableAdapter to get the last value that was generated for the identity column of its table.

    You can doubleclick on the .xsd file for the sqlDataSource, which opens the DataSet Designer. Go to the table you want to work with (lets call it myTable) and right click on any existing method in there. From this menu, choose Add Query.

    This will give you a handy-dandy TableAdapter Query Configuration Wizard. In this wizard:

    1. When it says "Choose a Query Type", choose "SELECT which returns a single value".

    2. When it says "Specify a SQL SELECT statement, type in  

    SELECT IDENT_CURRENT ('myTable') FROM myTable

     3. When it says "Choose a Function Name", give it a name like "GetLastIdentityValue".

    Voila, you're done. So now, in your page, lets say you want to grab the ID of the last record entered and disply it in a label (Label1).

    int theID = (int)myTableAdapter.GetLastIdentityValue();
    Label1.Text = theID.ToString();

     Notice there is a cast in the first line, because the value returned by IDENT_CURRENT is of type nullable Decimal.

    Hope that helps.

  • Re: @@identity

    10-27-2006, 10:05 AM
    • Participant
      1,738 point Participant
    • SuperGhost
    • Member since 12-29-2005, 4:58 PM
    • Palmdale CA, USA
    • Posts 409

    After you do an insert you can return the key for the insert like:

    INSERT INTO [dbo].[table]
     (name, phone)
     VALUES
     ("Denny", "555-1234")
    
    Return @@IDENTITY

    Also be sure to read on the differences of SCOPE_IDENTITY() and @@IDENTITY - to be sure which one you should use to return the correct id.

    ** Hope this helps!

    DennyDotNet
    Social42.com
  • Re: @@identity

    10-27-2006, 10:33 AM
    • Member
      692 point Member
    • mike7510uk
    • Member since 10-03-2006, 2:06 PM
    • Posts 318
    so after i set Return @@IDENTITY how would that display it into a label / textbox?
    www.the-web-shed.com
  • Re: @@identity

    10-27-2006, 10:40 AM
    • Member
      692 point Member
    • mike7510uk
    • Member since 10-03-2006, 2:06 PM
    • Posts 318
    Lee Dumond:

    mike7510uk:
    i am using an sqldatasource, dont know if that changes anything. I dont know how the syntax works when the connection is built into an object

    If the connection is built into the object, you have a TableAdapter that is connected to the table you are using. This TableAdapter contains methods that pull data from the table, like Fill() and GetData(). You can easily add another method to this TableAdapter to get the last value that was generated for the identity column of its table.

    You can doubleclick on the .xsd file for the sqlDataSource, which opens the DataSet Designer. Go to the table you want to work with (lets call it myTable) and right click on any existing method in there. From this menu, choose Add Query.

    This will give you a handy-dandy TableAdapter Query Configuration Wizard. In this wizard:

    1. When it says "Choose a Query Type", choose "SELECT which returns a single value".

    2. When it says "Specify a SQL SELECT statement, type in  

    SELECT IDENT_CURRENT ('myTable') FROM myTable

     3. When it says "Choose a Function Name", give it a name like "GetLastIdentityValue".

    Voila, you're done. So now, in your page, lets say you want to grab the ID of the last record entered and disply it in a label (Label1).

    int theID = (int)myTableAdapter.GetLastIdentityValue();
    Label1.Text = theID.ToString();

     Notice there is a cast in the first line, because the value returned by IDENT_CURRENT is of type nullable Decimal.

    Hope that helps.

    where is this file? i dont have any xsd files in my folders!!

    www.the-web-shed.com
  • Re: @@identity

    10-27-2006, 1:26 PM
    • Contributor
      6,366 point Contributor
    • Lee Dumond
    • Member since 11-03-2004, 10:51 AM
    • Decatur, IL USA
    • Posts 1,168

    Sorry, I thought you were using an ObjectDataSource. I should have read more closely. My bad. Embarrassed (With the OnjectDataSource, you can use a strongly-typed Dataset that contains TableAdapters.)

    In the case of using an SqlDataSource, you'll can use one of the methods above to return the identity value with the insert command.

  • Re: @@identity

    11-14-2006, 5:20 PM
    • Member
      65 point Member
    • juliabeach
    • Member since 05-08-2006, 6:32 PM
    • Northern Virginia, USA
    • Posts 13
    Lee Dumond:

    If the connection is built into the object, you have a TableAdapter that is connected to the table you are using. This TableAdapter contains methods that pull data from the table, like Fill() and GetData(). You can easily add another method to this TableAdapter to get the last value that was generated for the identity column of its table.

    You can doubleclick on the .xsd file for the sqlDataSource, which opens the DataSet Designer. Go to the table you want to work with (lets call it myTable) and right click on any existing method in there. From this menu, choose Add Query.

    This will give you a handy-dandy TableAdapter Query Configuration Wizard. In this wizard:

    1. When it says "Choose a Query Type", choose "SELECT which returns a single value".

    2. When it says "Specify a SQL SELECT statement, type in  

    SELECT IDENT_CURRENT ('myTable') FROM myTable

     3. When it says "Choose a Function Name", give it a name like "GetLastIdentityValue".

    Voila, you're done. So now, in your page, lets say you want to grab the ID of the last record entered and disply it in a label (Label1).

    int theID = (int)myTableAdapter.GetLastIdentityValue();
    Label1.Text = theID.ToString();

     Notice there is a cast in the first line, because the value returned by IDENT_CURRENT is of type nullable Decimal.

    Question: doesn't this risk getting a later insert's identity value?  How can I get the scope_identity value through the table adapter like this but from the same database call that actually makes the insert (less risky)?  Regardless, thanks for a great solution using table adapters!

    Filed under: , , ,
  • Re: @@identity

    11-14-2006, 6:49 PM
    • Contributor
      6,366 point Contributor
    • Lee Dumond
    • Member since 11-03-2004, 10:51 AM
    • Decatur, IL USA
    • Posts 1,168

    Yes, is which why you should call it immediately, right after you call the Update method. As you correctly point out, it surely doesn't make sense to use it in any other manner.

    Here is an example where it could come in handy. Say, for instance, you have two tables: Orders and OrderItems. The OrderItems table is going to contain an OrderID column that is a foreign key back to the Orders table (OrderID would be a primary key column in Orders).

    When you place OrderItems in your dataset, you have no idea what value to put in that foreign key OrderID column, because the current order hasn't been saved yet. You have a classic chicken-and-egg situation. What to do?

    Easy... when you create the OrderItems, just put in any dummy value you want (it is going to be replaced with the correct value in a minute).

    When you've added all the items you want, the current order is finished and you'll want to save it. To do that, do an Update on the OrdersTableAdapter, then immediately call the GetLastIdentityValue() method you've set up:

    ordersTableAdapter.Update(myDBDataSet.Orders);
    int orderId = (int) ordersTableAdapter.GetLastIdentityValue();
     This gives you the number to put in that foreign key column of the OrderItems table. So, now you loop through the dataset of the OrderItems, set each OrderID value of each accordingly, then update that:
    foreach (myDBDataSet.OrderItemsRow row
        in myDBDataSet.OrderItems)
    {
        row.OrderID = orderId;
    }
    orderItemsTableAdapter.Update(myDBDataSet.OrderItems);
     Voila! All the foreign keys match, anf the integrity of the database is maintained.

     

  • Re: @@identity

    11-15-2006, 7:08 AM
    • Member
      692 point Member
    • mike7510uk
    • Member since 10-03-2006, 2:06 PM
    • Posts 318

    i did this in the end by using mysql and puttin it into the code behind.

    All works good,

    cheers for all help 

    www.the-web-shed.com
  • Re: @@identity

    07-11-2007, 4:52 PM
    • Member
      32 point Member
    • ThomasK67
    • Member since 09-25-2006, 3:29 PM
    • Posts 15

    That is exactly the answer I needed... all the other projects here had been done in Deklarit, and after an update the DataRow held the new identity value... not so, apparently, with the xsd dataset in studio.  This is exactly what I needed.  Thanks so much!

    Thomas

Page 1 of 2 (19 items) 1 2 Next >