Start by fixing the
SQL Injection[
^] vulnerability in your code.
Then, make the
VId
column
an IDENTITY
column[
^].
Finally, insert the record
without specifying a value for the
VId
column, and return the
Scope_Identity()
value to get the auto-generated ID.
using (var connection = new SqlConnection("YOUR CONNECTION STRING HERE"))
using (var command = new SqlCommand("INSERT INTO tbladdcart (item_id, quant, ad) VALUES (@ItemId, @quant, @ad); SELECT Scope_Identity();", connection))
{
command.Parameters.AddWithValue("@ItemId", e.CommandArgument);
command.Parameters.AddWithValue("@quant", lblPrice.Text);
command.Parameters.AddWithValue("@ad", DateTime.Now);
int id = (int)command.ExecuteScalar();
Response.Cookies["addcart"].Value = id.ToString();
Response.Cookies["addcart"].Expires = DateTime.Now.AddDays(1);
Response.Redirect("~/Restaurant_menu.aspx");
}