parameter date conversion

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maheshwag
    New Member
    • Aug 2010
    • 71

    #1

    parameter date conversion

    my code is:

    Code:
    string sql="insert into dummy(date)values(@date)
    
    sqlcommand cmd=new sqlcommand(sql,con)
    cmd.parameters.Add("@date", SqlDbtype.datetime).values=maskedtextbox1.text;
    cmd.ExecuteNonquery();
    it's throw varchar to date conversion error how to solve it.

    my date format is "DD/MM/YYYY"
  • hype261
    New Member
    • Apr 2010
    • 207

    #2
    I believe you are getting an error because you are .text from the maskedtextbox1 is returning a string instead of a date. To convert it you should be able to cast it to a date using the following code example.

    Code:
    DateTime convertedDate = DateTime.Parse(dateString);

    Comment

    • maheshwag
      New Member
      • Aug 2010
      • 71

      #3
      that will convert to the date but sir what about format like DD/MM/YYYY. yeah! here user should type date in dd/mm/yyyy format and sql store date in mm/dd/yyyy format that's the problem how to overcome with it. in paramerized query as per my first post.

      Comment

      • hype261
        New Member
        • Apr 2010
        • 207

        #4
        I am not big on localization for SQL Server, but I believe there is a way you can change your SQL Server to natively handle dates the way you want to.

        You may want to look at this page.

        http://msdn.microsoft.com/en-us/libr...81(VS.85).aspx

        Or this one

        http://msdn.microsoft.com/en-us/libr...(SQL.100).aspx

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          DateTime fields are NOT stored as strings in SqlServer. How you want to view them, does not matter to it.
          You can use DateTime.TryPar seExact() to supply the input format of "dd/MM/yyyy" and populate a DateTime object that you can supply to the sql parameter.

          Comment

          Working...