I am using SQL Server Express 2005 as my Database and programming in C# 2005. In one of my Table "Results" I have a field named "ResultDate" of DateTime data type. In my form I have a DateTimePicker. I want to save only one record for a particular date, hence while saving the record I am checking whether a record for that particular date is available.
My Query string is "Select * from Results where ResultDate = " + dtpresultDate.value;
But it is not working. SQL stores both Date and Time in a DateTime field, so it is saving 12:00:00 in addition to the given date while saving the record. DateTimePicker is including the current time to the value. I tried to use "dtpResultDate.Value.ToShortDateString", but it is giving me an error about data type mismatch. I also tried using convert, but the problem was in India we generally use "dd/mm/yy" as short date format, whereas the closest match is "dd/mm/yyyy". Do I have to use "dd/mm/yyyy" in my DateTimePicker also?
How should I modify the Query string so that a match is done only using the Date parts (ignoring the Times)?
Also can I use Count to check whether a record is available?
Thank you.
Lalit Kumar Barik
India
My Query string is "Select * from Results where ResultDate = " + dtpresultDate.value;
But it is not working. SQL stores both Date and Time in a DateTime field, so it is saving 12:00:00 in addition to the given date while saving the record. DateTimePicker is including the current time to the value. I tried to use "dtpResultDate.Value.ToShortDateString", but it is giving me an error about data type mismatch. I also tried using convert, but the problem was in India we generally use "dd/mm/yy" as short date format, whereas the closest match is "dd/mm/yyyy". Do I have to use "dd/mm/yyyy" in my DateTimePicker also?
How should I modify the Query string so that a match is done only using the Date parts (ignoring the Times)?
Also can I use Count to check whether a record is available?
Thank you.
Lalit Kumar Barik
India
Mirko MeierPosted Feb 18, 2009, 5:44 AM
As Parameter you set the DateTime as Input Parameter.
Detailed Information about Parameterized Statements
you will find here: MSDN
LisaPosted Feb 18, 2009, 1:28 AM
what you can do is pass it as a normal string
for example 18/02/2009 is todays date pass it as 20091802, the sql server can accept it and will check for feb 18th 2009 in the database.
Hope it Helps,
Lisa