locked
Date Format RRS feed

  • Question

  • User-797751191 posted

    Hi

      Is the below code correct . In Sql parameter Type is Date.

    cmd.Parameters.AddWithValue("@FromDate", Convert.ToDateTime("2019-02-01").ToString());

    Thanks

    Monday, July 8, 2019 1:04 PM

All replies

  • User475983607 posted

      Is the below code correct . In Sql parameter Type is Date.

    cmd.Parameters.AddWithValue("@FromDate", Convert.ToDateTime("2019-02-01").ToString());

    It does not make sense to convert a string to a date then convert back to a string.  Rather than showing code and making us guess the problem you are trying to solve, tell us what exactly what you are trying to do and include any error messages or unexpected results.

    Monday, July 8, 2019 1:15 PM
  • User-797751191 posted

    Hi mgebhard

       Is the below code correct now . In Sql parameter Type is Date.

    cmd.Parameters.AddWithValue("@FromDate", Convert.ToDateTime("2019-07-01"));

    Thanks

    Monday, July 8, 2019 1:19 PM
  • User475983607 posted

    jsshivalik

       Is the below code correct now . In Sql parameter Type is Date.

    cmd.Parameters.AddWithValue("@FromDate", Convert.ToDateTime("2019-07-01"));

    How about you tell us if your code works or not.  

    Convert.ToDateTime("2019-07-01").Date

    Monday, July 8, 2019 1:20 PM
  • User-797751191 posted

    Hi Mgebhard

       I am not getting error . In sql i have variable with Datatype as Date. I want to know which is the correct method to send parameter from c#

    cmd.Parameters.AddWithValue("@FromDate", "2019-07-01");

    Convert.ToDateTime("2019-07-01").Date

    cmd.Parameters.AddWithValue("@FromDate", Convert.ToDateTime("2019-07-01"));

    Thanks

    Monday, July 8, 2019 1:28 PM
  • User475983607 posted

    Hi Mgebhard

       I am not getting error . In sql i have variable with Datatype as Date. I want to know which is the correct method to send parameter from c#

    cmd.Parameters.AddWithValue("@FromDate", "2019-07-01");

    Convert.ToDateTime("2019-07-01").Date

    cmd.Parameters.AddWithValue("@FromDate", Convert.ToDateTime("2019-07-01"));

    Thanks

    The syntax is correct.  Asking the question infers you are having a problem. 

    1. Consult the openly published documentation if you are unsure of the correct syntax.
    2. If you are getting unexpected results then post the code, unexpected results, and expected results.

    Do not post a line code asking if the line of code is correct!  There is no way for us to know if the line of code is correct as it depends on other code that we cannot see.

    Monday, July 8, 2019 1:32 PM
  • User-797751191 posted

    Hi mgebhard

      Sorry . Out of 3 which is more correct

    Is this one ok - 

    Convert.ToDateTime("2019-07-01").Date

    Thanks

    Monday, July 8, 2019 1:39 PM
  • User475983607 posted

    Hi mgebhard

      Sorry . Out of 3 which is more correct

    Is this one ok - 

    Convert.ToDateTime("2019-07-01").Date

    Thanks

    Clearly, none of the 3 examples would be found in production code.  Usually the data is coming from user input which is run through validation code.  What you've shown is demo code which I assume you shared to convey your intention.  

    The main point is I tried to convey above is; DateTime.Date.

    https://docs.microsoft.com/en-us/dotnet/api/system.datetime.date?view=netframework-4.8

    Monday, July 8, 2019 1:51 PM
  • User288213138 posted

    Hi jsshivalik,

    You just need to make sure the value to be added is of the Date type.

    The AddWithValue of  overload that takes a String and a SqlDbType enumeration value where passing an integer with the string could be interpreted as being either the parameter value or the corresponding SqlDbType value.

    When setting command parameters, the SqlDbType and DbType are linked. Therefore, setting the DbType changes the SqlDbTypeto a supporting SqlDbType.
     
    Best Regard,
    Sam

    Tuesday, July 9, 2019 6:17 AM