Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8226765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:53:22+00:00 2026-06-07T15:53:22+00:00

getting Error while executing this query , because column text may contain text with

  • 0

getting Error while executing this query , because column text may contain text with single quotes also. How can i use this query w/o any error
My code is

public bool updateCMStable(int id, string columnName, string columnText)
{
    try
    {
        string sql = "UPDATE  CMStable SET " + columnName + 
                      "='" + columnText + "' WHERE cmsID=" + id;
        int i = SqlHelper.ExecuteNonQuery(Connection.ConnectionString,
                                          CommandType.Text,
                                          sql);
        if (i > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    catch (Exception ee)
    {
        throw ee;
    }
} 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T15:53:24+00:00Added an answer on June 7, 2026 at 3:53 pm

    To fix your code, escape all single quotes with an additional single quote. However I agree with Oded… you need to be using a parameterized query, or possibly a stored proc.

    public bool updateCMStable(int id, string columnName, string columnText) 
    { 
       if(!string.IsNullOrEmpty))
       {
           switch(columnName)
           {
               // TODO: change 50 & 100 to the real sizes of your columns, 
               // and obviously the column names too...
               case "column1":
                   if(columnText.Length > 50)
                       columnText = columnText.SubString(0, 50);
                   break;
               case "column2":
                   if(columnText.Length > 100)
                       columnText = columnText.SubString(0, 100);
                   break;
               etc... 
            }
        }
        // replace single quote with double single quotes
        columnText = columnText.Replace("'", "''");
        string sql = string.Format("UPDATE CMStable SET {0} = '{1}' WHERE cmsID={2}", 
            columnName, 
            columnText, 
            id); 
        int i = SqlHelper.ExecuteNonQuery(Connection.ConnectionString, CommandType.Text, sql); 
        return (i > 0); 
    }
    

    I made some additional corrections to your code.

    1. You can simply return the result of an if statement when you’re returning true | false
    2. You don’t need to catch exceptions if you’re simply going to throw it in the catch block
    3. If you DO catch an exception, do something meaningful with it, and decide to rethrow it, use throw by itself, or you’ll reset the stack trace. Don’t use throw ee;
    4. Replace + type concatination with string.Format if it becomes too unreadable.

    Edit:

    The error you posted is happening because the length of the data being passed in is longer than the specified length of the column. Since you’re using dynamic SQL, the only way around it that I can see is to use a case statement. Each field may have a different size, or maybe not, but the string will have to be truncated to fit to avoid the error. If all the field sizes are the same, you won’t need the case statement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting this Error while executing my android application : Host is unresolved:
I am getting following error while executing my java code. What is this error
Getting this error: java.lang.RuntimeException: An error occured while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:200) at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
I´m getting this error while trying to commit to a svn repository: svn: MKACTIVITY
I am getting this error While running this LoadError: Expected /home/user/Desktop/Tripurari/myapp/app/models/host.rb to define Host##
I am getting this error while i am trying to upload a database that
Im getting following error while running the query. org.hibernate.hql.ast.QuerySyntaxException: expecting CLOSE, found 'LIMIT' near
i have this query in my code I am getting this Error. var auditMandate
While Doing clean and build i am getting following error. How to avoid this
Hi I'm getting the error while executing following code in MySQL : BEGIN DECLARE

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.