title: Convert a object variable to a DBType before setting object as value of a parameter?
body: <p>i am using a SqlCommand to execute a stored procedure. The parameters are created automatically based on the definition of the stored procedure by using the <code>DeriveParameters</code> method of class <code>SqlCommandBuilder</code>. This automatically sets the DbType for me. Next I loop through the dictionary with <code>&lt;string, object&gt;</code> key-value pairs where the string is the name of the parameter and the object contains its value to set.</p>
  
  <p>Simplied example source:</p>
  
  <pre><code>public DataTable FetchProducts(SqlConnection sqlConn, IDictionary&lt;string, object&gt; paramvalues)
  {
      using (SqlCommand cmd = new SqlCommand("ProcFetchProducts", sqlConn))
      {
          cmd.CommandType = CommandType.StoredProcedure;
          SqlCommandBuilder.DeriveParameters(cmd);
  
          foreach (KeyValuePair&lt;string, object&gt; pair in paramvalues)
          {
              var index = cmd.Parameters.IndexOf(pair.Key);
              cmd.Parameters[index].Value = pair.Value;
          }
  
          using (var dr = cmd.ExecuteReader())
          {
              var dt = new DataTable("Result");
              dt.Load(dr);
              return dt;
          }
      }
  }
  </code></pre>
  
  <p>Sometimes the object contains a value which does not match the parameter's DBType. For instance, a parameter is of type smallint and the object contains a string. Now when I execute the datareader, I get a "input string is not in a correct format" <code>FormatException</code>, which does not tell me which parameter is causing this problem. </p>
  
  <p>So my main question is: is there a way to cast the object from the dictionary to the DBType defined in the parameter, so I can check if it is of the right type before executing the datareader?</p>