Apostrophe in SQL Search String

string sqlString = "SELECT * FROM Customers WHERE Name='" + 
		 txtName.Text + "'";

Here if

txtName.text = "O'Brian";

We will get an error as the Apostrophe will split the sql statement. Simply replace the Apostrophe (’) with twho Apostrophes (”) and pass the sql statement.

sqlString = sqlString .Replace("'","''");

Leave a Reply

You must be logged in to post a comment.