In this article learn how to get last inserted value in a table. When we insert data into table we get the last identity value using @@IDENTITY.
private void insertProduct()
{
SqlConnection myConnection;
try
{
myConnection = new SqlConnection((string)Session["DBConnector"]);
myConnection.Open();
StringBuilder sql = new StringBuilder();
SqlCommand myCommand = new SqlCommand("usp_InsertProduct", myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.Clear();
myCommand.Parameters.Add("@Name", txtName.Text);
myCommand.ExecuteNonQuery();
myCommand = new SqlCommand("SELECT @@IDENTITY", myConnection);
int ProductId = Convert.ToInt32(myCommand.ExecuteScalar());
}
catch (Exception ex)
{
//Something went wrong
throw;
}
finally
{
myConnection.Close();
//Finally, close the connection
}
}
Post your comments / questions
Recent Article
- ModuleNotFounEerror:No module named celery in Django Project
- How to get domain name information from a Domain using Python
- ModulenotFoundError: no module named 'debug_toolbar' -SOLUTION
- How to create superuser in django project hosted in cPanel without terminal
- CSS & images not loading in django admin | cpanel without terminal
- Could not build wheels for mysqlclient, which is required to install pyproject.toml-based projects
- How to sell domain name on Godaddy (2023)
- TemplateSyntaxError at / Could not parse the remainder: ' + 1' from 'forloop.counter0 + 1'
Related Article