title: FormatException when trying to write string to database
body: <p>I am writting a small web application and I want to add some details to my database using a web form. I am using asp.net and c# to create this web application and visual studio 2013 as developing tool. I am using a barcode scanner to scan the barcode from a product. Because i can not include a screenshot  from my user interface, i have 3 labels, 3 textboxes (Barcode,Description,Price), a add button and i have created a database to save my details. My 3 colums in the database is <code>Barcode</code>, <code>Description</code>, <code>Price</code> and their data types are <code>varchar(50)</code>, <code>varchar(50)</code>, <code>decimal(10,2)</code>. When I run the page and scan the barcode i get an exception:</p>
  
  <blockquote>
    <p>System.FormatException: Input string was not in a correct format.</p>
  </blockquote>
  
  <p>I have tried it without the barcode scanner and put the barcode by keyboard and is working fine. </p>
  
  <p>Here are  more details from the exception:</p>
  
  <blockquote>
    <h2>Server Error in '/' Application.</h2>
    
    <p>Input string was not in a correct format.   Description: An unhandled
    exception occurred during the execution of the current web request.
    Please review the stack </p>
    
    <p>trace for more information about the error and where it originated in
    the code. </p>
    
    <p>Exception Details: System.FormatException: Input string was not in a
    correct format.</p>
    
    <p>Source Error: </p>
    
    <p>Line 31:             con.Open(); Line 32:  Line 33:<br>
    da.InsertCommand.ExecuteNonQuery(); Line 34:  Line 35:<br>
    con.Close();</p>
    
    <p>Source File:  path.aspx.cs    Line:  33 </p>
    
    <p>Stack Trace: </p>
    
    <p>[FormatException: Input string was not in a correct format.]<br>
    System.Number.StringToNumber(String str, NumberStyles options,
    NumberBuffer&amp; number, NumberFormatInfo info, Boolean </p>
    
    <p>parseDecimal) +11177559    System.Number.ParseDecimal(String value,
    NumberStyles options, NumberFormatInfo numfmt) +172<br>
    System.Convert.ToDecimal(String value, IFormatProvider provider) +67<br>
    System.String.System.IConvertible.ToDecimal(IFormatProvider provider)
    +10    System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) +11043185<br>
    System.Data.SqlClient.SqlParameter.CoerceValue(Object value, MetaType
    destinationType, Boolean&amp; coercedToDataFeed, </p>
    
    <p>Boolean&amp; typeChanged, Boolean allowStreaming) +5332823</p>
    
    <p>[FormatException: Failed to convert parameter value from a String to a
    Decimal.]    System.Data.SqlClient.SqlParameter.CoerceValue(Object
    value, MetaType destinationType, Boolean&amp; coercedToDataFeed, </p>
    
    <p>Boolean&amp; typeChanged, Boolean allowStreaming) +5331815<br>
    System.Data.SqlClient.SqlParameter.GetCoercedValue() +185<br>
    System.Data.SqlClient.SqlParameter.Validate(Int32 index, Boolean
    isCommandProc) +102<br>
    System.Data.SqlClient.SqlCommand.BuildParamList(TdsParser parser,
    SqlParameterCollection parameters) +201<br>
    System.Data.SqlClient.SqlCommand.BuildExecuteSql(CommandBehavior
    behavior, String commandText, SqlParameterCollection </p>
    
    <p>parameters, _SqlRPC&amp; rpc) +237<br>
    System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior
    cmdBehavior, RunBehavior runBehavior, Boolean </p>
    
    <p>returnStream, Boolean async, Int32 timeout, Task&amp; task, Boolean
    asyncWrite, SqlDataReader ds) +1421<br>
    System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior
    cmdBehavior, RunBehavior runBehavior, Boolean </p>
    
    <p>returnStream, String method, TaskCompletionSource<code>1 completion, Int32
    timeout, Task&amp; task, Boolean asyncWrite) +177<br>
    System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource</code>1
    completion, String methodName, Boolean </p>
    
    <p>sendToPipe, Int32 timeout, Boolean asyncWrite) +208<br>
    System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +163<br>
    PosSystem.Stock.addStock.btnAdd_Click(Object sender, EventArgs e) in
    path\add.aspx.cs:33<br>
    System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9615682<br>
    System.Web.UI.WebControls.Button.RaisePostBackEvent(String
    eventArgument) +103<br>
    System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
    eventArgument) +10<br>
    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
    sourceControl, String eventArgument) +13<br>
    System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
    +35    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) </p>
    
    <p>+1724</p>
  </blockquote>
  
  <p>Any one has an idea why is that and how i can fix it please? </p>
  
  <p>Here is my code for when the button is clicked:</p>
  
  <pre><code>    protected void btnAdd_Click(object sender, EventArgs e)
      {
          int y;
          da.InsertCommand = new SqlCommand("INSERT INTO Products VALUES(@Barcode,@Description,@Price)", con);
          da.InsertCommand.Parameters.Add("@Barcode", SqlDbType.VarChar).Value = txtBarcode.Text;
          da.InsertCommand.Parameters.Add("@Description", SqlDbType.VarChar).Value = txtDescription.Text;
          da.InsertCommand.Parameters.Add("@Price", SqlDbType.Decimal).Value = txtPPrice.Text;
          con.Open();
  
          da.InsertCommand.ExecuteNonQuery();
  
          con.Close();
      }
  </code></pre>