Example of getting the inserted Record ID

Source: Internet
Author: User
<% @ Import namespace="System. Data"  %>

<% @ Import namespace="System. Data. sqlclient"  %>

< Html >

< Head >

< Title > Example of getting the inserted Record ID </ Title >

< Script Language = "C #" Runat = "Server" >



Void Page_load (Object sender, system. eventargs E)

{

// Database connection string

String connstr = System. configuration. configurationsettings. deleettings [ " Connectionsqlserver " ];

// Create an insert SQL statement and call the @ identity function to return the id value

String insert_query =   " Insert into categories (categoryname, description) values ('it', 'internet '); "

+   " Select @ identity as 'identified '; " ;


// Perform database operations

Sqlcommand mycommand =   New Sqlcommand (insert_query, New Sqlconnection (connstr ));

Mycommand. Connection. open ();

Mylabel. Text = Mycommand. executescalar (). tostring ();

Mycommand. Connection. Close ();

}



</ Script >

</ Head >

< Body >

< Form ID = "Form1" Method = "Post" Runat = "Server" >

< H3 > Example of getting the inserted Record ID </ H3 >

The returned id value is:

< ASP: Label ID = "Mylabel" Runat = "Server" > </ ASP: Label >

</ Form >

</ Body >

</ Html >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.