Format text in datatable c#, 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")))
        SqlDataAdapter sqlDa = new SqlDataAdapter("proc_Productlist", sqlConnection);
        sqlDa.SelectCommand.CommandType = CommandType.StoredProcedure;

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

        foreach (DataRow row in dataTable.Rows)
    return View(dataTable);

>Solution :

You didn’t reassign the result of the method ToTitleCase or ToLower to row["Name"].
If you read the documentation about ToTitleCase it says:
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.


Leave a Reply