Click here to Skip to main content
15,893,663 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
cmd.Parameters.AddWithValue("@dateFrom",DateTime.Now.ToString("dd/MM/yyyy"));


//storing as date in my db

//error is Conversion failed when converting date and/or time from character string.
Posted
Updated 30-Jan-14 22:44pm
v2

don't convert datetime.now to string. just add it as it is.
C#
cmd.Parameters.AddWithValue("@dateFrom",DateTime.Now)
 
Share this answer
 
Comments
Member 10548723 31-Jan-14 4:46am    
but datetime.now is getting the current date of mine, not the selected date.
Vedat Ozan Oner 31-Jan-14 4:52am    
:) yes, of course, it is your datetime. you have coded that. i can't know what you are trying to do. i just gave the solution for the given problem which is "//error is Conversion failed when converting date and/or time from character string."
Try this

C#
cmd.Parameters.AddWithValue("@dateFrom" , Convert.ToDateTime( DateTime.Now.ToShortDateString()));
 
Share this answer
 
Comments
phil.o 31-Jan-14 10:04am    
Well: the DB expects a DateTime.
You then take a DateTime (Now), convert it to string, and use the Convert function to convert it back to DateTime.
I'm sorry, but I cannot agree with you on that.
Karthik_Mahalingam 31-Jan-14 10:06am    
it will save in the DB as time current date with time stamp as 12:00:AM.. which will be easy for querying..
else it will save with the current TIme stamp..
hop you understood ??
phil.o 31-Jan-14 10:09am    
Then use DateTime.Today. Much more clean, and does not involve two opposite conversions in the process.
It may 'work', but, I'm sorry, it's ugly :(
Karthik_Mahalingam 31-Jan-14 10:32am    
If the sql column is of type date your logic will work. What if datetime column

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900