Note that there are some explanatory texts on larger screens.

plurals
  1. POReplace ' with \' in all textboxes in my program
    text
    copied!<p>So throughout my program I have probably 50 or so text boxes in various places. I know how to replace a single quote with something (in this case ' -> \'), but I am thinking there must be a better way to do this than go through and add specific code for every single text box. I have to do this because when this stuff is getting sent to the database, if there is a single quote, it gives an error. Is there a way to change the default TextBox control behavior so that all textboxes in the program automatically replace all single quotes with \'?</p> <p>EDIT:</p> <pre><code> string statement = "select count(*) from users where username='@username'"; MySqlCommand command = new MySqlCommand(statement, conn); command.Parameters.AddWithValue("@username", username); if (Convert.ToInt32(command.ExecuteScalar()) == 1) </code></pre> <p>I have been playing with the paramerterized code and this is what I have right now. From how I understand it, the statement string is basically the same as before, but where I used to have the variable "username" I know use a parameter (which I called @username), then the command is created. Then in the parameters.addwithvalue, it replaces the parameter username, with whatever is in the variable username. Unfortunately, this is not working for me, and I don't really see how it helps because username is still just getting stuck in the command?</p> <p>EDIT: Found the problem, in the statement you don't need to put single quotes around '@username'</p> <p>so it should be:</p> <pre><code>string statement = "select count(*) from users where username=@username"; </code></pre>
 

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