title: Using a SubQuery in an Insert Statement in SQL Server 2005
body: <p>I have a carsale project. It completely works on localhost. I have a "AddCar.aspx" page that inserts a car record with car's features. Car features are selected with checkboxes. If i don't check any checkbox, there is no problem. But if i check one of feature checkboxes, my page gives an error like this:</p>
  
  <blockquote>
    <p>"Subqueries are not allowed in this
    context. Only scalar expressions are
    allowed."</p>
  </blockquote>
  
  <p>And my code is like that:</p>
  
  <pre><code>foreach (DataListItem item in Security1.Items) {
          CheckBox CheckBox1 = (CheckBox)item.FindControl("CheckBox1");
  
          if (CheckBox1.Checked) {
              HiddenField h = (HiddenField)item.FindControl("FeaID");
              string add = "Insert into Carfeature (RecID,FeatureID) values ((select Max(RecID) from record),@FeatureID)";
              cmd[k] = new SqlCommand();
              cmd[k].CommandType = CommandType.Text;
              cmd[k].Parameters.Add("@FeatureID", SqlDbType.Int).Value = h.Value;
              cmd[k].CommandText = add;
              k++;
          }
  }
  </code></pre>
  
  <p>Is there any solution?</p>