1

I'm working on a C# Web Application project built in framework 3.5 where - for some odd reason I haven't figured out - the   - (non-breaking space) is added.

This is the scenario:

I query certain data that is binded in a GridView. One of the values returned from the database is:

04/03/2013 12:00:00 a.m.

Screenshot shows the value is returned correctly:

Returned value from the Database

But, when this value is binded - i.e: GvEmployee.DataBind(); - in the "RowDataBound" event of the GridView called "GvEmployee", the value is shown as:

4/03/2013 12:00:00 a. m. - note the non-breaking space added.

and the System.FormatException exception occurs:

System.FormatException: 'String was not recognized as a valid DateTime.'

This error is due the non-breaking space and the a.m. additional string. If I remove those characters - while debugging at runtime - , the DateTime conversion can be done.

This the code where the error occurs:

protected void GvEmployee_RowDataBound(object sender, GridViewRowEventArgs e)
{
    string id; // This is a reusable variable.

    // Check rows only: 
    if (e.Row.RowIndex >= 0)
    {
        // Get the value stored in the given cell at the row: 
        id = e.Row.Cells[3].Text;
        
        // Here, the value of "id" variable is: "4/03/2013 12:00:00 a. m.".
        // But, the value returned by the database is: "04/03/2013 12:00:00 a.m.".
        
        // Check if the "id" variable is not empty...
        if (!id.Equals(" ") && !string.IsNullOrEmpty(id))
        {
            // Here fails, due to the value of "id" variable, that is: "4/03/2013 12:00:00 a. m."
            // isn't recognized as a valid DateTime value.
            e.Row.Cells[3].Text = Convert.ToDateTime(id, CultureInfo.CurrentCulture).ToShortDateString();
        }
    }
}

As you can see, the non-breaking space on the string value:

Debugged value shown the non-breaking value

This is very strange since this project is already published and only happens on my notebook - I believe there is some missing configuration in the notebook I'm using for work on this project, but, I haven't found any clue about what that could be - I didn't know about non-breaking spaces until this problem happens to me and I found this answer on Stack Overflow...

I've searched about the root of this issue and I tried solving this by adding in the Global.asax the configuration shown in this source and added here for clarity:

protected void Application_BeginRequest(Object sender, EventArgs e)
{
    System.Globalization.CultureInfo newCulture = (System.Globalization.CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
    newCulture.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
    newCulture.DateTimeFormat.AMDesignator = "";
    newCulture.DateTimeFormat.LongDatePattern = "dd/MM/yyyy";
    newCulture.DateTimeFormat.LongTimePattern = "HH:mm:ss";
    newCulture.DateTimeFormat.PMDesignator = "";
    newCulture.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
    newCulture.DateTimeFormat.TimeSeparator = ":";
    newCulture.DateTimeFormat.DateSeparator = "/";
    System.Threading.Thread.CurrentThread.CurrentCulture = newCulture;
}

and searched about how to remove those non-breaking spaces globally = by changing some configuration in the web.config and/or the Global.asax of the project, but, nothing so far that worked.

Other alternatives found are about using regex for removing the problematic characters, but, I'm looking for a global setting to apply - due to this situation happens in all the project - not just one page and not only with the given datetime value posted above, and, I have checked the response from the database and it's correct - the problem must be in the ASP.NET C# Web project, but, I don't know exactly where and why this is happening.

I don't know what else it could be, maybe is the language of the operating system?, or the Culture settings of the computer?...

I'm out of ideas, any help is appreciated.

1 Answers1

0

tl;dr: I applied these changes and settings in Visual Studio - not sure which one is the one who applied the solution to this issue, but, I'll leave it as is - since it worked for me:

  • Discard the changes I made in the Global.asax file that I described in my question.
  • Uncheck the Save documents as Unicode when data cannot be saved in codepage option found in this path: Tools > Options > Environment > Documents - credits to this answer.
  • Download the latest version of the problematic aspx/ascx file from the Source Control - (this project is on TFS) - overwriting the local files1.
  • Applied this line of code on the Site.master of the solution: <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> - credits to this answer.

1 I forgot mention that this project is on TFS and when I downloaded the project on this notebook, I got this warning:

"This file contains characters in Unicode format which will be lost if you save this file as an ANSI encoded text file. To keep the Unicode information, click cancel below and then select one of the Unicode options from the Encoding drop-down list. Do you want to continue?"

OK / Cancel

warning message

Source of the image

I ignored that warning and I think I select OK -> I think this is the root of the problem.


Long version: The project I'm working is on TFS; when I downloaded the project in the notebook - which is a brand new one - I remember I got the warning that says:

This file contains characters in Unicode format which will be lost if you save this file as an ANSI encoded text file. To keep the Unicode information, click Cancel below and then select one of the Unicode options from the Encoding drop down list. Continue?

I selected OK - if I recall correctly - and the project downloading continued without more warnings.

When I compiled all parts of the project and execute it, all was good - until I came to retrieve some data - for check if all was working fine (since this project uses Oracle as database and in my experience with this project and Oracle is that sometimes, unexpected errors presents) and I got the error I described in the question.

After trying all I could possibly could think of, suddenly I remembered this warning I got when I download the project.

Then, I start searching about how check the encoding of the files in the project and I got this answer (1) - this answer (2) looked promising too, but, when I tried and - as I understand - the first linked answer says to make those steps file by file, manually = which is not viable; about the second linked answer, I could not find the location in the .sln or .csproj file.

I keep searching and added visual studio check aspx encoding on Google, one of the answers was this answer and I added the line on the Site.master of the project - since all pages uses this Master Page:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 

When I did execute the solution/project, the non-breaking spaces where gone and the DateTime conversion was successful as is shown in this screenshot:

Conversion successful without non-breaking spaces