Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 26th, 2007, 06:31 PM
Registered User
 
Join Date: Dec 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Determining Indentity Ineteger of New Record

I am using the formview control to insert records.

The table receiving the inserts has an ID field that is an identity integer. The insert is done with the button supplied by the formview control. The formview does all the inserting. I write no insert code.

So how do I determine the identity integer of a newly Inserted record?

Right now I am adding a unique field to the table and after the insert I do a select to get the new identity ID. With all the new functionality in ASP.NET 2.0 I can't believe I have to do it this way.

Any ideas?
 
Old January 27th, 2007, 01:07 PM
Registered User
 
Join Date: Dec 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You can do some of this visually but it is hard to show that here.

In the INSERT command add a select statement to select the SCOPE_IDENTITY().
Be sure to put a semi colon after the INSERT statement.

InsertCommand="INSERT INTO Employees(LastName, FirstName) VALUES (@LastName, @FirstName);
                 SELECT @EmpID = SCOPE_IDENTITY()"


Define an OUTPUT parameter for the SQLDatasource.
You can change the InsertQuery property for the SQLDataSource to do this part visually.
<InsertParameters>
    <asp:Parameter Name="EmpID" Direction="Output" Type="Int32" DefaultValue="0" />
</InsertParameters>


In the SqlDataSource1_Inserted event......

   Protected Sub SqlDataSource1_Inserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEvent Args) Handles SqlDataSource1.Inserted

        Dim intID As Integer
        intID = Convert.ToInt32(e.Command.Parameters("@EmpID").Val ue)

    End Sub



 
Old January 27th, 2007, 03:58 PM
Registered User
 
Join Date: Dec 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This will do it in 1 query instead of 2.

InsertCommand="INSERT INTO Employees(LastName, FirstName) VALUES (@LastName, @FirstName);
                 Set @EmpID = SCOPE_IDENTITY()"


 
Old January 30th, 2007, 04:13 PM
Authorized User
 
Join Date: Apr 2005
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I thank you myself for that. I have a 2 quesry storeprocedure that i am reducing down. I did basically the same thing you did in my apps in your first example. Like your second one better. :)






Similar Threads
Thread Thread Starter Forum Replies Last Post
Determining File Types seananderson Access VBA 2 February 12th, 2007 12:59 PM
Determining the cache size easwars C++ Programming 0 January 28th, 2007 12:33 PM
Determining the name of function dotnetprogrammer VS.NET 2002/2003 0 October 23rd, 2005 04:46 AM
Determining how a window is closed chrscote Javascript How-To 2 June 16th, 2005 07:53 AM
Determining a Sub or Procedure Name in Code pmgibson Beginning VB 6 3 March 10th, 2005 01:16 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.