Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Format text in datatable c#, asp.net core

I would like to format my text to Titlecase or lowercase in the controller before viewing in Razor page. but it is not working. what I am doing wrong here. I see no effect in view.

public IActionResult Index()
{
    DataTable dataTable = new DataTable();
    using (SqlConnection sqlConnection = new SqlConnection(_configuration.GetConnectionString("abc")))
    {
        sqlConnection.Open();
        SqlDataAdapter sqlDa = new SqlDataAdapter("proc_Productlist", sqlConnection);
        sqlDa.SelectCommand.CommandType = CommandType.StoredProcedure;
        sqlDa.Fill(dataTable);

        TextInfo textInfo = new CultureInfo("en-US", false).TextInfo;

        foreach (DataRow row in dataTable.Rows)
        {
            textInfo.ToTitleCase(row["Name"].ToString().ToLower());
            textInfo.ToLower(row["Made"].ToString().ToLower());
        }
    }
    return View(dataTable);
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You didn’t reassign the result of the method ToTitleCase or ToLower to row["Name"].
If you read the documentation about ToTitleCase it says:
Returns
The specified string converted to title case.

You need to write:

row["Name"] = textInfo.ToTitleCase(row["Name"].ToString().ToLower());
row["Made"] = textInfo.ToLower(row["Made"].ToString().ToLower());

Have a nice day.

Source: https://docs.microsoft.com/fr-fr/dotnet/api/system.globalization.textinfo.totitlecase?view=net-5.0

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading