title: SqlCommand.Prepare method requires all parameters to have an explicitly set type
body: <p>I have the following snippet of code in my WCF web service that builds a set of where conditions according to the formatting of the values of a provided dictionary.</p>
  
  <pre><code>public static Dictionary&lt;string, string&gt;[] VehicleSearch(Dictionary&lt;string, string&gt; searchParams, int maxResults)
  {
      string condition = "";
      foreach (string key in searchParams.Keys)
      {
          //Split out the conditional in case multiple options have been set (i.e. SUB;OLDS;TOY)
          string[] parameters = searchParams[key].Split(';');
          if (parameters.Length == 1)
          {
              //If a single condition (no choice list) check for wildcards and use a LIKE if necessary
              string predicate = parameters[0].Contains('%') ? " AND {0} LIKE @{0}" : " AND {0} = @{0}";
              condition += String.Format(predicate, key);
          }
          else
          {
              //If a choice list, split out query into an IN condition
              condition += string.Format(" AND {0} IN({1})", key, string.Join(", ", parameters));
          }
      }
  
      SqlCommand cmd = new SqlCommand(String.Format(VEHICLE_QUERY, maxResults, condition));
      foreach (string key in searchParams.Keys)
          cmd.Parameters.AddWithValue("@" + key, searchParams[key]);
      cmd.Prepare();
  </code></pre>
  
  <p>Note that the values in the dictionary are explicitly set to strings and that they are the only items being sent into the <code>AddWithValue</code> statements. This produces SQL like this:</p>
  
  <pre><code>SELECT TOP 200 MVINumber AS MVINumber
      , LicensePlateNumber
      , VIN
      , VehicleYear
      , MakeG
      , ModelG
      , Color
      , Color2
  FROM [Vehicle_Description_v]
  WHERE 1=1 AND VIN LIKE @VIN
  </code></pre>
  
  <p>And errors out saying that </p>
  
  <blockquote>
    <p><em>System.InvalidOperationException: SqlCommand.Prepare method requires
    all parameters to have an explicitly set type.</em></p>
  </blockquote>
  
  <p>All searching that I have done says that I need to tell <code>AddWithValue</code> the type of values that I'm preparing, but all of my prepared values are strings and all examples I've seen don't perform anything extra when they're dealing with strings. What am I missing?</p>