I'm having trouble getting a return value from SQL Server with the ID of the row it just updated.
I want to get the id of the row I just updated to use in the following query, but when I execute this method the first T-SQL statements update the row fine but the object activeRecord
does not get the row number returned (no value)
If I run the T-SQL statement in SQL directly, I do get a return value. I've used ExecuteScalar
elsewhere in my code without issue.
What am I missing here?
if (Regex.IsMatch(Encoding.ASCII.GetString(udpMessageReceived), "Valitating"))
{
//a device is validating itself following endpoint registration
string[] splitaction = Encoding.ASCII.GetString(udpMessageReceived).Split(deliminator);
using (var conn = new SqlConnection(Globals.ConnectionString))
{
conn.Open();
object activeRecord;
using (var comm = new SqlCommand("UPDATE oView_MachineRegister SET validated= 1 where hostname= @hostname; SELECT ID = SCOPE_IDENTITY()", conn))
{
comm.Parameters.AddWithValue("datenow", DateTime.Now);
comm.Parameters.AddWithValue("hostname", splitaction[1]);
activeRecord = comm.ExecuteScalar();
}
using (var comm2 = new SqlCommand("DELETE from oView_OutBoundMessageQ WHERE machines = @activerecord AND command = 'Validate'", conn))
{
comm2.Parameters.AddWithValue("activerecord", activeRecord);
comm2.ExecuteNonQuery();
}
conn.Close();
}
}