Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Member
 
Join Date: Aug 2007
Posts: 35
#1: Aug 28 '07
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?
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#2: Aug 28 '07

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()
kenobewan's Avatar
Moderator
 
Join Date: Dec 2006
Posts: 4,745
#3: Aug 28 '07

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
#4: Aug 28 '07

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"
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#5: Aug 28 '07

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?
Shashi Sadasivan's Avatar
Moderator
 
Join Date: Aug 2007
Location: Brisbane, Australia
Posts: 1,414
#6: Aug 28 '07

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
#7: Aug 29 '07

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
#8: Aug 29 '07

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?
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#9: Aug 29 '07

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
#10: Aug 30 '07

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
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#11: Aug 30 '07

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


I dunno then. It works for me.
Reply