Tuesday, July 22, 2014

Allow single quotation in a string for Insert or update query in C#

While inserting the data into database from application sometimes we encounter with the error of single quotation exists in the input data. Single quotation is the special character used to hold the string value in database (SQL). Here is the simple technique by which we can insert the details with single quotation into the input data.

Code language C#.

public string AllowSingleQouteToInsertOrUpdate(string inputValue)
        {
            string outputValue = string.Empty;
            if ((inputValue.IndexOf("'", 0) > 0) || (inputValue.IndexOf("'", 0) == 0))
            { outputValue = inputValue.Replace("'", "''"); }
            else
            { outputValue = inputValue; }
            return outputValue;
        }

No comments:

Post a Comment

Put your comments here

Motivational qoutes

पूरे विश्वास के साथ अपने सपनों की तरफ बढ़ें। वही ज़िन्दगी जियें जिसकी कल्पना आपने की है।