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

Donald Symmons 3,066 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();
Developer technologies | .NET | Other
Developer technologies | ASP.NET | Other
Developer technologies | C#
{count} vote

Accepted answer
  1. Lan Huang-MSFT 30,191 Reputation points Microsoft External Staff
    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 3,066 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

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.