Click here to Skip to main content
Licence 
First Posted 4 Sep 2006
Views 65,328
Bookmarked 12 times

Replace Escape Character (C#)

By | 4 Sep 2006 | Article
For the beginning we are facing the same problem while inserting a text with escaped character like single quote (‘) into the database. That’s why we’ve solved this problem by creating our own library and reuse that library.

Introduction:

For the beginning we are facing the same problem while inserting a text with escaped character like single quote (') into the database. That's why we've solved this problem by creating our own library and reuse that library.

This example is based on ASP.Net 2.0 with Visual Studio 2005 and using C#.


Start the project:

  1. Start Visual Studio and start a new "Web Site". Give the project name "ReplaceEscapeCharacters".
    Start Visual Studio

  2. Give a title of the "default.aspx" file "Replace Escape Characters" and rename the "Form" ID to "frmReplaceEscapeCharacters". This is a optional work.
    Change Form Name

  3. Click right mouse button in the project name of "Solution Explorer". A pop-up menu will appear, choose "Add New Item…" and click.
    Add New Item...

  4. A Template library of Visual Studio will appear. Choose a "Class" Template and give a name "MyClass.cs" of the template.
    Add Class

  5. A message box will appear showing whether you want to add that class file into "App_Code" folder or not. Choose "Yes" from the three options. It will make a "App_Code" folder into your project and add the newly build "MyClass.cs" file into that folder. If you add your class into "App_Code" folder, that class file will be easily accessible all over your project(s).
    Message Box

    Message Box

  6. Now right the following codes into "MyClass.cs".
    public static string ReplaceEscapeChars(string str)
    {
        //If the string is null
        if (str == null)
            return str;
    
        //If the string is empty
        if (str == "")
            return str;
    
        //Replaces single quote (') with two (2) single quotes ('')
        //solves the problem of inserting, updating or selecting a text with single quote (')
        //i.e.: Cox's Bazar, World's economy etc.
        str = str.Replace("'", "''");
        return str;
    }
    

Using the library:

  1. Add some control into the "default.aspx" file
    Name: <asp:TextBox ID="txtName" runat="server" />
    Address: <asp:TextBox ID="txtAddress" runat="server" />
    Age: 
    <asp:DropDownList ID="cboSex" runat="server">
        <asp:ListItem Value="M"> Male </ asp:ListItem>
        <asp:ListItem Value="F"> Female </ asp:ListItem>
    </asp:DropDownList>
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
    
    <asp:Label ID="lblShowResult" runat="server" Text="" />
    

  2. In the click event of the submit button write the following code
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //Declare variables
        string strName = "", strAddress = "", strSex = "";
        string strMyResult = "";
    
        //Collecting values from the front-end
        strName     = MyClass.ReplaceEscapeChars(txtName.Text);
        strAddress  = MyClass.ReplaceEscapeChars(txtAddress.Text);
        strSex      = cboSex.SelectedItem.ToString();
    
        //Building the string for showing the result
        strMyResult  = "<b>Name:</b> " + strName + "<br />";
        strMyResult += "<b>Address:</b> " + strAddress + "<br />";
        strMyResult += "<b>Sex:</b> " + strSex + "<br />";
    
        //Showing the result into the text
        lblShowResult.Text = strMyResult;
    }
    

  3. Run the project
    Add Class

  4. You've seen that Cox's Bazar has been changed to Cox''s Bazar. Now if you want this value into the database, it will not generate any error.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Md. Zikrul Ahsan

Web Developer

Bangladesh Bangladesh

Member



Organisation (No members)

Working in IT field since 1996 and working in Software field since 1999. Worked with C/C++, VC++, ASP 3.0, PHP, JSP, Java, VB6. Currently working with .Net (mostly C#).

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PinmemberJahid Khan8:29 25 Mar '12  
GeneralHelpful function PinmemberBrad Bruce1:15 5 Sep '06  
AnswerRe: Helpful function PinmemberAnonymuos7:54 5 Sep '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 5 Sep 2006
Article Copyright 2006 by Md. Zikrul Ahsan
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid