Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a Form for customer i want to save the customer basic information in a table but while saving that information i need to convert First Letter of a every string and save it to database table

Here is code for my form

XML
<div class="form-group col-md-5">
                                <label for="exampleInputEmail1">First Name : </label>
                                <asp:TextBox ID="txt_first_name" runat="server" CssClass='form-control' placeholder="First Name"></asp:TextBox>
                            </div>

                            <div class="form-group col-md-5">
                                <label for="exampleInputEmail1">Middle Name : </label>
                                <asp:TextBox ID="txt_middle_name" runat="server" CssClass='form-control' placeholder="Middle Name"></asp:TextBox>
                            </div>

                            <div class="form-group col-md-5">
                                <label for="exampleInputEmail1">Last Name : </label>
                                <asp:TextBox ID="txt_last_name" runat="server" CssClass='form-control' placeholder="Last Name"></asp:TextBox>
                            </div>



Here Is My Code For Save Button


C#
//string fn = txt_first_name.Text.Substring(0, 1).ToUpper() + txt_first_name.Text.Substring(1, txt_first_name.Text.Length-1 ).ToLower();

        cmd = new SqlCommand("new_cust", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@invoice_associate_id", custid.ToString());
        cmd.Parameters.AddWithValue("@invoice_associate_name", txt_first_name.Text.Substring(0, 1).ToUpper() + txt_first_name.Text.Substring(1, txt_first_name.Text.Length - 1).ToLower());


The commented code works perfectly but while passing parameters i need to convert it is there is any way to achieve this
Please suggest the methods to do this and thanks in advance for your help
Posted
Comments
Gihan Liyanage 12-Sep-14 7:33am    
Convert first letter to what ? it should be upper case ? like, gihan liyanage to Gihan Liyanage
Omkar Hendre 12-Sep-14 7:35am    
Yes sir i need to convert it to Upper Case
ZurdoDev 12-Sep-14 7:38am    
1. Reply to comment so the user is notified.
2. Why can't you do this? Where are you stuck?

Hi,

Try the following:
C#
cmd.Parameters.AddWithValue("@invoice_associate_name", CultureInfo.CurrentCulture.TextInfo.ToTitleCase(txt_first_name.Text.ToLower()));


It uses the ToTitleCase method top achieve the desired format.
... adding the .ToLower() to your text ensures the desired format is achieved, even if the user enter all capital letters into your textbox.

Hope it help.
 
Share this answer
 
Comments
George Jonsson 12-Sep-14 7:47am    
Nice one.
hypermellow 12-Sep-14 8:49am    
Thanks George, no point in re-inventing the wheel when it's already built into the framework.
Gihan Liyanage 12-Sep-14 7:52am    
5ed, hypermellow
if he has accepted my answer as the answer 'CultureInfo.CurrentCulture.TextInfo.ToTitleCase()' will be convert every first char of strings to upper. I have edited my answer and thanx for remembering
hypermellow 12-Sep-14 8:50am    
No worries, and thanks for updating your solution.
Use this function to convert before sending it to stored procedure

C#
public static string FirstCharToUpper(string input)
{
    if (String.IsNullOrEmpty(input))
        throw new ArgumentException("Error");
    return input.First().ToString().ToUpper() + String.Join("", input.Skip(1));
}


this will convert every first letter of string to Upper

C#
public static string FirstCharToUpper(string str)
{
     return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(str.ToLower());
}
 
Share this answer
 
v2
Comments
Omkar Hendre 12-Sep-14 7:47am    
Thanks it works for me..
Omkar Hendre 15-Sep-14 3:12am    
Sir i have another question that how i can removes the multiple given spaces between two words while passing to database
Gihan Liyanage 15-Sep-14 3:25am    
Better if you can ask it from an another thread, because this is for your first question.I will find a way till you ask. Start a new thread
Gihan Liyanage 15-Sep-14 3:37am    
Any way, This is the answer.
string dirtyString= "Hello how are you doing?";
string cleanedString = System.Text.RegularExpressions.Regex.Replace(dirtyString,@"\s+"," ");

See this Example on dotnetfiddle

https://dotnetfiddle.net/oXdAeV
Omkar Hendre 15-Sep-14 5:24am    
yeap G8 its works for me thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900