Share The Code Concept

418days since
Republic Day

SharePoint Utility

Create a sharepoint Utility to make the webservice deployment eazier.
C#.NET‎ > ‎

Handling single quotes in C# .net

posted Oct 15, 2008 6:11 AM by Praveen Krishna R
While building some dynamic strings for queries or something you should keep in mind that the single quotes / double quotes will definitely make some problems. 

Code which results in Bug

drPosts = oMainDS.Tables["BlogPostslist"].Select("PostCategory = '" + CategoryName + "'");

In the above case we placed the quotes just around the String Value.
But what happens if it contains a single Quotes. It will Throw Exception, for sure.
The solution which I found on GOOGLE is given below.

Solution

drPosts = oMainDS.Tables["BlogPostslist"].Select("PostCategory = '" + CategoryName.Replace("'", "''") + "'");

Yes replace the lonely single quotes with a couple of single quotes. 
And it resolved my problem.