I have this piece of code:
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = connection;
cmd.CommandText = "UPDATE S " +
"SET S.WebId = S.WebId + 1 " +
"OUTPUT DELETED.WebId " +
"FROM jcdSetting S";
SqlParameter parameter = cmd.Parameters.Add("@id", SqlDbType.Int);
parameter.Direction = ParameterDirection.Output;
int i = cmd.ExecuteNonQuery();
webId = Convert.ToInt32(cmd.Parameters["@id"].Value);
}
For some reason, the last line fails, the parameter I'm trying to access is always DbNull. I've tried the query in Management Studio and it returns the value just fine.
I've checked the return value of ExecureNonQuery and that as well returns 1 as expected. I'm totally lost here.
Any help would be appreciated.
OUTPUTdoes not define an output parameter (those are only for stored procedures); examine the result set instead. Incidentally, yourUPDATEhas noWHERE. This can work correctly only ifjcdSettinghas only a single row, otherwise you'll get back a table of updated values.