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:
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:
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.