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
  • 704,970
  • 168
  • 1,303
  • 1,425
Thijser
  • 2,485
  • 1
  • 32
  • 66

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
  • 42,334
  • 17
  • 98
  • 130