Reputation: 15965
I am trying to get the id of the newly inserted record from the database using the sql output clause, i.e.
insert into table1(forename, surname) output inserted.id values(@forename, @surname)
and I am trying to capture the id like this:
Int32 id = (int)command.ExecuteScalar;
This is giving me an error message:
Compiler Error Message: CS0428: Cannot convert method group 'ExecuteScalar' to non-delegate type 'int'. Did you intend to invoke the method?
Not sure what I am doing wrong.
Here is a large extract of the existing code:
using (connection = new SqlConnection(ConfigurationManager.AppSettings["connString"]))
{
using (command = new SqlCommand("insert into table1(forename, surname) OUTPUT INSERTED.ID values(@forename, @surname)", connection))
{
command.Parameters.Add("@forename", SqlDbType.VarChar, 255).Value = forename;
command.Parameters.Add("@surname", SqlDbType.VarChar, 255).Value = surname;
command.Connection.Open();
id = (int)command.ExecuteScalar;
}
}
Upvotes: 1
Views: 583
Reputation: 13104
ExecuteScalar is a method, so you have to put an argument list after it: ExecuteScalar().
Any time you get an error about MethodGroups, this is probably the mistake.
Upvotes: 3
Reputation: 19308
Try id = (int) command.ExecuteScalar()
. You just left the parens off.
Upvotes: 5