DateTime issue for about 3-4 days

Discussion in 'ASP.NET / ASP.NET Core' started by Razor, Jul 22, 2005.

Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.
  1. Hello,

    This problem seems to have materialised over the past few days without any change of code and am not sure whether there is a bug with the code or the server itself.

    To help keep track of users that are logging on to my site I have setup a table that consists of 3 separate columns:

    WebCounter_Site = Site code, I have multiple accounts that I may wish to report to the same database in the near future. This is a simple integer value.
    WebCounter_IP = IP address of the host that has access the site. This is of the varchar (16 chars) type.
    WebCounter_Date = The date/time that the client made the connection.

    Here is a stripped version of the code:

    //Setup the connection
    SqlConnection sqlConn=new sqlConnection("xxxxxxx");
    //The command itself - Writes the sitecode, Date and IP address to the SQL server
    string strCmd="INSERT INTO WebCounter (WebCounter_Date, WebCounter_IP,WebCounter_Site) VALUES ('"+DateTime.Now.ToString()+"','"+Request.ServerVariables["REMOTE_HOST"].ToString()+"','"+intWebSiteNumber.ToString()+"')";
    //Setup the command that will be passed to the sql server.
    SqlCommand sqlCmd=new SqlCommand(strCmd,sqlConn);
    //Perform the actual command
    sqlCmd.ExecuteNonQuery();

    Now in the past this has worked fine, but since last week it has thrown up the following error message:


    Server Error in '/' Application.


    The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value. The statement has been terminated.





    The only way around this is to convert the DateTime.Now.ToString() (the bit that is causing the problem) to DateTime.Now.ToLongDateString() - the only problem with this is that it:


    1) Only shows the date


    2) Sometimes shows some weird and wonder stuff that such as 22/12/2005 etc. As the time is pulled from the server I have ruled out it being a client problem.


    I have also tried other ones such as ToTimeLongString, ticks etc - they other come with corrupt values, or simply miss out the time and/or date completely.


    Any suggestions would be more than appreciated. :)


    Best regards,





    Mark
     
  2. I knew I should have checked the code before posting this message.. :)

    Solution / Reason why it happened

    As part of the initialisation code I have modified the culture of the web page for GB use to default to £ currency as opposed to the normal $ currency default.




    Thread.CurrentThread.CurrentCulture=new CultureInfo("en-gb");





    However, as modifying the Culture to UK(GB) also has the knock on effect of modifying the datetimeformat to DD/MM/YY (as opposed to MM/DD/YY) - it was obviously giving an invalid date format to the SQL server that is most probably expecting to receive a US format date standard.





    Mini Crisis over! [​IMG]
     
Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.

Share This Page