Get identity after inserted to database C#

Untitled.png

con.Open();
SqlCommand cmd = new SqlCommand(“INSERT INTO dbo.tblMainStatus(user_Id,user_Name,Start_Time)VALUES(@user_Id,@user_Name,@Start_Time) SELECT SCOPE_IDENTITY();”, con);
cmd.Parameters.AddWithValue(“@user_Id”, txtUser_ID.Text);
cmd.Parameters.AddWithValue(“@user_Name”, txtUser_Name.Text);
cmd.Parameters.AddWithValue(“@Start_Time”, SqlDbType.Time).Value = txtStartTime.Text.ToString();
string query2 = “Select @@Identity as no from dbo.tblMainStatus”;
cmd.CommandText = query2;
cmd.CommandType = CommandType.Text;
decimal no = (decimal)cmd.ExecuteScalar();
con.Close();
txtIdentity.Text = no.ToString();

//using full.

Leave a comment