title: Failed to convert parameter value from a String to a Int32
body: <p>I'm using MVC3 and SQL 2005.  I was getting error: "Error converting data type varchar to numeric." I believe this was being generated because I was using "1" instead of "1.00000" for QTYORDER.  Using "1.00000" for QTYORDER I now see the error: "Failed to convert parameter value from a String to a Int32."</p>
  
  <p>I've tried using Convert.ToInt32(Variable) like below:</p>
  
  <p><code>cmd.Parameters.Add("@QTYSOLD", SqlDbType.Int).Value = Convert.ToInt32(QTYORDER);</code></p>
  
  <p>Which returns: "Input string was not in a correct format."  Int.Parse(QTYORDER) does the same thing...</p>
  
  <p>The SQL Stored Proc Variables:</p>
  
  <pre><code>Procedure [dbo].[SVC_AddPart_To_Service_Call]      
      (@SRVRECTYPE smallint,
       @CALLNBR char(11),
       @TECHID char(11),
       @LINITMTYP char(3),
       @ITEMNMBR char(31),
       @QTY numeric(19,5) = 0,
       @QTYSOLD numeric(19,5) = 0,
       @STARTDATE datetime = '01/01/1900 00:00:00',   
       @STARTTIME datetime = '01/01/1900 00:00:00',
       @ENDDATE datetime = '01/01/1900 00:00:00',
       @ENDTIME datetime = '01/01/1900 00:00:00',
       @Status int output) As ...
  </code></pre>
  
  <p>The MVC C# Code:</p>
  
  <pre><code>SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CPLU"].ConnectionString);
  var cmd = new SqlCommand("SVC_AddPart_To_Service_Call", conn);
  cmd.CommandType = CommandType.StoredProcedure;
  
  cmd.Parameters.Add("@SRVRECTYPE", SqlDbType.SmallInt).Value = "2";
  cmd.Parameters.Add("@CALLNBR", SqlDbType.Char).Value = CALLNBR;
  cmd.Parameters.Add("@TECHID", SqlDbType.Char).Value = Session["TechID"];
  cmd.Parameters.Add("@LINITMTYP", SqlDbType.Char).Value = "P";
  cmd.Parameters.Add("@ITEMNMBR", SqlDbType.Char).Value = ITEMNMBR;
  cmd.Parameters.Add("@QTY", SqlDbType.Int).Value = QTYORDER;
  cmd.Parameters.Add("@QTYSOLD", SqlDbType.Int).Value = QTYORDER;
  cmd.Parameters.Add("@STARTDATE", SqlDbType.DateTime).Value = "1900-01-01 00:00:00.000";
  cmd.Parameters.Add("@STARTTIME", SqlDbType.DateTime).Value = "1900-01-01 00:00:00.000";
  cmd.Parameters.Add("@ENDDATE", SqlDbType.DateTime).Value = "1900-01-01 00:00:00.000";
  cmd.Parameters.Add("@ENDTIME", SqlDbType.DateTime).Value = "1900-01-01 00:00:00.000";
  cmd.Parameters.Add("@STATUS", SqlDbType.Int);
  cmd.Parameters["@STATUS"].Direction = ParameterDirection.Output;
  
  conn.Open();
  cmd.ExecuteNonQuery();
  conn.Close();
  </code></pre>
  
  <p>The MVC Razor Code:</p>
  
  <pre><code>@using (Html.BeginForm()) 
  {
      @Html.ValidationSummary(true)
      &lt;fieldset&gt;
  
              @Html.LabelFor(model =&gt; model.CALLNBR, "Call Number")
              @Html.Label("CALLNBR", (string)ViewBag.CALLNBR)
              @Html.ValidationMessageFor(model =&gt; model.ITEMDESC)
          &lt;br /&gt;
              @Html.LabelFor(model =&gt; model.ITEMNMBR, "Item Number")
              @Html.DropDownList("ITEMNMBR", (SelectList) ViewBag.Items, "Please Select a Part #")
              @Html.ValidationMessageFor(model =&gt; model.ITEMNMBR)
          &lt;br /&gt;
              @Html.LabelFor(model =&gt; model.ITEMDESC, "Description")
              @Html.EditorFor(model =&gt; model.ITEMDESC)
              @Html.ValidationMessageFor(model =&gt; model.ITEMDESC)
          &lt;br /&gt;
              @Html.LabelFor(model =&gt; model.QTYORDER, "Quantity")
              @Html.EditorFor(model =&gt; model.QTYORDER, new { required = "required"})
              @Html.ValidationMessageFor(model =&gt; model.QTYORDER)
          &lt;br /&gt;
              &lt;input type="submit" class="submit" value="Add Part" /&gt;
  
      &lt;/fieldset&gt;
  }
  </code></pre>
  
  <p>Values I'm trying to pass through the form:</p>
  
  <pre><code>CALLNBR = 0000198210 
  ITEMNMBR = RF5-1885 (22 trailing spaces)
  QTYORDER = 1
  </code></pre>
  
  <p>Values taken from a line in the SQL Table:</p>
  
  <pre><code>CALLNBR = 0000198638  
  ITEMNMBR = RM1-0043 (22 trailing spaces)
  QTYORDER = 1.00000
  </code></pre>
  
  <p>Questions:</p>
  
  <p>1) Since the SP has default values coded for STARTDATE, STARTTIME, ENDDATE, and ENDTIME... do I need to supply those values when adding the parameters on the MVC App?</p>
  
  <p>2) How would I go about using a Try-Catch here and would that give me a better error message?  Something I could actually follow up on?</p>
  
  <p>3) Is there a way to cast the QTYORDER value to 5 decimal places in C#?  This would make sure it submits in the #.00000 format...</p>
  
  <p>Considerations:</p>
  
  <p>I'm using Entity Framework, but I can't map the Stored Proc to the Select Insert Function for that table because that SP is used for adding a part, and another SP is used for adding labor.</p>