1

I'm working on a system that increments has an auto incrementing index used as an identifier. But this gives me a problem as I would like to have the identifiers after doing the insert.

What I have in C# is:

string commandtxt = "INSERT INTO DATA(POINT) VALUES(@DP)";

SqlConnection conn = new SqlConnection(WebConfigurationManager.AppSettings["ConnectionInfo"].ToString());

SqlCommand sqlCommand = new SqlCommand(commandtxt, conn);
sqlCommand.Parameters.AddWithValue("@DP", point);

conn.Open();
sqlCommand.ExecuteNonQuery();
int record id = ???? 
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Thijser
  • 2,625
  • 1
  • 36
  • 71

1 Answers1

4

replace

 string commandtxt = "INSERT INTO DATA(POINT) VALUES(@DP)";
 sqlCommand.ExecuteNonQuery();

with

string commandtxt = "INSERT INTO DATA(POINT) VALUES(@DP) SELECT SCOPE_IDENTITY()";
int ID = (int)sqlCommand.ExecuteScalar();
fubo
  • 44,811
  • 17
  • 103
  • 137