Link to home
Start Free TrialLog in
Avatar of pr_wainwright
pr_wainwright

asked on

Get Last Identity from table ASP.NET

Hi,
    I have a web form as shown below. I wish to modify the INSERT statement to include a IDENT_CURRENT('Employees') statement. I wish to store this in an 'Output' parameter & then retrieve its value in the ItemInserted event handler in the code behind file (C#). Any help appreciated.

Thanks
Paul.

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DummyDBConnectionString %>"
            DeleteCommand="DELETE FROM [Employees] WHERE [Employeeid] = @Employeeid"
//Modify insert here to include IDENT_CURRENT('Employees')
InsertCommand="INSERT INTO [Employees] ([LastName], [FirstName]) VALUES (@LastName, @FirstName)"
            SelectCommand="SELECT [LastName], [FirstName], [Employeeid] FROM [Employees]"
            UpdateCommand="UPDATE [Employees] SET [LastName] = @LastName, [FirstName] = @FirstName WHERE [Employeeid] = @Employeeid">
            <DeleteParameters>
                <asp:Parameter Name="Employeeid" Type="Int32" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="LastName" Type="String" />
                <asp:Parameter Name="FirstName" Type="String" />
                <asp:Parameter Name="Employeeid" Type="Int32" />
            </UpdateParameters>
            <InsertParameters>
                <asp:Parameter Name="LastName" Type="String" />
                <asp:Parameter Name="FirstName" Type="String" />
//Add a parameter here to store IDENT_CURRENT('Employee') last value
            </InsertParameters>
        </asp:SqlDataSource>
        <asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True" AutoGenerateRows="False"
            DataKeyNames="Employeeid" DataSourceID="SqlDataSource1" Height="50px" Width="125px">
            <Fields>
                <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
                <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
                <asp:BoundField DataField="Employeeid" HeaderText="Employeeid" InsertVisible="False"
                    ReadOnly="True" SortExpression="Employeeid" />
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" />
            </Fields>
        </asp:DetailsView>
   
    </div>
    </form>
</body>
</html>
Avatar of Anurag Thakur
Anurag Thakur
Flag of India image

ASKER CERTIFIED SOLUTION
Avatar of HeoQue
HeoQue

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial