Home > Mobile >  Format text in datatable c#, asp.net core
Format text in datatable c#, asp.net core

Time:11-05

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);
}

CodePudding user response:

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

  • Related