I am looking to know how to Store a newly created ID(record) in sql server DB into a Session Variable. ( or other option)
For example:
I am using asp.Net Web Forms with a sql server Database
i have a web page where users enter in details of a booking and then click a addBooking button which stores this to my DB.
I am looking to know how to capture this Newly created ID to display it after that button is submitted
protected void AddBooking_Click(object sender, EventArgs e)
{
//Creating a connection to my database using the connection string
string cs = ConfigurationManager.ConnectionStrings["BookingDb"].ConnectionString;
SqlConnection con = new SqlConnection(cs);
//preparing a query which will insert the data entered in the textboxes to the database
SqlCommand cmd = new SqlCommand("[dbo].[spAddBooking]", con);
cmd.CommandType = CommandType.StoredProcedure;
try
{
cmd.Parameters.AddWithValue("@UserId", UserID.Text.Trim());
cmd.Parameters.AddWithValue("@MemberType", ddlMemberType.SelectedValue);
cmd.Parameters.AddWithValue("@PitchSection", ddlSyntheticPitch.SelectedValue);
cmd.Parameters.AddWithValue("@Date", txtDate.Text.Trim());
cmd.Parameters.AddWithValue("@StartTime", ddlStartTime.SelectedValue);
cmd.Parameters.AddWithValue("@EndTime", ddlEndTime.SelectedValue);
cmd.Parameters.AddWithValue("@Description", txtDescription.InnerText.Trim());
con.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Response.Write(ex);
}
finally
{
// SetSession();
con.Close();
Response.Redirect("WebForm2.aspx");
}
}
// Get the ID of the inserted record in the database

Prakash ChasiyaRank #1,041 · 1.8K reputationPosted Feb 27, 2018, 5:58 PM EST
Pankaj Kumar ChoudharyRank #80 · 24.4K reputationPosted Feb 27, 2018, 3:23 PM EST
Nitin SontakkeRank #264 · 9.6K reputationPosted Feb 28, 2018, 3:03 AM EST
Jennifer ODonovan29 reputationPosted Feb 28, 2018, 3:59 AM EST
Nitin SontakkeRank #264 · 9.6K reputationPosted Feb 28, 2018, 4:49 AM EST
Mageshwaran RRank #411 · 6.1K reputationPosted Nov 22, 2019, 6:31 AM EST