title: ASP.NET ERROR: SqlCommand.Prepare method requires all variable length parameters to have an explicitly set non-zero Size
body: <p>First of all I have to take obvious things out of the way; I have searched for the same problem, there is an answer to this question on StackOverFlow but it didn't worked for me.</p>
  
  <p><strong>Problem:</strong> I am getting an error </p>
  
  <blockquote>
    <p>SqlCommand.Prepare method requires all variable length parameters to have an explicitly set non-zero Size</p>
  </blockquote>
  
  <p>on the line with this statement </p>
  
  <pre><code>cmd.Prepare();
  </code></pre>
  
  <p>DB schema:</p>
  
  <p><a href="https://i.stack.imgur.com/qEAjP.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qEAjP.jpg" alt="enter image description here"></a></p>
  
  <p>Code:</p>
  
  <pre><code>//alreadyAnsweredQues is Global -- List&lt;Int32&gt;
  var parameters = new string[alreadyAnsweredQues.Count];
  var cmd = new SqlCommand();
  
  for (int i = 0; i &lt; alreadyAnsweredQues.Count; i++)
  {
      parameters[i] = string.Format("@qid{0}", i);
  
      cmd.Parameters.Add(parameters[i].ToString(), SqlDbType.Int).Value = alreadyAnsweredQues[i];
  }
  
  cmd.Parameters.Add("@cid", SqlDbType.Int).Value = Convert.ToInt32(Session["c_id"].ToString());
  
  cmd.CommandText = string.Format("SELECT TOP 1 * FROM questions WHERE c_id = @cid AND q_id NOT IN ({0}) ORDER BY NEWID() ;", string.Join(", ", parameters));
  System.Diagnostics.Debug.WriteLine(string.Format("SELECT TOP 1 * FROM questions WHERE c_id = @cid AND q_id NOT IN ({0}) ORDER BY NEWID() ;", string.Join(", ", parameters)));
  
  cmd.Connection = new SqlConnection(ConnectionClass.constr);
  cmd.Connection.Open();
  
  cmd.Prepare();
  
  SqlDataReader sdr = cmd.ExecuteReader();
  </code></pre>