PDA

View Full Version : Problem with SQL Update command


bkagan98
06-18-2004, 01:39 AM
I am trying to update a record in ms access database.
If new value (cDesc) contains apostrof (') code crashes.
Is there another way to do this?

code:
string _cSQL = "UPDATE Code SET" +
" Code.Desc = '" + cDesc.Trim() + "'" +
" WHERE Code.n_id = " + iCodeID.ToString();

Scott
06-18-2004, 04:06 AM
Try this:

Const q As String = Chr(34)
string _cSQL = "UPDATE Code SET Code.Desc = " + q + cDesc.Trim() + q + " WHERE Code.n_id = " + iCodeID.ToString();

If you are going to use this method, it's a good idea to define the constant q (for quote) as a global variable at the highest level you can.

Actually, a better way is to get in the habit of using the command object with parameters in sql insert and update actions.