locked
How Can I Ignore System Date Formate In My String RRS feed

  • Question

  • Hi All

    I am Facing A problem in DateTime Function it  show me a Error : FAILED TO CONVERT PARAMETER VALUE FROM A STRING TO A DATETIME

    my System Date Format is "MM/dd/yyyy" and in Software i am using this string

    sCommand.Parameters["@ExchangeRateDate"].Value = Convert.ToDateTime(lblCurrentDate.Text).ToString("yyyy-MM-dd");

    i want to ignore System Date Format Because i never know which type of date format using my customer

     

    Regards!

    Hites

     

     

    Friday, November 19, 2010 6:25 AM

Answers

  • if you just want to have the curent datetime, you could use directly:
    sCommand.Parameters["@ExchangeRateDate"].Value = DateTime.Now;

    And when you have a UI (Label sounds like an user application), then you could stick to the culture settings. There is no need to fix the format to "dd/MM/yyyy". That way, you can be sure, that the date is exactly in the format, that the system requires - and then it can be parsed again.

    Or if you want to fix the format, you must make sure, that you tell the system, that the format is exactly in that format.

    But in general, you should avoid translations that are not required. You have a DateTime, you convert it to a String. Then you convert it back to a DateTime. Just store the DateTime then (This is the native format, that should be safed) and only convert it quickly when you need to display it to the user (Which is fully ok, I just ould keep the regional setting so each user gets exactly what he wants to seem so I would like to see dd.MM.yyyy with my DE-de settings. To reach this, ToShortDateString method could be used.)

    Also important: If you only want to get the Date, then you could use DateTime.Now.Date.

    I hope I was able to explain it in an understandable way and that I didn't mix up to many topics in not enough text. Just ask if you want me to clarify it more in detail.

    With kind regards,

    Konrad

    Friday, November 19, 2010 7:50 AM

All replies

  • Just a dumb question from my side:  @ExchangeRateDate is not provided as DateTime / DateTime2 or so? In databases I would always store dates in the appropriate type and not just as varchar or so!

    So I can understand, that you do a Convert.ToDateTime(lblCurrentDate.Text) so you get the date. But why do you try to convert it back to text?

    Playing around with dates as text is never a good idea, because your application is likely to fail because some settings change. For some reason an administrator changes the default regional settings or something like that.

    Ok, you could hardcode it everywhere. So your application converts everything in a fixed format and on the datbase side, you convert it back. But this work is not required, because the database provider can take care for you and makes sure, that a date stored in your application is transmitted to the database correctly.

    The exception text sounds like a typical message that you provide a text and the database expects a datetime. Thi normaly happens when people do not use parameters and instead build their SQL scripts manualy through concatenation of strings. So hopefully, all you need to do is removing the ".ToString("yyyy-MM-dd")" from your code. As far as I know, the exception message from Convert Class looks different.

    With kind regards,

    Konrad

    Friday, November 19, 2010 6:50 AM
  • hi

                 sCommand.Parameters.Add("@ExchangeRateDate", SqlDbType.Date);
               
                sCommand.Parameters["@ExchangeRateDate"].Value = Convert.ToDateTime(lblCurrentDate.Text);

     

    this will display Error: STRING WAS NOT RECOGNIZED AS A VALID DATETIME

     

    Hitesh

    Friday, November 19, 2010 7:03 AM
  • What is lblCurrentDate.Text? How did you fill this field and what regional settings is your application running under?

    That error message sounds like the parse error of the converting now. If it is a user input, then you should validate the input first. (e.g. a DateTime.TryParse call that stores the date in a field or property if it is successfull.)

    With kind regards,

    Konrad

    Friday, November 19, 2010 7:21 AM
  • lblCurrentDate.tex is a LABLE i fill this

    lblCurrentDate.Text = Convert.ToDateTime(DateTime.Now.Date).ToString("dd/MM/yyyy");

     

    Friday, November 19, 2010 7:27 AM
  • if you just want to have the curent datetime, you could use directly:
    sCommand.Parameters["@ExchangeRateDate"].Value = DateTime.Now;

    And when you have a UI (Label sounds like an user application), then you could stick to the culture settings. There is no need to fix the format to "dd/MM/yyyy". That way, you can be sure, that the date is exactly in the format, that the system requires - and then it can be parsed again.

    Or if you want to fix the format, you must make sure, that you tell the system, that the format is exactly in that format.

    But in general, you should avoid translations that are not required. You have a DateTime, you convert it to a String. Then you convert it back to a DateTime. Just store the DateTime then (This is the native format, that should be safed) and only convert it quickly when you need to display it to the user (Which is fully ok, I just ould keep the regional setting so each user gets exactly what he wants to seem so I would like to see dd.MM.yyyy with my DE-de settings. To reach this, ToShortDateString method could be used.)

    Also important: If you only want to get the Date, then you could use DateTime.Now.Date.

    I hope I was able to explain it in an understandable way and that I didn't mix up to many topics in not enough text. Just ask if you want me to clarify it more in detail.

    With kind regards,

    Konrad

    Friday, November 19, 2010 7:50 AM
  • hi Thank for your Help now i get what i need

    Thanks Again

     

     

    Friday, November 19, 2010 8:10 AM