error:the string was not recognized as a valid DateTime. | Member | | Join Date: Aug 2007
Posts: 35
| | |
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,Registration,Date/Time)VALUES (@Surname, @Names, @Registration,@DateTime,getdate())"
<Insert Parameters><asp:Parameter DefaultValue= DateTime.Now() Type=DateTime Name="DateTime" /></Insert Parameters>
any suggestions?
|  | Moderator | | Join Date: Apr 2007 Location: New England
Posts: 7,161
| | | re: error:the string was not recognized as a valid DateTime.
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.ToString()
|  | Moderator | | Join Date: Dec 2006
Posts: 4,745
| | | re: error:the string was not recognized as a valid DateTime.
Have you tried Now()? I seem to recall getDate returning the name of the day first, so depends on the design of your table.
| | Member | | Join Date: Aug 2007
Posts: 35
| | | re: error:the string was not recognized as a valid DateTime. Quote:
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.ToString() now the error says "conversion failed when converting DateTime from character to string"
|  | Moderator | | Join Date: Apr 2007 Location: New England
Posts: 7,161
| | | re: error:the string was not recognized as a valid DateTime.
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?
|  | Moderator | | Join Date: Aug 2007 Location: Brisbane, Australia
Posts: 1,414
| | | re: error:the string was not recognized as a valid DateTime. Quote:
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,Registration,Date/Time)VALUES (@Surname, @Names, @Registration,@DateTime,getdate())"
<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
| | Expert | | Join Date: Aug 2007 Location: nj,usa
Posts: 299
| | | re: error:the string was not recognized as a valid DateTime.
what is the statement @DateTime,getdate() in your sql.I guess this is creating the problem
| | Member | | Join Date: Aug 2007
Posts: 35
| | | re: error:the string was not recognized as a valid DateTime.
hi guys, thanks for all your input. i tried all your suggestions but unfortunately non of them worked.any more?
|  | Moderator | | Join Date: Apr 2007 Location: New England
Posts: 7,161
| | | re: error:the string was not recognized as a valid DateTime.
If your column is really called "Date/Time" then:
InsertCommand="INSERT INTO test101(Surname,Names,Registration,Date/Time)VALUES (@Surname, @Names, @Registration,@DateTime)"
<Insert Parameters><asp:Parameter DefaultValue= DateTime.Now Type=DateTime Name="DateTime" /></Insert Parameters>
| | Member | | Join Date: Aug 2007
Posts: 35
| | | re: error:the string was not recognized as a valid DateTime. Quote:
Originally Posted by Plater If your column is really called "Date/Time" then:
InsertCommand="INSERT INTO test101(Surname,Names,Registration,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
|  | Moderator | | Join Date: Apr 2007 Location: New England
Posts: 7,161
| | | re: error:the string was not recognized as a valid DateTime.
I dunno then. It works for me.
|  | Similar .NET Framework bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,411 network members.
|