Click here to Skip to main content
15,888,802 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
i am saving date in custom fromat dd/MM/yyyy from datetimepicker propertis. but it is saving MM/dd/yyyy in database.when i want to see a report, it works when i enter day as month and month as day.how can i solve this problem.Please help

What I have tried:

changed date customformat many times in datetimepicker propertis.but it did not work
Posted
Updated 9-Apr-18 2:55am
Comments
F-ES Sitecore 9-Apr-18 4:39am    
Dates represent a moment in time, they don't have a format until you convert them to a string, so your database isn't saving them in a specific format as it doesn't save them as strings. What is happening is you are asking the date to simply convert itself to a string without saying the format you want, so it has to use *some* format so it is probably looking at the system settings and using the default format there.

So to "fix" this you need to track down whatever is converting the date to a string and changing that process such that it uses the format you want.

Simple: you are doing it completely wrong. Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

That isn't the problem you have seen and this specific code isn't vulnerable, but it's symptomatic of the same cause and that means you urgently need to fix this through your whole app. When you do, the problem you have noticed will disappear as well because you are no longer passing a string based date to SQL, but the actual original DateTime value from the DateTimePicker, which means that SQL Server doesn't have to "guess" what date format you might have passed it. Pass the DateTimePicker.Value property directly as a parameter and there is no conversion needed, and no possibility of misinterpretation.
 
Share this answer
 
Comments
Member 11698375 9-Apr-18 4:44am    
it is saving wrong values of data and month i can't understand why? but when i save the date as 2018/03/15, it is saved as 2018/15/03.
OriginalGriff 9-Apr-18 5:08am    
No, it isn't. Look at your code, and read what I said. If you still don't understand, post the code fragment that inserts it, and the code fragment the retrieves it.
i got my answer now. it is about system language.my system language is not english US.
it is uk english.so this was the reason of saving wrong date format in sqlserver
 
Share this answer
 
Comments
Dave Kreskowiak 9-Apr-18 9:31am    
You still don't get it. THERE IS NOT DATE FORMAT WHEN YOU SAVE A DATE IN THE DATABASE. The datetime being shown when you look at the report is the datetime from the database being converted to a string in the culture of your Windows settings. You could the the exact same thing on a machines in country on the planet and get a different date formats!
The reason why datetime type was created is because date format differs from 1 part of earth to another. Note that neither dd/MM/yyyy nor MM/dd/yyyy allow you to compare 2 dates to know which 1 is earlier, and date arithmetic becomes unusable very quickly.

I strongly advice you to convert your dates to datetime type as much as possible. It will ease your life.
 
Share this answer
 

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