If the command doesn't perform an insertion into a table the value of InsertOid won't be defined.
static void GetLastId(PgSqlConnection pgConnection) { PgSqlCommand pgCommand = new PgSqlCommand("INSERT INTO Test.Dept (DeptNo, DName, Loc) VALUES (Null, 'AnotherOne', 'SomeWhere')", pgConnection); pgConnection.Open(); try { pgCommand.ExecuteNonQuery(); Console.WriteLine(pgCommand.InsertOid); } finally { pgConnection.Close(); } }
Public Sub GetLastId(ByVal pgConnection As PgSqlConnection) Dim pgCommand As New PgSqlCommand("INSERT INTO Test.Dept (DeptNo, DName, Loc) VALUES (Null, 'AnotherOne', 'SomeWhere')", pgConnection) pgConnection.Open() Try pgCommand.ExecuteNonQuery() Console.WriteLine(pgCommand.InsertOid) Finally pgConnection.Close() End Try End Sub