I want to write a IFEXISTS condition and update the record from the code behind I don’t know whether its possible or not…
if its possible then someone please tell me the syntax for that..
How to update the record..
I want to write something like this.. but what is correct?
public void UpdateModalitiesId(int? CaseId, int ModalitiesId)
{
string query = "if exists (select count(*) from ImageModality where ImageModality.Id='"+ ModalitiesId +"')
UPDATE ImageGroup set ImageModalityId='" + ModalitiesId + "' where BaseCaseId='" + CaseId +"' ;
//string query = "UPDATE ImageGroup set ImageModalityId='" + ModalitiesId + "' where BaseCaseId='" + CaseId + "'";
SqlHelper.ExecuteNonQuery(strConnectionString, CommandType.Text, query);
}
First, the statement select count(*) will always return at least one record, so your condition will always return TRUE. So you need condition like
Next, if you need to update the record using your condition, you can use the IF .. BEGIN .. END statement. It is not necessarily to use BEGIN .. END, You can use your UPDATE immediatelly after IF condition, but in future, if you will modify your code, you can avoid the logic errors.
So, your code will be like this:
EDIT: And the correct code is