Is there a way to capitalize first letters of each word in a parameter string?

Donald Symmons 2,856 Reputation points
2023-03-14T14:04:14.9+00:00

I tried to set letters case to store textbox values in the database table, and store the beginning of each word in capital letter, but so far, I have been able to set only capital letters into the database, with this lines.

These are two different parameters from two pages. It accepts only “ToUpper”. I know that in trying to store words and make the first letters of word to be in capital is by calling “capitalize” in stylesheet for client-side.

How can I do this in the server side?

The below code only set all the words in capital letters. I tried to replace ".ToUpper()", but capitalize did not work

cmd.Parameters.AddWithValue("@Name", Textbox1.Text.ToUpper());
objCMD.Parameters.Add("@Name", SqlDbType.NVarChar, 100).Value = Textbox1.Text.ToUpper().Trim();
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,396 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,272 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,278 questions
{count} votes

Accepted answer
  1. Lan Huang-MSFT 25,711 Reputation points Microsoft Vendor
    2023-03-15T07:33:22.3766667+00:00

    Hi @Donald Symmons,

    You can do it with regular expressions.

    For example:

    string Eventtxt = eventtxt.Text.ToString();

    Eventtxt = Regex.Replace(Eventtxt, @"\b([a-z])", m => m.Value.ToUpper());

    cmd.Parameters.AddWithValue("@ticketname", Eventtxt);

    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.


1 additional answer

Sort by: Most helpful
  1. Donald Symmons 2,856 Reputation points
    2023-03-15T08:48:23.25+00:00

    I finally got help.

    Using the TextInfo class ToTitleCase method, like this:

    This is my namespace

    using System.Globalization;
    
    

    Then added this to my cmd parameter

    objCMD.Parameters.Add("@Name", SqlDbType.NVarChar, 100).Value = CultureInfo.InvariantCulture.TextInfo.ToTitleCase(txtname.Text.Trim());
    
    

    ....and it works.

    0 comments No comments