title: The parameterized query ..... expects the parameter '@units', which was not supplied
body: <p>I'm getting this exception:</p>
  
  <blockquote>
    <p>The parameterized query '(@Name nvarchar(8),@type nvarchar(8),@units nvarchar(4000),@rang' expects the parameter '@units', which was not supplied.</p>
  </blockquote>
  
  <p>My code for inserting is:</p>
  
  <pre><code>public int insertType(string name, string type, string units = "N\\A", string range = "N\\A", string scale = "N\\A", string description = "N\\A", Guid guid = new Guid())
  {
      using (SqlConnection connection = new SqlConnection(connectionString))
      {
          connection.Open();
          SqlCommand command = new SqlCommand();
          command.CommandText = "INSERT INTO Type(name, type, units, range, scale, description, guid) OUTPUT INSERTED.ID VALUES (@Name, @type, @units, @range, @scale, @description, @guid) ";
          command.Connection = connection;
          command.Parameters.AddWithValue("@Name", name);
          command.Parameters.AddWithValue("@type", type);
          command.Parameters.AddWithValue("@units", units);
          command.Parameters.AddWithValue("@range", range);
          command.Parameters.AddWithValue("@scale", scale);
          command.Parameters.AddWithValue("@description", description);
          command.Parameters.AddWithValue("@guid", guid);
          return (int)command.ExecuteScalar();
      }
  }
  </code></pre>
  
  <p>The exception was a surprise because I'm using the <code>AddWithValue</code> function and making sure I added a default parameters for the function. </p>
  
  <p>SOLVED:</p>
  
  <p>The problem was that the some parameters where empty Strings (that override the default)</p>
  
  <p>This is the working code: </p>
  
  <pre><code>public int insertType(string name, string type, string units = "N\\A", string range = "N\\A", string scale = "N\\A", string description = "N\\A", Guid guid = new Guid())
      {
          using (SqlConnection connection = new SqlConnection(connectionString))
          {
              connection.Open();
              SqlCommand command = new SqlCommand();
              command.CommandText = "INSERT INTO Type(name, type, units, range, scale, description, guid) OUTPUT INSERTED.ID VALUES (@Name, @type, @units, @range, @scale, @description, @guid) ";
              command.Connection = connection;
              command.Parameters.AddWithValue("@Name", name);
              command.Parameters.AddWithValue("@type", type);
  
              if (String.IsNullOrEmpty(units))
              {
                  command.Parameters.AddWithValue("@units", DBNull.Value); 
              }
              else
                  command.Parameters.AddWithValue("@units", units);
              if (String.IsNullOrEmpty(range))
              {
                  command.Parameters.AddWithValue("@range", DBNull.Value);
              }
              else
                  command.Parameters.AddWithValue("@range", range);
              if (String.IsNullOrEmpty(scale))
              {
                  command.Parameters.AddWithValue("@scale", DBNull.Value);
              }
              else
                  command.Parameters.AddWithValue("@scale", scale);
              if (String.IsNullOrEmpty(description))
              {
                  command.Parameters.AddWithValue("@description", DBNull.Value);
              }
              else
                  command.Parameters.AddWithValue("@description", description);
  
  
  
  
              command.Parameters.AddWithValue("@guid", guid);
  
  
              return (int)command.ExecuteScalar();
          }
  
  
      }
  </code></pre>