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.
Problem: I am getting an error
SqlCommand.Prepare method requires all variable length parameters to have an explicitly set non-zero Size
on the line with this statement
cmd.Prepare();
DB schema:
Code:
//alreadyAnsweredQues is Global -- List<Int32>
var parameters = new string[alreadyAnsweredQues.Count];
var cmd = new SqlCommand();
for (int i = 0; i < 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();