Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your sql insert text doesn't use parameters.<br> This is the cause of bugs and worse <a href="http://en.wikipedia.org/wiki/SQL_injection" rel="nofollow">(SqlInjection)</a></p> <p>Change your code in this way;</p> <pre><code>using(OleDbConnection myCon = new OleDbConnection(ConfigurationManager.ConnectionStrings["DbConn"].ToString())) { OleDbCommand cmd = new OleDbCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = "insert into Items ([Item_Name],[Item_Price]) values (?,?); cmd.Parameters.AddWithValue("@item", itemNameTBox.Text); cmd.Parameters.AddWithValue("@price", Convert.ToDouble(itemPriceTBox.Text)); cmd.Connection = myCon; myCon.Open(); cmd.ExecuteNonQuery(); System.Windows.Forms.MessageBox.Show("An Item has been successfully added", "Caption", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); } </code></pre> <p>Of course this assumes that the text box for price contains a correct numeric value.<br> To be sure add this line before calling the code above</p> <pre><code>double price; if(double.TryParse(itemPriceTBox.Text, out price) == false) { MessageBox.Show("Invalid price"); return; } </code></pre> <p>then use <code>price</code> as value for the parameter <code>@price</code></p> <p>**EDIT 4 YEARS LATER ** </p> <p>This answer needs an update. In the code above I use AddWithValue to add a parameter to the Parameters collection. It works but every reader should be advised that AddWithValue has some drawbacks. In particular if you fall for the easy path to add just strings when the destination column expects decimal values or dates. In this context if I had written just </p> <pre><code>cmd.Parameters.AddWithValue("@price", itemPriceTBox.Text); </code></pre> <p>the result could be a syntax error or some kind of weird conversion of the value and the same could happen with dates. AddWithValue creates a string Parameter and the database engine should convert the value to the expected column type. But differences in locale between the client and the server could create any kind of misinterpretation of the value.</p> <p>I think that it is always better to use </p> <pre><code>cmd.Parameters.Add("@price", OleDbType.Decimal).Value = Convert.ToDecimal(itemPriceTBox.Text); </code></pre> <p>More info on <a href="http://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/" rel="nofollow">AddWithValue problems can be found here</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload