I have create custome module in umbraco, I am inserting product data into umbraco database, I want to get Identity value newly inserted product on Save button click event.
My code is here.
int rows = SqlHelper.ExecuteNonQuery(string.Format("insert into products(SubCategoryId,Name,ShortDescription) values({0},'{1}','{2}')",p.SubCategoryId, p.Name, p.ShortDescription)); if (rows > 0) { int id = SqlHelper.ExecuteScalar<int>("select @@IDENTITY"); return id; } return 0;
Get Identity value by SqlHelper.ExecuteNonQuery
Hello friends.
I have create custome module in umbraco, I am inserting product data into umbraco database, I want to get Identity value newly inserted product on Save button click event.
My code is here.
int rows = SqlHelper.ExecuteNonQuery(string.Format("insert into products(SubCategoryId,Name,ShortDescription) values({0},'{1}','{2}')",p.SubCategoryId, p.Name, p.ShortDescription));
if (rows > 0)
{
int id = SqlHelper.ExecuteScalar<int>("select @@IDENTITY");
return id;
}
return 0;
Thanks
Imrankhan
Hi,
I think this is more of a .NET question than Umbraco-specific, so maybe these posts will help:
http://www.mikesdotnetting.com/Article/54/Getting-the-identity-of-the-most-recently-added-record
http://stackoverflow.com/questions/8493127/returning-scope-identity-using-sqlcommand-executenonquery
http://forums.asp.net/t/1728650.aspx/1
-Tom
Actually I was talking about umbraco SqlHelper class.
umbraco.BusinessLogic.Application.SqlHelper.ExecuteNonQuery("CommandText");
Anyways, I got this solution by umbraco.BusinessLogic.Application.SqlHelper.ExecuteScalar<int>("Insert Statement; select @@IDENTITY;");
Thanks
Imrankhan
is working on a reply...