Click here to Skip to main content
Click here to Skip to main content

Replace Escape Character (C#)

, 4 Sep 2006
Rate this:
Please Sign up or sign in to vote.
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".

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

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

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

  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).

  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

  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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Md. Zikrul Ahsan
Web Developer
Bangladesh Bangladesh
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#).
Group type: Organisation (No members)



Comments and Discussions

 
GeneralMy vote of 5 PinmemberJahid Khan25-Mar-12 8:29 
GeneralHelpful function PinmemberBrad Bruce5-Sep-06 1:15 
AnswerRe: Helpful function PinmemberAnonymuos5-Sep-06 7:54 

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.

| Advertise | Privacy | Mobile
Web04 | 2.8.140709.1 | Last Updated 5 Sep 2006
Article Copyright 2006 by Md. Zikrul Ahsan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid