error:the string was not recognized as a valid DateTime.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mercea
    New Member
    • Aug 2007
    • 35

    error:the string was not recognized as a valid DateTime.

    hi all, i'm trying to insert the time/date a button was clicked on a gridview and it generates an error:the string was not recognized as a valid DateTime.There is an unknown word starting at index 0 i have changed the culture to en-US but it still doesn't work. i actually created the date/time column after some data had been entered into the table so the column allows nulls. this is my code:

    InsertCommand=" INSERT INTO test101(Surname ,Names,Registra tion,Date/Time)VALUES (@Surname, @Names, @Registration,@ DateTime,getdat e())"

    <Insert Parameters><asp :Parameter DefaultValue= DateTime.Now() Type=DateTime Name="DateTime" /></Insert Parameters>

    any suggestions?
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    You shouldn't be using DateTime.Now() but instead be using DateTime.Now, as Now is not a function.

    If it doesn't like that, try DateTime.Now.To String()

    Comment

    • kenobewan
      Recognized Expert Specialist
      • Dec 2006
      • 4871

      #3
      Have you tried Now()? I seem to recall getDate returning the name of the day first, so depends on the design of your table.

      Comment

      • mercea
        New Member
        • Aug 2007
        • 35

        #4
        Originally posted by Plater
        You shouldn't be using DateTime.Now() but instead be using DateTime.Now, as Now is not a function.

        If it doesn't like that, try DateTime.Now.To String()
        now the error says "conversion failed when converting DateTime from character to string"

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          How did you name a colum "Date/Time" what is it's data type?
          Have you checked the msdn on what datatypes convert to what?

          Comment

          • Shashi Sadasivan
            Recognized Expert Top Contributor
            • Aug 2007
            • 1435

            #6
            Originally posted by mercea
            hi all, i'm trying to insert the time/date a button was clicked on a gridview and it generates an error:the string was not recognized as a valid DateTime.There is an unknown word starting at index 0 i have changed the culture to en-US but it still doesn't work. i actually created the date/time column after some data had been entered into the table so the column allows nulls. this is my code:

            InsertCommand=" INSERT INTO test101(Surname ,Names,Registra tion,Date/Time)VALUES (@Surname, @Names, @Registration,@ DateTime,getdat e())"

            <Insert Parameters><asp :Parameter DefaultValue= DateTime.Now() Type=DateTime Name="DateTime" /></Insert Parameters>

            any suggestions?
            the fields you want to be populated are surname, name, registration, date/time (4 fields)
            and you are passing 5 values.... you should rather get an sql error

            Comment

            • mzmishra
              Recognized Expert Contributor
              • Aug 2007
              • 390

              #7
              what is the statement @DateTime,getda te() in your sql.I guess this is creating the problem

              Comment

              • mercea
                New Member
                • Aug 2007
                • 35

                #8
                hi guys, thanks for all your input. i tried all your suggestions but unfortunately non of them worked.any more?

                Comment

                • Plater
                  Recognized Expert Expert
                  • Apr 2007
                  • 7872

                  #9
                  If your column is really called "Date/Time" then:

                  InsertCommand=" INSERT INTO test101(Surname ,Names,Registra tion,Date/Time)VALUES (@Surname, @Names, @Registration,@ DateTime)"

                  <Insert Parameters><asp :Parameter DefaultValue= DateTime.Now Type=DateTime Name="DateTime" /></Insert Parameters>

                  Comment

                  • mercea
                    New Member
                    • Aug 2007
                    • 35

                    #10
                    Originally posted by Plater
                    If your column is really called "Date/Time" then:

                    InsertCommand=" INSERT INTO test101(Surname ,Names,Registra tion,Date/Time)VALUES (@Surname, @Names, @Registration,@ DateTime)"

                    <Insert Parameters><asp :Parameter DefaultValue= DateTime.Now Type=DateTime Name="DateTime" /></Insert Parameters>
                    i've tried that before and when i do that, the error is
                    :the string was not recognized as a valid DateTime.There is an unknown word starting at index 0

                    Comment

                    • Plater
                      Recognized Expert Expert
                      • Apr 2007
                      • 7872

                      #11
                      I dunno then. It works for me.

                      Comment

                      Working...