remove  from textbox

RAVI 1,056 Reputation points
2024-04-10T06:40:49.22+00:00

Hello

In textbox user enter something like this ABFREE on insert sql query from C# i have to remove this special charater 

I already used trim but not working cmd1.Parameters.Add(new SqlParameter("@CodeName", TextBox4Col.Text.ToUpper().Trim()));

how to do so

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,483 questions
0 comments No comments
{count} votes

Accepted answer
  1. Lan Huang-MSFT 29,336 Reputation points Microsoft Vendor
    2024-04-10T07:40:04.6166667+00:00

    Hi @RAVI,

    You can use the Regex.Replace method.

    If you only have this special character(), use the following code:

    string inStr = TextBox4Col.Text.ToString();
    string outStr = Regex.Replace(inStr, "\u0002", "");
      //.....
    cmd1.Parameters.Add(new SqlParameter("@CodeName", outStr));
    

    If there may be other special characters that need to be removed, you can use the following code.

    This will replace all non-alphabets and non-numbers.

    string inStr = TextBox4Col.Text.ToString();
    string outStr = Regex.Replace(inStr, @"[^\w\d]", "");
    

    User's image

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.